Google App Engine, ReportLab and True Type Fonts
All! I'm writing an application that would run on Google App Engine. I'm using ReportLab for PDF files generation. However, the application needs to be able to generate PDF with cyrilic text. So I need to l开发者_如何学运维oad some True Type Font. I should write something like this
pdfmetrics.registerFont(TTFont('Verdana', 'verdana.ttf'))
I tried to copy 'verdana.ttf' file to /reportlab/fonts
but registerFont
failed again. However in desktop environment it works fine.
How can I load True Type Font from application running on GAE?
Thanks in advance
if you use docker container to do this job, or any linux environment just use
pdfmetrics.registerFont(TTFont('Verdana', 'Vera.ttf'))
or
pdfmetrics.registerFont(TTFont('Verdana-Bold', 'VeraBd.ttf'))
instead of
pdfmetrics.registerFont(TTFont('Verdana', 'verdana.ttf'))
it worked for me.
According to the documentation:
In Marius' original patch the filename was supposed to be exactly correct, but we have modified things so that if the filename is relative then a search for the corresponding file is done in the current directory and then in directories specified by reportlab.rl_config.TTFSearchpath!
Since when you upload you won't be able to modify /reportlab/fonts
, what you need to do is just upload it and then specify the location relative to the current working directory (should be the same directory as your Python code resides in, but if that doesn't work you might need to check the GAE documentation on where you can place support files like a font on the server).
But maybe I misunderstand and that's what you tried to do and failed?
The answer below is good but does not work on Google App Engine i don't know why! Here is a simple example based on the app directory for flask developers:
~/myapp
|-- mymodule.py
|-- myfont.ttf
So your code will be:
font = r"myfont.ttf"
pdfmetrics.registerFont(TTFont("myfont", font))
精彩评论