Unable to integrate custom fonts and bold text when inserting text into PDFs using PyMuPDF #4824
-
|
I’m implementing a PDF personalization pipeline that uses PyMuPDF either:
The same issue occurs with bold text: setting the bold flag or using font names such as "helv-bold" or custom font files does not change the rendered output. What I Expected
What Actually Happens
Code Sample
Do let me know if any more details are needed. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
|
This is not an issue but a Discussions item: transferring ... |
Beta Was this translation helpful? Give feedback.
-
|
Using your own font requires specifying the name of the file or the its content in memory (font buffer). This must happen per page - not per PDF. Second you must specify the Best use this approach: font = pymupdf.Font(fontfile="path to font")
fontname = "mycoolfont" # this is NOT the font filename!
# once per page:
page.insert_font(fontname=fontname, fontbuffer=font.buffer)
# insert text with the font:
page.insert_textbox(..., fontname=fontname)Notes:
|
Beta Was this translation helpful? Give feedback.
Method
insert_htmlbox()works much likeinsert_textbox()in that it position the text across the rectangle in the best possible way.In addition however it understand HTML syntax and reacts to tags like
<b>to start bold text. It also detects whether text portions require other than the declared font(s) and automatically install the needed additional font.You can influence this automatism by specifying HTML stylesheet instructions that can contain font information based on
@font-face.The method can output tables given in HTML syntax plus images if the text references them.