I am having trouble getting weasyprint to use custom fonts for PDF creation. I think this is related to the interaction between weasyprint's base_url parameter and my static files, but I'm struggling to resolve it.
When generating the PDF, I get the following error:
fonts/add_font_face - Font-face 'Playfair Display' cannot be loaded
My font files are within static/fonts in my "gallery" app:
- project
- manage.py
- ...
- gallery
- static
- files
- views.py
- url.py
- ...
I'm creating my pdf using the following command:
base_url = request.build_absolute_uri("/")
HTML(string=template, base_url=base_url).write_pdf(output)
My template uses a <style> block with the following font-face definitions, ideally using Django's static tag:
@font-face {
font-family: 'Playfair Display';
font-weight: normal;
src: url({% static 'fonts/PlayfairDisplay-Regular.ttf' %}) format("truetype");
}
An suggestions on how to resolve this?