Embed Font in existing PDF

Hi there,

I do have a legacy 3rd party tool, generating a pdf with a lot of graphics and texts.
This tool can not embed fonts.
This Tool somehow removes spaces from font names.

I use “JetBrains Mono” for some texts, and want to embed this font into the pdf
So, I managed to add this font to the pdf using pymupdf.

so output from pdffonts looks like:

name                                 type              encoding         emb sub uni object ID

JetBrainsMono                        TrueType          WinAnsi          no  no  no      50  0
Courier-Bold                         Type 1            WinAnsi          no  no  no      52  0
fstroke3                             TrueType          WinAnsi          no  no  no      53  0
JetBrains Mono Regular               CID Type 0C (OT)  Identity-H       yes no  yes   1739  0

But it seems not every viewer can work with this. Basically I just found chrome browser viewer showing the correct font, but maybe it has JetBrains somwhere with it and is not using the embedded version.
But this gives me the indication that “JetBrainsMono“ without spaces is somehow working as non embedded font.

Unfortunately I can not use rpl_font.py, since there are many hidden text items, the document is not readable then.

So can I embed a font for existing text at all?
At lest I can not embed a font and use an existing name.

Any Idea here?

It seems this is similar topic:

I will give it a try

@arne123 Nice find - hope the tips in there work - please let us know how it goes!

So, I just learned, that white-spaces in PDF names are not allowed.
But “JetBrains Mono” contains white-spaces.
Maybe this should be handled when loading fonts initially (seems the legacy tool knows something here).

So I manually copied the xref and removed white-spaces:


 for key in doc.xref_get_keys(xref_src):
     item = doc.xref_get_key(xref_src, key)
     if key == "BaseFont":
         item = (item[0], item[1].replace(" ", ""))

     doc.xref_set_key(53, key, item[1])
     doc.xref_set_key(50, key, item[1])

So pdffonts looks like this now:

name                                 type              encoding         emb sub uni object ID
------------------------------------ ----------------- ---------------- --- --- --- ---------
JetBrainsMonoRegular                 CID TrueType      Identity-H       yes no  yes     50  0
Courier-Bold                         Type 1            WinAnsi          no  no  no      52  0
JetBrainsMonoRegular                 CID TrueType      Identity-H       yes no  yes     53  0
fstroke3                             TrueType          WinAnsi          no  no  no      55  0
JetBrains Mono Regular               CID TrueType      Identity-H       yes no  yes   1741  0

But all text is gone now in the document, so not yet successful.
I will try an other font next to exclude any name issue.