pythonpdfcyrillicpymupdf

Python fitz insert russian text in PDF not work


I tried insert russian text in PDF file and I see that, what am I doing wrong?

import fitz

if __name__ == '__main__':
    src_pdf_filename = 'original.pdf'
    dst_pdf_filename = 'destination.pdf'

    document = fitz.open(src_pdf_filename)
    
    ss = u"ToUnicode Привет Hello"
    
    for page in document:    
       
        page.insert_text((50, 250), ss, fontname="Times-Roman", fontsize=50,encoding=fitz.TEXT_ENCODING_CYRILLIC, overlay=False)

    document.save(dst_pdf_filename)

    document.close()

Solution

  • Your way of writing text uses no embedded (file-based) font and is therefore limited.

    import fitz
    doc=fitz.open()
    page=doc.new_page()
    font=fitz.Font("tiro")  # this is a full font file
    page.insert_font(fontname="F0", fontbuffer=font.buffer)
    page.insert_text((50, 250), "ToUnicode Привет Hello", fontname="F0", fontsize=50, overlay=False)
    doc.save("test.pdf")
    

    Gives you this: enter image description here