-
Notifications
You must be signed in to change notification settings - Fork 232
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
local_context()
blocks appear to "leak" into one another
#1204
Comments
Even this is not enough to get the footer on page 3 to use 12pt font: def footer(self):
self.set_y(-15)
self.set_font_size(12)
with self.local_context(text_color='#0000ff', font_size=12):
self.set_font_size(12)
self.cell(text=f'Footer {self.page_no()}') |
Hi @smitelli Thank you for the detailed bug report. |
@allcontributors please add @smitelli for bug |
I've put up a pull request to add @smitelli! 🎉 |
This is indeed a bug. In the meantime, for this use case, I suggest that use you use the use_font_face() method instead, which is more reliable for now: from fpdf import FPDF, FontFace
class PDF(FPDF):
def header(self):
with self.use_font_face(FontFace(color="#00ff00", size_pt=12)): # LABEL A
self.cell(text=f"Header {self.page_no()}")
self.ln()
def footer(self):
self.set_y(-15)
with self.use_font_face(FontFace(color="#0000ff", size_pt=12)): # LABEL B
self.cell(text=f"Footer {self.page_no()}")
pdf = PDF()
pdf.set_font(family="helvetica", size=12)
pdf.add_page()
with pdf.use_font_face(FontFace(size_pt=36)): # LABEL C
pdf.multi_cell(w=0, text="\n".join(f"Line {i + 1}" for i in range(21))) |
I opened PR #1207 to fix this bug. @smitelli If you want, you can test this fix by installing
With this fix, this code should now work as expected, with only a single call to from fpdf import FPDF
class PDF(FPDF):
def header(self):
self.cell(text=f"Header {self.page_no()}")
self.ln()
def footer(self):
self.set_y(-15)
self.cell(text=f"Footer {self.page_no()}")
pdf = PDF()
pdf.set_font(family="helvetica", size=12)
pdf.add_page()
with pdf.local_context(font_size_pt=36): # LABEL C
pdf.multi_cell(w=0, text="\n".join(f"Line {i + 1}" for i in range(21)))
pdf.output('testcase.pdf') |
When using
local_context()
in the main document and header/footer methods, the parameters set in the document leak into the header/footer in unexpected ways:The expected output is that all header/footer text is in font size 12. The actual output is that the 36pt font defined at label
C
leaks into thefooter()
function and overrides the 12pt font defined at labelB
-- but this only happens on the last page of the document. There is no obvious way to make the font size atB
take effect consistently without removing theset_context()
atC
.Furthermore, based on my understanding of the
set_context()
philosophy (briefly... any values set explicitly will take effect, unspecified values continue to use whatever the outer environment had, and when the context exits everything is exactly the way is was before the context was entered) I would think I don't need the explicitfont_size=12
at labelsA
orB
at all to use the main document font size. But omitting them causes all headers/footers after the top of page 1 to get the 36pt font fromC
.This is fpdf2 version 2.7.9 on both Python 3.9.16 and 3.12.3.
The text was updated successfully, but these errors were encountered: