Reputation: 1
I have a python google cloud function that should create a pdf when triggered, but the code seems to breakdown, seemingly because of the font, see below.
I tested the workflow with my local installation of fpdf2 and everything worked fine, but it seems to be breaking up when I run it on the GC console.
The fonts are stored in a github repo.
Code below -
class PDF(FPDF):
import logging
....
def header(self):
..........
self.add_font('Spectral', '', fname = repo.get_contents("Style/Spectral/spectrallight.php").decoded_content)
self.add_font('Spectral', 'B', fname = repo.get_contents("Style/Spectral/spectralb.php").decoded_content)
self.add_font('SpectralB', 'B', fname = repo.get_contents("Style/Spectral/spectralb.php").decoded_content)
self.add_font('Spectral', 'BI', fname = repo.get_contents("Style/Spectral/spectralbi.php").decoded_content)
self.add_font('Spectral', 'I', fname = repo.get_contents("Style/Spectral/spectrali.php").decoded_content)
.....
Here's the error I'm getting -
.......File "/workspace/main.py", line 378, in generation
pdf.add_page()
File "/layers/google.python.pip/pip/lib/python3.11/site-packages/fpdf/fpdf.py", line 904, in add_page
self.header()
File "/workspace/main.py", line 217, in header
self.add_font('Spectral', '', fname = repo.get_contents("Style/Spectral/spectrallight.php").decoded_content, uni = False)
File "/layers/google.python.pip/pip/lib/python3.11/site-packages/fpdf/fpdf.py", line 1810, in add_font
raise ValueError(
ValueError: Unsupported font file extension: . add_font() used to accept .pkl file as input, but for security reasons this feature is deprecated since v2.5.1 and has been removed in v2.5.3
At first I used .ttf fonts from Google Fonts, I've also tried actually using plk fonts, now .php, but I keep get the same error.
I've also tried parsing the fonts with BytesIO, tried changing my fpfd2 to 2.5.1 (the function wouldn't deploy"...and a whole host of other fixes...
#======================================
EDIT:
I found the cause of the error; FPDF2 add_font definition
The add_font module checks if the string name (on the file path) contacts .ttf, which doesn't work if using requests or BytesIO to retrieve and parse the font.
This isn't an issue when testing on a local machine, but I need a workaround for a cloud function.
def add_font(self, family=None, style="", fname=None, uni="DEPRECATED"):
"""
Imports a TrueType or OpenType font and makes it available
for later calls to the `FPDF.set_font()` method.
You will find more information on the "Unicode" documentation page.
Args:
family (str): optional name of the font family. Used as a reference for `FPDF.set_font()`.
If not provided, use the base name of the `fname` font path, without extension.
style (str): font style. "B" for bold, "I" for italic.
fname (str): font file name. You can specify a relative or full path.
If the file is not found, it will be searched in `FPDF_FONT_DIR`.
uni (bool): [**DEPRECATED since 2.5.1**] unused
"""
if not fname:
raise ValueError('"fname" parameter is required')
ext = splitext(str(fname))[1].lower()
if ext not in (".otf", ".otc", ".ttf", ".ttc"):
raise ValueError(
f"Unsupported font file extension: {ext}."
" add_font() used to accept .pkl file as input, but for security reasons"
" this feature is deprecated since v2.5.1 and has been removed in v2.5.3."
)
Upvotes: 0
Views: 124