0

i have a txt file containing a tick-marked list as so

Done List
☑ grocery
☑ electronics

The list above is created using Python like so:

chkbx = u"\u2611"   #u'\U0001F5F9'
for ...
    with open('List.txt','a',encoding='utf-8') as f:
           f.write(f"{chkbx} {activity}\n")

I would like to convert the txt file into PDF using the FPDF library for python. I use the following command to save

pdf.output('list.pdf').encode('utf-8')

and get the following Error:

UnicodeEncodeError: 'latin-1' codec can't encode character '\u2611' in position 516: ordinal not in range(256)

According to https://pyfpdf.readthedocs.io/en/latest/Unicode/index.html it should work (?) What am I missing here?

ttuff
  • 45
  • 5
  • nobody :/ ? Is the question too easy or too hard?? – ttuff Aug 10 '20 at 16:12
  • Hmm, you forgot 1. to give the full stacktrace - it can help to understand exactly where the error is raised and 2. to say what fonts you have installed in PyFDF (and how). – Serge Ballesta Aug 10 '20 at 16:23

1 Answers1

0

Is it possible that you can set the encoding in the pdf font before you write? https://pyfpdf.readthedocs.io/en/latest/Unicode/index.html

pdf.add_page()
pdf.add_font('DejaVu', '',FPDF_FONTPATH+'/DejaVuSansCondensed.ttf', uni=True)
pdf.set_font('DejaVu', '', 12)

FPDF_FONTPATH being the path to your computer's local fonts.

I downloaded the set of unicode fonts that the documentation recommended and then placed them in the desired 'fonts' directory, including the 'unifont' subdirectory, which the documentation says it will look for.

bio_sprite
  • 438
  • 2
  • 14