0

i am trying to insert text in pdf using pre used fonts in pdf.

$ import fitz
$ doc = fitz.open('input.pdf')
$ page = doc[0]

$ doc.extract_font()
->  ('invalid-name', '', '', b'') 

$ doc.get_page_fonts(0)

-> [(6, 'ttf', 'Type0', 'AHRFAF+Merriweather-Bold', 'F4', 'Identity-H'),
 (11, 'ttf', 'Type0', 'ACGAMA+OpenSans-Bold', 'F5', 'Identity-H'),
 (16, 'ttf', 'Type0', 'AHEYPR+OpenSans-Regular', 'F6', 'Identity-H'),
 (21, 'ttf', 'Type0', 'AXOPEO+Merriweather-Regular', 'F7', 'Identity-H')]

How i can use 'AHRFAF+Merriweather-Bold' font to insert text in pdf. When i try to insert

$ page.insert_text((50, 127), "SINGH", fontname="'AHRFAF+Merriweather-Bold'", fontsize=36, color=color)

-> RuntimeError: FT_New_Memory_Face((null)): unknown file format

Is there any way to insert text using pre used fonts in pdfs.

  • 1
    As just answered in the [Discord](https://discord.com/invite/TSpYGBW4eq) channel: The font reference name must be used as parameter, in this case `fontname="F4"`. Because this is a subset font in this case, text output may show gaps if unsupported characters are used. – Jorj McKie Jun 20 '23 at 20:50
  • You can simply use fontname "F4" if your characters are contained in the font subset. No need to extract it. As an aside: If you want to extract a font, you must use its xref (6 in this case). – Jorj McKie Jun 20 '23 at 20:54

0 Answers0