为什么 pfpd.add_font() 中没有加载字体?
Why is the font not loading in pfpd.add_font()?
我正在尝试将 pfpdf 库中的字体添加到生成的 pdf 文件,但在将 DejaVuSans.ttf 移动到 "data" 目录后,无法再找到字体。
这是一个简化的代码:
from fpdf import FPDF
import os
pdf = FPDF()
if os.path.isfile("./data/DejaVuSans.ttf"):
print("oof")
if os.path.isfile(os.path.join(os.getcwd(), "data", "DejaVuSans.ttf")):
print("foo")
print(os.path.join(os.getcwd(), "data", "DejaVuSans.ttf"))
goal_dir = os.path.join(os.getcwd(), "data/DejaVuSans.ttf")
pdf.add_font("DejaVuSans", "", "DejaVuSans.ttf", uni=True) # working if .ttf is in the same directory
#pdf.add_font("DejaVuSans", "", "./data/DejaVuSans.ttf" , uni=True) # not working
#pdf.add_font("DejaVuSans", "", os.path.join(os.getcwd(), "data", "DejaVuSans.ttf"), uni = True) # not working
#pdf.add_font("DejaVuSans", "", r"c:\Users\ciszk\Documents\ECP\data\DejaVuSans.ttf", uni = True) # not working
#pdf.add_font("DejaVuSans", "", os.path.abspath(goal_dir), uni = True) # not working
pdf.set_font("DejaVuSans", size=11)
pdf.add_page()
pdf.cell(0, 10, 'Hello', 0, 1, 'C')
pdf.output('oopsie.pdf')
如您所见,我尝试了多种方法,但其中 none 似乎有效。
这就是控制台 returns:
oof
foo
Traceback (most recent call last):
File "c:\Users\ciszk\Documents\ECP\tempCodeRunnerFile.py", line 25, in
pdf.output('oopsie.pdf')
File "C:\Users\ciszk\AppData\Local\Programs\Python\Python37-32\lib\site-packages\fpdf\fpdf.py", line 1065, in output
self.close()
File "C:\Users\ciszk\AppData\Local\Programs\Python\Python37-32\lib\site-packages\fpdf\fpdf.py", line 246, in close
self._enddoc()
File "C:\Users\ciszk\AppData\Local\Programs\Python\Python37-32\lib\site-packages\fpdf\fpdf.py", line 1637, in _enddoc
self._putresources()
File "C:\Users\ciszk\AppData\Local\Programs\Python\Python37-32\lib\site-packages\fpdf\fpdf.py", line 1584, in _putresources
self._putfonts()
File "C:\Users\ciszk\AppData\Local\Programs\Python\Python37-32\lib\site-packages\fpdf\fpdf.py", line 1288, in _putfonts
ttfontstream = ttf.makeSubset(font['ttffile'], subset)
File "C:\Users\ciszk\AppData\Local\Programs\Python\Python37-32\lib\site-packages\fpdf\ttfonts.py", line 459, in makeSubset
self.fh = open(file ,'rb')
FileNotFoundError: [Errno 2] No such file or directory: 'DejaVuSans.ttf'
如果数据目录没有 DejaVuSans.ttf,则会引发错误:
Traceback (most recent call last):
File "c:\Users\ciszk\Documents\ECP\tempCodeRunnerFile.py", line 18, in
pdf.add_font("DejaVuSans", "", os.path.abspath(goal_dir), uni = True) # not working
File "C:\Users\ciszk\AppData\Local\Programs\Python\Python37-32\lib\site-packages\fpdf\fpdf.py", line 469, in add_font
raise RuntimeError("TTF Font file not found: %s" % fname)
RuntimeError: TTF Font file not found: c:\Users\ciszk\Documents\ECP\data\DejaVuSans.ttf
我是不是遗漏了一些非常明显的东西?怎样才能成功添加字体?
您必须删除您要使用的字体目录中扩展名为.pkl 的文件。
这些文件必须在您的脚本 运行 时创建,因为它们包含您本地字体的所有配置(路径、前缀...)。
我认为您的字体配置太旧了,这就是它不起作用的原因。
我正在尝试将 pfpdf 库中的字体添加到生成的 pdf 文件,但在将 DejaVuSans.ttf 移动到 "data" 目录后,无法再找到字体。
这是一个简化的代码:
from fpdf import FPDF
import os
pdf = FPDF()
if os.path.isfile("./data/DejaVuSans.ttf"):
print("oof")
if os.path.isfile(os.path.join(os.getcwd(), "data", "DejaVuSans.ttf")):
print("foo")
print(os.path.join(os.getcwd(), "data", "DejaVuSans.ttf"))
goal_dir = os.path.join(os.getcwd(), "data/DejaVuSans.ttf")
pdf.add_font("DejaVuSans", "", "DejaVuSans.ttf", uni=True) # working if .ttf is in the same directory
#pdf.add_font("DejaVuSans", "", "./data/DejaVuSans.ttf" , uni=True) # not working
#pdf.add_font("DejaVuSans", "", os.path.join(os.getcwd(), "data", "DejaVuSans.ttf"), uni = True) # not working
#pdf.add_font("DejaVuSans", "", r"c:\Users\ciszk\Documents\ECP\data\DejaVuSans.ttf", uni = True) # not working
#pdf.add_font("DejaVuSans", "", os.path.abspath(goal_dir), uni = True) # not working
pdf.set_font("DejaVuSans", size=11)
pdf.add_page()
pdf.cell(0, 10, 'Hello', 0, 1, 'C')
pdf.output('oopsie.pdf')
如您所见,我尝试了多种方法,但其中 none 似乎有效。 这就是控制台 returns:
oof foo Traceback (most recent call last): File "c:\Users\ciszk\Documents\ECP\tempCodeRunnerFile.py", line 25, in pdf.output('oopsie.pdf') File "C:\Users\ciszk\AppData\Local\Programs\Python\Python37-32\lib\site-packages\fpdf\fpdf.py", line 1065, in output self.close() File "C:\Users\ciszk\AppData\Local\Programs\Python\Python37-32\lib\site-packages\fpdf\fpdf.py", line 246, in close self._enddoc() File "C:\Users\ciszk\AppData\Local\Programs\Python\Python37-32\lib\site-packages\fpdf\fpdf.py", line 1637, in _enddoc self._putresources() File "C:\Users\ciszk\AppData\Local\Programs\Python\Python37-32\lib\site-packages\fpdf\fpdf.py", line 1584, in _putresources self._putfonts() File "C:\Users\ciszk\AppData\Local\Programs\Python\Python37-32\lib\site-packages\fpdf\fpdf.py", line 1288, in _putfonts ttfontstream = ttf.makeSubset(font['ttffile'], subset) File "C:\Users\ciszk\AppData\Local\Programs\Python\Python37-32\lib\site-packages\fpdf\ttfonts.py", line 459, in makeSubset self.fh = open(file ,'rb') FileNotFoundError: [Errno 2] No such file or directory: 'DejaVuSans.ttf'
如果数据目录没有 DejaVuSans.ttf,则会引发错误:
Traceback (most recent call last): File "c:\Users\ciszk\Documents\ECP\tempCodeRunnerFile.py", line 18, in pdf.add_font("DejaVuSans", "", os.path.abspath(goal_dir), uni = True) # not working File "C:\Users\ciszk\AppData\Local\Programs\Python\Python37-32\lib\site-packages\fpdf\fpdf.py", line 469, in add_font raise RuntimeError("TTF Font file not found: %s" % fname) RuntimeError: TTF Font file not found: c:\Users\ciszk\Documents\ECP\data\DejaVuSans.ttf
我是不是遗漏了一些非常明显的东西?怎样才能成功添加字体?
您必须删除您要使用的字体目录中扩展名为.pkl 的文件。
这些文件必须在您的脚本 运行 时创建,因为它们包含您本地字体的所有配置(路径、前缀...)。
我认为您的字体配置太旧了,这就是它不起作用的原因。