0

我有一个 txt 文件,其中包含一个带刻度的列表

Done List
☑ grocery
☑ electronics

上面的列表是使用 Python 创建的,如下所示:

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

我想使用FPDFpython 库将 txt 文件转换为 PDF。我使用以下命令保存

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

并得到以下错误:

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

根据https://pyfpdf.readthedocs.io/en/latest/Unicode/index.html它应该可以工作(?)我在这里错过了什么?

4

1 回答 1

0

是否可以在编写之前将编码设置为pdf字体? 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 是计算机本地字体的路径。

我下载了文档推荐的 unicode 字体集,然后将它们放在所需的“fonts”目录中,包括文档说它将查找的“unifont”子目录。

于 2021-08-25T04:39:21.660 回答