0

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()
2
  • Using PyMuPDF for non-Latin text in this way is not the best option, because here only the non-ASCII part of the character codes <= 256 are influenced by the encodings option. Full Cyrillic support must use a font that contains more than just 26 characters - see my example below (give me 5 minutes please). Commented Nov 29, 2023 at 12:49
  • (Tangentially, code you put in if __name__ == '__main__': should be trivial; the purpose of this boilerplate is to allow you to import the code, which you will not want to do anyway if the logic you need is not available via import. See also stackoverflow.com/a/69778466/874188) Commented Nov 29, 2023 at 13:35

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

1 Comment

We have updated the documentation in this way - and no longer recommend non-embedded fonts where an extended Latin character (non-ASCII) set is required.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.