Custom fonts "cannot be loaded" when rendering PDF with weasyprint in Django app

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?

I turned on "debug" logging for weasyprint, and saw that fonts were not being loaded because of the following SSL error:

URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate (_ssl.c:997)>

I had previously enabled SSL for this project, but it's no longer needed. Disabling all of the project's SSL solved this issue.

The following will now load fonts correctly:

@font-face {
  font-family: 'Playfair Display';
  font-weight: normal;
  src: url({% static 'fonts/PlayfairDisplay-Regular.ttf' %}) format("truetype");
}
Back to Top