简体   繁体   English

如何将新的 fonts 添加到 pyfpdf

[英]how to add new fonts to pyfpdf

i've tried to copy a dejavucondensed.ttf into pyfpdf/font/ But no luck when i tried我试图将 dejavucondensed.ttf 复制到 pyfpdf/font/ 但是当我尝试时没有运气

>>> from pyfpdf import *
>>> p = FPDF()
>>> p.add_font('dejavucondensed')
SyntaxError: Non-ASCII character '\xfd' in file pyfpdf/font/dejavucondensed.font 
on line 2, 
but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

This is probably a Unicode/encoding problem (see the PEP linked in the message ).这可能是一个 Unicode/编码问题(请参阅消息中链接的 PEP )。 I do not have pypdf installed here, but according to the reference documentation for add_font you need to use the uni parameter:我这里没有安装 pypdf,但是根据add_font 的参考文档,你需要使用uni参数:

pdf.add_font('DejaVu', '', 'DejaVuSansCondensed.ttf', uni=True)

For anyone who's looking at this all this time later, try this.对于以后一直在看这个的人,试试这个。 I'm in Ubuntu Linux using Python 3.8.我在 Ubuntu Linux 使用 Python 3.8。 Go to your installation of fpdf , which for me was in /usr/local/lib/python3.8/dist-packages/fpdf . Go 到您安装的fpdf ,对我来说它在/usr/local/lib/python3.8/dist-packages/fpdf中。

If there is not a folder named fonts there, do a mkdir fonts .如果那里没有名为fonts的文件夹,请执行mkdir fonts Grab the ttf file for your font from any number of sources and place it in the fonts folder.从任意数量的来源获取字体的ttf文件并将其放在fonts文件夹中。 I wanted Calibri Regular and named my file calibri_regular.ttf .我想要 Calibri Regular 并将我的文件命名为calibri_regular.ttf

Then in your interpreter or script:然后在你的解释器或脚本中:

from fpdf import FPDF
pdf = FPDF()

pdf.add_font('Calibri Regular', '', 'calibri_regular.ttf', uni=True)
pdf.set_font('Calibri Regular', '', 12)

At this point you're ready to write to the PDF in that font.此时,您已准备好使用该字体写入 PDF。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM