1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
|
from pathlib import Path
import pytest
from fpdf import FPDF, TextStyle, TitleStyle, errors
from fpdf.enums import Align
from fpdf.outline import TableOfContents
from test.conftest import LOREM_IPSUM, assert_pdf_equal
HERE = Path(__file__).resolve().parent
def test_simple_outline(tmp_path):
pdf = FPDF()
pdf.set_font("Helvetica")
pdf.add_page()
pdf.set_y(50)
pdf.set_font(size=40)
p(pdf, "Doc Title", align="C")
pdf.set_font(size=12)
pdf.insert_toc_placeholder(render_toc)
insert_test_content(pdf)
assert_pdf_equal(pdf, HERE / "simple_outline.pdf", tmp_path)
def render_toc(pdf, outline):
pdf.y += 50
pdf.set_font("Helvetica", size=16)
pdf.underline = True
p(pdf, "Table of contents:")
pdf.underline = False
pdf.y += 20
pdf.set_font("Courier", size=12)
for section in outline:
link = pdf.add_link(page=section.page_number)
p(
pdf,
f'{" " * section.level * 2} {section.name} {"." * (60 - section.level*2 - len(section.name))} {section.page_number}',
align="C",
link=link,
)
def test_insert_toc_placeholder_with_invalid_arg_type():
pdf = FPDF()
pdf.add_page()
with pytest.raises(TypeError):
pdf.insert_toc_placeholder("render_toc")
def test_insert_toc_placeholder_twice():
pdf = FPDF()
pdf.add_page()
pdf.insert_toc_placeholder(render_toc)
with pytest.raises(errors.FPDFException):
pdf.insert_toc_placeholder(render_toc)
def test_incoherent_start_section_hierarchy():
pdf = FPDF()
pdf.add_page()
with pytest.raises(ValueError):
pdf.start_section("Title", level=-1)
pdf.start_section("Title", level=0)
with pytest.raises(ValueError):
pdf.start_section("Subtitle", level=2)
def test_start_section_horizontal_alignment(tmp_path): # issue-1282
pdf = FPDF()
pdf.add_page()
pdf.set_font("Helvetica", size=20)
# left align
level0 = TextStyle(font_size_pt=20, l_margin=Align.L)
pdf.set_section_title_styles(level0)
pdf.start_section("left aligned section")
# center align
level0 = TextStyle(font_size_pt=20, l_margin=Align.C)
pdf.set_section_title_styles(level0)
pdf.start_section("center aligned section")
# right align
level0 = TextStyle(font_size_pt=20, l_margin=Align.R)
pdf.set_section_title_styles(level0)
pdf.start_section("right aligned section")
assert_pdf_equal(
pdf, HERE / "test_start_section_horizontal_alignment.pdf", tmp_path
)
def test_set_section_title_styles_with_invalid_arg_type():
pdf = FPDF()
with pytest.raises(TypeError):
pdf.set_section_title_styles("Times")
def test_2_pages_outline(tmp_path):
pdf = FPDF()
pdf.set_font("Helvetica")
pdf.set_section_title_styles(
# Level 0 titles:
TextStyle(
font_family="Times",
font_style="B",
font_size_pt=24,
color=128,
underline=True,
t_margin=10,
l_margin=10,
b_margin=0,
),
)
pdf.add_page()
pdf.set_y(50)
pdf.set_font(size=40)
p(pdf, "Doc Title", align="C")
pdf.set_font(size=12)
pdf.insert_toc_placeholder(render_toc, pages=2)
for i in range(40):
pdf.start_section(f"Title {i}")
p(
pdf,
(
"Lorem ipsum dolor sit amet, consectetur adipiscing elit,"
" sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
),
)
assert_pdf_equal(pdf, HERE / "2_pages_outline.pdf", tmp_path)
def test_2_pages_outline_with_deprecated_TitleStyle(tmp_path):
pdf = FPDF()
pdf.set_font("Helvetica")
with pytest.warns(DeprecationWarning):
pdf.set_section_title_styles(
# Level 0 titles:
TitleStyle(
font_family="Times",
font_style="B",
font_size_pt=24,
color=128,
underline=True,
t_margin=10,
l_margin=10,
b_margin=0,
),
)
pdf.add_page()
pdf.set_y(50)
pdf.set_font(size=40)
p(pdf, "Doc Title", align="C")
pdf.set_font(size=12)
pdf.insert_toc_placeholder(render_toc, pages=2)
for i in range(40):
pdf.start_section(f"Title {i}")
p(
pdf,
(
"Lorem ipsum dolor sit amet, consectetur adipiscing elit,"
" sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
),
)
assert_pdf_equal(pdf, HERE / "2_pages_outline.pdf", tmp_path)
def test_toc_with_nb_and_footer(tmp_path): # issue-548
class TestPDF(FPDF):
def render_toc(self, outline):
self.x = self.l_margin
self.set_font(size=12)
for section in outline:
self.ln()
self.cell(text=section.name)
def footer(self):
self.set_y(-15)
self.set_font("helvetica", style="I", size=8)
self.cell(0, 10, f"Page {self.page_no()}/{{nb}}", align="C")
pdf = TestPDF()
pdf.set_font(family="helvetica", size=12)
pdf.add_page()
pdf.insert_toc_placeholder(TestPDF.render_toc, pages=2)
for i in range(1, 80):
pdf.set_font(style="B")
pdf.start_section(f"Section {i}")
pdf.cell(text=f"Section {i}")
pdf.ln()
assert_pdf_equal(pdf, HERE / "toc_with_nb_and_footer.pdf", tmp_path)
@pytest.mark.skip(reason="Font related tests are failing with the fonts available in Debian")
def test_toc_with_russian_heading(tmp_path): # issue-320
pdf = FPDF()
pdf.add_font(fname="test/fonts/Roboto-Regular.ttf")
pdf.set_font("Roboto-Regular")
pdf.add_page()
pdf.start_section("Русский, English, 1 2 3...")
pdf.write(8, "Русский текст в параграфе.")
assert_pdf_equal(pdf, HERE / "toc_with_russian_heading.pdf", tmp_path)
def test_toc_with_thai_headings(tmp_path): # issue-458
pdf = FPDF()
for txt in [
"ลักษณะเฉพาะของคุณ",
"ระดับฮอร์โมนเพศชาย",
"ระดับฮอร์โมนเพศหญิง",
"hello",
]:
pdf.add_page()
pdf.start_section(txt)
assert_pdf_equal(pdf, HERE / "toc_with_thai_headings.pdf", tmp_path)
def test_toc_without_font_style(tmp_path): # issue-676
pdf = FPDF()
pdf.set_font("helvetica")
pdf.set_section_title_styles(
level0=TextStyle(font_size_pt=28, l_margin=10), level1=TextStyle()
)
pdf.add_page()
pdf.start_section("Title")
pdf.start_section("Subtitle", level=1)
assert_pdf_equal(pdf, HERE / "toc_without_font_style.pdf", tmp_path)
def test_toc_with_font_style_override_bold(tmp_path): # issue-1072
pdf = FPDF()
pdf.add_page()
pdf.set_font("Helvetica", style="B")
pdf.set_section_title_styles(TextStyle("Helvetica", font_size_pt=20))
pdf.start_section("foo")
assert_pdf_equal(pdf, HERE / "toc_with_font_style_override_bold1.pdf", tmp_path)
pdf = FPDF()
pdf.add_page()
pdf.set_font("Helvetica", style="B")
pdf.set_section_title_styles(TextStyle("Helvetica", font_size_pt=20, font_style=""))
pdf.start_section("foo")
assert_pdf_equal(pdf, HERE / "toc_with_font_style_override_bold2.pdf", tmp_path)
def test_toc_without_font_style_with_deprecated_TitleStyle(tmp_path):
pdf = FPDF()
pdf.set_font("helvetica")
with pytest.warns(DeprecationWarning):
pdf.set_section_title_styles(
level0=TitleStyle(font_size_pt=28, l_margin=10), level1=TitleStyle()
)
pdf.add_page()
pdf.start_section("Title")
pdf.start_section("Subtitle", level=1)
assert_pdf_equal(pdf, HERE / "toc_without_font_style.pdf", tmp_path)
def test_toc_with_font_style_override_bold_with_deprecated_TitleStyle(tmp_path):
pdf = FPDF()
pdf.add_page()
pdf.set_font("Helvetica", style="B")
with pytest.warns(DeprecationWarning):
pdf.set_section_title_styles(
TitleStyle("Helvetica", font_size_pt=20, color=(0, 0, 0))
)
pdf.start_section("foo")
assert_pdf_equal(pdf, HERE / "toc_with_font_style_override_bold1.pdf", tmp_path)
pdf = FPDF()
pdf.add_page()
pdf.set_font("Helvetica", style="B")
with pytest.warns(DeprecationWarning):
pdf.set_section_title_styles(
TitleStyle("Helvetica", font_style="", font_size_pt=20, color=(0, 0, 0))
)
pdf.start_section("foo")
assert_pdf_equal(pdf, HERE / "toc_with_font_style_override_bold2.pdf", tmp_path)
def test_toc_with_table(tmp_path): # issue-1079
def render_toc_with_table(pdf: FPDF, outline: list):
pdf.set_font(size=20)
with pdf.table([[x.name, str(x.page_number)] for x in outline]):
pass
pdf = FPDF()
pdf.set_font(family="helvetica", size=30)
pdf.add_page()
pdf.insert_toc_placeholder(render_toc_function=render_toc_with_table, pages=4)
for i in range(60):
pdf.start_section(name=str(i), level=0)
pdf.cell(text=str(i))
pdf.ln()
assert_pdf_equal(pdf, HERE / "toc_with_table.pdf", tmp_path)
def test_toc_with_right_aligned_page_numbers(tmp_path):
def render_toc_with_right_aligned_page_numbers(pdf, outline):
pdf.set_font("Helvetica", size=16)
for section in outline:
link = pdf.add_link(page=section.page_number)
pdf.cell(
text=f'{" " * section.level * 2} {section.name}',
link=link,
new_x="LEFT",
)
pdf.cell(text=f"{section.page_number}", link=link, w=pdf.epw, align="R")
pdf.ln()
pdf = FPDF()
pdf.set_font("Helvetica", size=12)
pdf.add_page()
pdf.insert_toc_placeholder(render_toc_with_right_aligned_page_numbers)
insert_test_content(pdf)
assert_pdf_equal(pdf, HERE / "toc_with_right_aligned_page_numbers.pdf", tmp_path)
def p(pdf, text, **kwargs):
"Inserts a paragraph"
pdf.multi_cell(
w=pdf.epw,
h=pdf.font_size,
text=text,
new_x="LMARGIN",
new_y="NEXT",
**kwargs,
)
def insert_test_content(pdf):
pdf.set_section_title_styles(
# Level 0 titles:
TextStyle(
font_family="Times",
font_style="B",
font_size_pt=24,
color=128,
underline=True,
t_margin=10,
l_margin=10,
b_margin=0,
),
# Level 1 subtitles:
TextStyle(
font_family="Times",
font_style="B",
font_size_pt=20,
color=128,
underline=True,
t_margin=10,
l_margin=20,
b_margin=5,
),
)
pdf.start_section("Title 1")
pdf.start_section("Subtitle 1.1", level=1)
p(
pdf,
(
"Lorem ipsum dolor sit amet, consectetur adipiscing elit,"
" sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
),
)
pdf.add_page()
pdf.start_section("Subtitle 1.2", level=1)
p(
pdf,
"Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.",
)
pdf.add_page()
pdf.start_section("Title 2")
pdf.start_section("Subtitle 2.1", level=1)
p(
pdf,
"Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.",
)
pdf.add_page()
pdf.start_section("Subtitle 2.2", level=1)
p(
pdf,
"Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
)
def test_insert_toc_placeholder_with_last_page_in_landscape(tmp_path):
pdf = FPDF()
pdf.set_font("Helvetica")
pdf.add_page()
pdf.set_y(50)
pdf.set_font(size=40)
p(pdf, "Doc Title", align="C")
pdf.set_font(size=12)
pdf.insert_toc_placeholder(render_toc)
insert_test_content(pdf)
pdf.add_page(orientation="L")
pdf.start_section("Title 3")
p(pdf, text="Nullam tempus magna quam, ac dictum neque blandit quis.")
assert_pdf_equal(
pdf, HERE / "insert_toc_placeholder_with_last_page_in_landscape.pdf", tmp_path
)
@pytest.mark.parametrize(
"test_number",
tuple(range(3)),
)
def test_toc_extra_pages_with_labels(tmp_path, test_number):
file_content = [
{
"name": "Chapter 1: The Origins of Procrastination",
"level": 0,
"paragraphs": 1,
},
{
"name": "1.1 A Brief History of Putting Things Off",
"level": 1,
"paragraphs": 3,
},
{
"name": "1.1.1 Ancient Procrastinators: Pharaohs and Philosophers",
"level": 2,
"paragraphs": 3,
},
{
"name": "1.1.2 Renaissance Slacking: Michelangelo's 4-Year Ceiling Break",
"level": 2,
"paragraphs": 2,
},
{
"name": "1.2 Famous Procrastinators Through Time",
"level": 1,
"paragraphs": 2,
},
{"name": "1.2.1 The Tale of Last-Minute Da Vinci", "level": 2, "paragraphs": 3},
{
"name": "1.2.2 Procrastination in the Age of the Internet",
"level": 2,
"paragraphs": 4,
},
{
"name": "Chapter 2: The Science Behind Delaying Everything",
"level": 0,
"paragraphs": 1,
},
{"name": "2.1 Why We Procrastinate", "level": 1, "paragraphs": 2},
{
"name": "2.1.1 The Pleasure Principle vs. Deadline Panic",
"level": 2,
"paragraphs": 2,
},
{
"name": "2.1.2 Brain Chemistry: Dopamine's Role in Putting Off Tasks",
"level": 2,
"paragraphs": 3,
},
{"name": "2.2 The Types of Procrastinators", "level": 1, "paragraphs": 2},
{"name": "2.2.1 The Creative Avoider", "level": 2, "paragraphs": 4},
{"name": "2.2.2 The Perfectionist Delayer", "level": 2, "paragraphs": 2},
{"name": "2.2.3 The Professional Postponer", "level": 2, "paragraphs": 3},
{
"name": "Chapter 3: Procrastination Techniques and Mastery",
"level": 0,
"paragraphs": 1,
},
{
"name": "3.1 Strategies for Effective Procrastination",
"level": 1,
"paragraphs": 2,
},
{
"name": '3.1.1 The 10-Minute "Break" That Lasts an Hour',
"level": 2,
"paragraphs": 1,
},
{
"name": "3.1.2 Productive Procrastination: Cleaning When You Should be Working",
"level": 2,
"paragraphs": 2,
},
{"name": "3.2 Advanced Delaying Techniques", "level": 1, "paragraphs": 1},
{
"name": '3.2.1 The "I\'ll Start After Lunch" Maneuver',
"level": 2,
"paragraphs": 2,
},
{"name": "3.2.2 The Art of Endless List-Making", "level": 2, "paragraphs": 3},
{"name": "3.2.3 The Social Media Rabbit Hole", "level": 2, "paragraphs": 5},
{
"name": "Chapter 4: Consequences of Procrastination (That We'll Worry About Later)",
"level": 0,
"paragraphs": 1,
},
{
"name": "4.1 What Happens When You Put Things Off",
"level": 1,
"paragraphs": 2,
},
{"name": "4.1.1 Stress Levels and Sudden Panics", "level": 2, "paragraphs": 3},
{
"name": "4.1.2 Creative Excuses for Deadline Extensions",
"level": 2,
"paragraphs": 4,
},
{
"name": "4.2 Real-World Examples of Procrastination Fails",
"level": 1,
"paragraphs": 3,
},
{"name": "4.2.1 The Last-Minute Report Disaster", "level": 2, "paragraphs": 4},
{"name": "4.2.2 The All-Nighter Presentation", "level": 2, "paragraphs": 3},
{
"name": "Chapter 5: Embracing Procrastination as a Lifestyle",
"level": 0,
"paragraphs": 1,
},
{"name": "5.1 Procrastination as a Philosophy", "level": 1, "paragraphs": 2},
{"name": "5.1.1 The Zen of Doing It Tomorrow", "level": 2, "paragraphs": 2},
{"name": "5.1.2 Practicing Mindful Delay", "level": 2, "paragraphs": 3},
{
"name": (
"5.1.3 An Incredibly Long and Detailed Guide to Procrastination Tools and Techniques "
"that Every Expert Procrastinator Should Know, Including but Not Limited to Avoidance Strategies, "
"Deadline Extensions, and Perfecting the Art of Doing Absolutely Nothing While Looking Busy"
),
"level": 2,
"paragraphs": 5,
},
{"name": "5.2 The Future of Procrastination", "level": 1, "paragraphs": 2},
{"name": "5.2.1 Delaying with AI Assistance", "level": 2, "paragraphs": 3},
{
"name": "5.2.2 The Rise of Procrastination Apps and Tools",
"level": 2,
"paragraphs": 2,
},
]
def footer():
if pdf.page == 1:
return
pdf.set_y(pdf.h - 10)
pdf.cell(text=pdf.get_page_label(), center=True)
pdf = FPDF()
pdf.footer = footer
pdf.add_page()
pdf.set_font("helvetica", "", 60)
pdf.cell(w=pdf.epw, text="TITLE", align="C")
pdf.set_font("helvetica", "", 12)
pdf.set_section_title_styles(
level0=TextStyle(
font_family="helvetica",
font_style="B",
font_size_pt=16,
color="#00316e",
b_margin=10,
),
level1=TextStyle(
font_family="helvetica",
font_style="B",
font_size_pt=14,
t_margin=2.5,
b_margin=5,
),
level2=TextStyle(
font_family="helvetica",
font_style="I",
font_size_pt=14,
t_margin=2.5,
b_margin=5,
),
)
if test_number == 0:
# Test with a standard table of contents
pdf.add_page(label_style="r")
pdf.start_section("Table of Contents 1", level=0)
toc = TableOfContents()
pdf.insert_toc_placeholder(toc.render_toc, allow_extra_pages=True)
if test_number == 1:
# Test with a ToC that has a different style using a ttf font
pdf.add_page(label_style="R")
pdf.start_section("Table of Contents 2", level=0)
pdf.multi_cell(
w=pdf.epw,
text="This is a second table of contents where a custom font is used for the ToC only.",
new_x="lmargin",
new_y="next",
)
pdf.ln()
pdf.multi_cell(
w=pdf.epw,
text="Also adding extra lines to test the ToC starting renderening from the position the function is invoked.",
new_x="lmargin",
new_y="next",
)
pdf.ln()
pdf.ln()
pdf.add_font(
family="Quicksand",
fname=HERE.parent / "fonts" / "Quicksand-Regular.otf",
)
toc = TableOfContents()
toc.text_style = TextStyle(font_family="Quicksand", font_size_pt=14)
pdf.insert_toc_placeholder(toc.render_toc, allow_extra_pages=True)
if test_number == 2:
# Test using the section styles on the ToC
pdf.add_page(label_style="a")
pdf.start_section("Table of Contents 3", level=0)
pdf.multi_cell(
w=pdf.epw,
text="This is a third table of contents using the same style as the sections.",
new_x="lmargin",
new_y="next",
)
pdf.ln()
pdf.ln()
toc = TableOfContents(use_section_title_styles=True)
pdf.insert_toc_placeholder(toc.render_toc, allow_extra_pages=True)
pdf.set_page_label(label_style="D")
for index, content in enumerate(file_content):
if content["level"] == 0 and index > 0:
pdf.add_page()
pdf.start_section(content["name"], level=content["level"])
for _ in range(content["paragraphs"]):
pdf.multi_cell(w=pdf.epw, text=LOREM_IPSUM, new_x="lmargin", new_y="next")
pdf.ln()
assert_pdf_equal(pdf, HERE / f"toc_with_extra_page_{test_number}.pdf", tmp_path)
def test_page_label():
pdf = FPDF()
# Test with no label style, prefix, or starting number (default behavior)
pdf.add_page(label_style=None, label_prefix=None, label_start=None)
assert pdf.get_page_label() == "1"
pdf.add_page()
assert pdf.get_page_label() == "2"
# Test decimal numerals with no prefix and starting from 1
pdf.add_page(label_style="D", label_prefix=None, label_start=1)
assert pdf.get_page_label() == "1"
pdf.add_page()
assert pdf.get_page_label() == "2"
# Test decimal numerals with a prefix and starting from 67
pdf.add_page(label_style="D", label_prefix="A-", label_start=67)
assert pdf.get_page_label() == "A-67"
pdf.add_page()
assert pdf.get_page_label() == "A-68"
# Test lowercase Roman numerals starting from 1
pdf.add_page(label_style="r", label_prefix=None, label_start=1)
assert pdf.get_page_label() == "i"
pdf.add_page()
assert pdf.get_page_label() == "ii"
# Test uppercase Roman numerals starting from 10 with prefix
pdf.add_page(label_style="R", label_prefix="Preface-", label_start=10)
assert pdf.get_page_label() == "Preface-X"
pdf.add_page()
assert pdf.get_page_label() == "Preface-XI"
# Test uppercase letters starting from 1
pdf.add_page(label_style="A", label_prefix=None, label_start=1)
assert pdf.get_page_label() == "A"
pdf.add_page()
assert pdf.get_page_label() == "B"
pdf.add_page()
assert pdf.get_page_label() == "C"
# Test uppercase letters with a prefix and starting beyond Z
pdf.add_page(label_style="A", label_prefix="Appendix-", label_start=26)
assert pdf.get_page_label() == "Appendix-Z"
pdf.add_page()
assert pdf.get_page_label() == "Appendix-AA"
pdf.add_page()
assert pdf.get_page_label() == "Appendix-AB"
# Test lowercase letters starting from 1
pdf.add_page(label_style="a", label_prefix=None, label_start=1)
assert pdf.get_page_label() == "a"
pdf.add_page()
assert pdf.get_page_label() == "b"
pdf.add_page()
assert pdf.get_page_label() == "c"
# Test lowercase letters with prefix and starting beyond z
pdf.add_page(label_style="a", label_prefix="Sec-", label_start=26)
assert pdf.get_page_label() == "Sec-z"
pdf.add_page()
assert pdf.get_page_label() == "Sec-aa"
pdf.add_page()
assert pdf.get_page_label() == "Sec-ab"
def test_toc_no_reset_page_indices(tmp_path): # cf. issue 1343
class CustomFPDF(FPDF):
def footer(self):
self.set_y(-15)
# The page number inserted below is incorrect there :(
p(pdf, "Page %s of {nb}" % self.get_page_label(), align="C")
pdf = CustomFPDF()
pdf.set_font("Helvetica", size=12)
pdf.set_section_title_styles(TextStyle()) # Use default style
pdf.add_page()
pdf.set_page_label(label_style="R")
p(pdf, "**Document title**", align="C", markdown=True)
pdf.add_page()
pdf.start_section("Table of Content")
pdf.insert_toc_placeholder(
TableOfContents().render_toc, allow_extra_pages=True, reset_page_indices=False
)
pdf.set_page_label(label_style="D")
assert pdf.pages_count == 3
page_label = pdf.pages[pdf.page].get_page_label()
assert page_label.get_start() == 3
for i in range(1, 100):
if i > 1:
pdf.add_page()
pdf.start_section(f"Section {i}")
assert_pdf_equal(pdf, HERE / "toc_no_reset_page_indices.pdf", tmp_path)
def test_footer_leaking_style_on_toc(tmp_path):
class CustomFPDF(FPDF):
def footer(self):
self.set_y(-15)
self.set_font("Helvetica", size=8)
self.cell(w=0, text="FOOTER", align="C")
pdf = CustomFPDF()
pdf.set_font("Helvetica", size=16)
pdf.set_section_title_styles(TextStyle()) # Use default style
pdf.add_page()
pdf.insert_toc_placeholder(TableOfContents().render_toc, allow_extra_pages=True)
for i in range(1, 100):
if i > 1:
pdf.add_page()
pdf.start_section(f"Section {i}")
assert_pdf_equal(pdf, HERE / "footer_leaking_style_on_toc.pdf", tmp_path)
|