Skip to content

Commit

Permalink
Fix insert_toc_placeholder() usage with footer() and {{nb}} - close #548
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas-C committed Sep 20, 2022
1 parent 7397faf commit bbbc258
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ This can also be enabled programmatically with `warnings.simplefilter('default',
- `HTMLMixin` is deprecated, and not needed anymore: the `write_html()` method is now natively available in the `FPDF` class - thanks to @yk-jp
### Fixed
- automatic page break is never performed on an empty page (when the Y position is at the top margin)
- The SVG parser now accepts stroke-width attribute values with an explicit unit, thanks to @gmischler; [#526](https://github.com/PyFPDF/fpdf2/issues/526)
- fixed [`insert_toc_placeholder()`](https://pyfpdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.insert_toc_placeholder) usage with [`footer()`](https://pyfpdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.footer) and `{{nb}}`; [#548](https://github.com/PyFPDF/fpdf2/issues/548)
- the SVG parser now accepts stroke-width attribute values with an explicit unit, thanks to @gmischler; [#526](https://github.com/PyFPDF/fpdf2/issues/526)

## [2.5.7] - 2022-09-08
### Changed
Expand Down
7 changes: 5 additions & 2 deletions fpdf/fpdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -3974,10 +3974,10 @@ def sign(

def _putpages(self):
nb = self.pages_count # total number of pages
if self.str_alias_nb_pages:
self._substitute_page_number()
if self._toc_placeholder:
self._insert_table_of_contents()
if self.str_alias_nb_pages:
self._substitute_page_number()
if self.def_orientation == "P":
dw_pt = self.dw_pt
dh_pt = self.dh_pt
Expand Down Expand Up @@ -4091,6 +4091,9 @@ def _insert_table_of_contents(self):
# Doc has been closed but we want to write to self.pages[self.page] instead of self.buffer:
self.state = DocumentState.GENERATING_PAGE
self.y = tocp.y
# Disabling footer & header, as they have already been called:
self.footer = lambda *args, **kwargs: None
self.header = lambda *args, **kwargs: None
tocp.render_function(self, self._outline)
expected_final_page = tocp.start_page + tocp.pages - 1
if self.page != expected_final_page:
Expand Down
27 changes: 27 additions & 0 deletions test/outline/test_outline.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,33 @@ def test_2_pages_outline(tmp_path):
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(style="", size=12)
for section in outline:
self.ln()
self.cell(txt=section.name)

def footer(self):
self.set_y(-15)
self.set_font("helvetica", "I", 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(txt=f"Section {i}")
pdf.ln()

assert_pdf_equal(pdf, HERE / "toc_with_nb_and_footer.pdf", tmp_path)


def test_russian_heading(tmp_path): # issue-320
pdf = FPDF()
pdf.add_font("Roboto", style="B", fname="test/fonts/Roboto-Regular.ttf")
Expand Down
Binary file added test/outline/toc_with_nb_and_footer.pdf
Binary file not shown.

0 comments on commit bbbc258

Please sign in to comment.