-
Notifications
You must be signed in to change notification settings - Fork 269
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
Only include fonts effectively used in the final PDF document #1382
Comments
Hi @Lucas-C , I checked this issue and plan to fix it in this way:
If the plan looks good, could you please assign this issue to me? Thanks. Please let me know if you have more ideas about how to fix this issue! Thanks! |
Hi @yuyiz67! Thank you for volunteering 🙂 👍 I just realized that @andersonhc slightly modified the font-embedding logic at the beginning of the year in PR #1334 So there is no more issue with fonts added with The remaining issue is with fonts inserted initially by There is a minimal script reproducing the case, using Noto Sans (the from pathlib import Path
from fpdf import FPDF
DIR = Path(__file__).parent
FONT_DIR = DIR / "test" / "fonts" # put the NotoSans .ttf font files in this directory
pdf = FPDF()
pdf.add_font("NotoSans", fname=FONT_DIR / "NotoSans-Regular.ttf") # will be /F1
pdf.add_font("NotoSans", style="B", fname=FONT_DIR / "NotoSans-Bold.ttf") # will be /F2
pdf.add_font("NotoSans", style="I", fname=FONT_DIR / "NotoSans-Italic.ttf") # will be /F3
pdf.add_font("NotoSans", style="BI", fname=FONT_DIR / "NotoSans-BoldItalic.ttf") # OK/fixed, will NOT be inserted in the PDF
pdf.set_font("NotoSans", size=12)
pdf.add_page() # currently inserts /F1 on the page
pdf.multi_cell(w=pdf.epw, text="**Text in bold**", markdown=True) # font effectively used is /F2 = NotoSans-Bold
pdf.add_page() # currently inserts /F1 on the page
pdf.multi_cell(w=pdf.epw, text="__Text in italic__", markdown=True) # font effectively used is /F3 = NotoSans-Italic
pdf.add_page() # currently inserts /F1 on the page
pdf.multi_cell(w=pdf.epw, text="Regular text\n**Text in bold**\n__Text in italic__", markdown=True) # all 3 fonts used
pdf.output("issue_1382.pdf") When inspecting the resulting PDF (
The problem is with It comes from:
The same logic (
We also have 5 defensive checks like this one in Given that Are my explanations clear enough @yuyiz67? Also, note that this change will cause many reference PDF to be modified, |
Hi @Lucas-C |
Quoting @andersonhc in #1133 (comment):
fpdf2
should NOT include unused fonts.We should implement a mechanism to only add to the final PDF document the fonts actually used.
cf. comment #1382 (comment) below for mor details.
The text was updated successfully, but these errors were encountered: