Reputation: 195
I'm trying to add a font in pfpdf library to a generated pdf file but after moving a DejaVuSans.ttf to "data" directory, font cannot longer be found.
Here's a simplified code:
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')
As you can see I tried multiple ways but none of them seems to be working. That's what console 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'
If data directory doesn't have DejaVuSans.ttf, it raises en error:
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
Am I missing something very obvious? What can I do to successufully add a font?
Upvotes: 3
Views: 2520
Reputation: 56
You have to delete the files with the .pkl extension in your Font directory for the front you want to use.
These files must be created when your script running because they contain all the configuration for your local font (path, prefix ...).
I think you have old font configuration and that's why it doesn't work.
Upvotes: 4