borb-examples
borb-examples copied to clipboard
Cyrillic font in PDF
I am trying to create a PDF with Cyrillic characters.
First with the build-in font:
d = Document()
page1 = Page()
doc.add_page(page1)
layout = SingleColumnLayout(page1)
layout.add(Paragraph('ААА ббб ВВВ ггг ДДД', font='Helvetica', font_size=11))
Produces the following error:
File "...\borb\pdf\canvas\layout\text\chunk_of_text.py", line 244, in _write_text_bytes_in_hex
assert cid is not None, "Font %s can not represent '%s'" % (
^^^^^^^^^^^^^^^
AssertionError: Font Helvetica can not represent 'А'
Looking into the source, I realize that the unicode_to_character_identifier
function cannot map a Cyrillic (Unicode) character to its glyph.
Then I try a separate TTF font, which does support Cyrillics (eurpope.ttf
):
from pathlib import Path
from borb.pdf.canvas.font.simple_font.true_type_font import TrueTypeFont
font_file = STATIC_FOLDER / 'eurpope.ttf'
assert font_file.exists()
MYFONT = TrueTypeFont.true_type_font_from_file(font_file)
# ...
layout.add(Paragraph('ААА ббб ВВВ ггг ДДД', font=MYFONT, font_size=11))
This time no error is produced, but the resulting PDF contains some scrambled characters instead of "ААА ббб ВВВ ггг ДДД":