Home>

An empty character appears instead of Russian letters and some symbols.

TXT content:

Test 1234567890
! " № ; % : ? * ( ) _ +
=-&
 ^ % $ # @ . ,
| \ /[ ] { }
; : '" < > `

Code:

from fpdf import FPDF
pdf= FPDF()
pdf.add_page()
pdf.add_font('Segoe UI Bold', '', 'Segoe UI Bold.ttf', uni=True)
pdf.set_font("Segoe UI Bold", size= 15)
with open('text.txt', 'r') as file:
    for info in file:
        pdf.cell(200, 10, txt=info, ln=1, align='C')
pdf.output("text.pdf") 

Possible duplicate of the question: How to display Cyrillic in a document in FPDF?

GrAnd2022-02-14 09:38:34

In fact, the problem is in the fonts. Below is the answer

tomato-magnet-regulato2022-02-14 09:44:47
  • Answer # 1
    #!/usr/bin/env python
    # -*-coding: utf8 -*-
    from fpdf import FPDF
    pdf= FPDF()
    pdf.add_page()
    # Add a DejaVu Unicode font (uses UTF-8)
    # Supports more than 200 languages. For a coverage status see:
    # http://dejavu.svn.sourceforge.net/viewvc/dejavu/trunk/dejavu-fonts/langcover.txt
    pdf.add_font('DejaVu', '', 'DejaVuSansCondensed.ttf', uni=True)
    pdf.set_font('DejaVu', '', 14)
    text= u"""
    English: Hello World
    Greek: Γειά σου κόσμος
    Polish: Witaj Swiecie
    Portuguese: Ola mundo
    English: Hello World
    Vietnamese: Xin chao thế giới
    Arabic: مرحبا العالم
    Hebrew: שלום עולם
    """
    for txt in text.split('\n'):
        pdf.write(8, txt)
        pdf.ln(8)
    # Add a Indic Unicode font (uses UTF-8)
    # Supports: Bengali, Devanagari, Gujarati,
    # Gurmukhi (including the variants for Punjabi)
    # Kannada, Malayalam, Oriya, Tamil, Telugu, Tibetan
    pdf.add_font('gargi', '', 'gargi.ttf', uni=True)
    pdf.set_font('gargi', '', 14)
    pdf.write(8, u'Hindi: नमस्ते दुनिया')
    pdf.ln(20)
    # Add a AR PL New Sung Unicode font (uses UTF-8)
    # The Open Source Chinese Font (also supports other east Asian languages)
    pdf.add_font('fireflysung', '', 'fireflysung.ttf', uni=True)
    pdf.set_font('fireflysung', '', 14)
    pdf.write(8, u'Chinese: 你好世界\n')
    pdf.write(8, u'Japanese: こんにちは世界\n')
    pdf.ln(10)
    # Add a Alee Unicode font (uses UTF-8)
    # General purpose Hangul truetype fonts that contain Korean syllable
    # and Latin9 (iso8859-15) characters.
    pdf.add_font('eunjin', '', 'Eunjin.ttf', uni=True)
    pdf.set_font('eunjin', '', 14)
    pdf.write(8, u'Korean: 안녕하세요')
    pdf.ln(20)
    # Add a Fonts-TLWG (formerly ThaiFonts-Scalable) (uses UTF-8)
    pdf.add_font('waree', '', 'Waree.ttf', uni=True)
    pdf.set_font('waree', '', 14)
    pdf.write(8, u'Thai: สวัสดีชาวโลก')
    pdf.ln(20)
    #Select a standard font (uses windows-1252)
    pdf.set_font('Arial', '', 14)
    pdf.ln(10)
    pdf.write(5, 'This is standard built-in font')
    pdf.output("unicode.pdf", 'F')
    

    A source

    And explain the code with text -what is the essence of the code, why does it work?

    CrazyElf2022-02-14 10:04:27

    I kind of shared a link to the source, everything is clearly explained there.

    tomato-magnet-regulato2022-02-14 10:14:29

    Read the rules. Links (especially if they point to something other than SO) can go bad. The link may contain more complete information, but at least briefly you need to explain the code.

    CrazyElf2022-02-14 10:22:47

    I will definitely read the rules ... thanks for your attention. But my time, it is only mine, if I consider it necessary to chew information to the user, I do it, and when the info is in a minute, I think this answer is enough

    tomato-magnet-regulato2022-02-14 10:24:30