Hello @HaraldLieder ! I’m trying to use a TTF font file to render some text as follows:
import pymupdf doc = pymupdf.open() n = doc.insert_page(-1) page = doc[0] pontano = pymupdf.Font(fontname="Pontano Sans Regular", fontfile = "PontanoSans.ttf") buffer = pontano.buffer page.insert_font(fontbuffer = buffer) print(pontano) text="Pontano! aQ" annot = page.add_freetext_annot(pymupdf.Rect(5,5,200,100),text,fontsize=40,fontname="Pontano Sans Regular",rotate=0) doc.save("document-with-pontano.pdf")
However it doesn’t seem to use the font ( I can tell this when I look at the “a” and the Q” characters as they are quite distinctive.
I attach the TTF, screen shot of the font characters & my resulting PDF file.
Please let me know what I’m doing wrong here!
document-with-pontano.pdf (1.1 KB)
TTF is here: https://dijipiji.com/fonts/PontanoSans.ttf
Two comments:
When creating a font object font=pymupdf.Font(...)
, the font name is only meaningful when selecting one of the Base-14 fonts by their character “code”, like “helv” or “tiro” or a code available after import pymupdf-fonts (“figo” etc.).
In that case other parameters are ignored.
Choosing fontfile or fontbuffer are alternatives (mutually exclusive).
After a font object has been defined, attribute font.name
contains some built-in name (from the font binary so to say).
So you either should do font=pymupdf.Font("helv")
or font = pymupdf.Font(fontfile="some.ttf")
, respectively fontbuffer.
Having said that, use of a user-selected font is not possible (yet) for defining a freetext annotation. Either use one of the Base-14 fonts or use rich text - in which case MuPDF selects the required font depending on the the characters and styling encountered in the text.
User-selected fonts are supported in insert_text/box
, TextWriter
and insert_htmlbox
yet.
1 Like
Aha - makes sense - many thanks!