I have a django app and I would like to create a pdf from my django view. I use weasyprint, and for some reason it doesn't accept my custom font. The font url is working and when I render the same html with the same font-face, I see the correct font, but my pdf is rendered with a wrong one. I also tried the base64 font string, but no luck. My render code is:
import os
from weasyprint import HTML, CSS
from weasyprint.fonts import FontConfiguration
from django.conf import settings
from django.http import HttpResponse
from django.template.loader import get_template
from django.urls import reverse
def render_to_pdf(template_src, context_dict={}):
font_config = FontConfiguration()
font_string = '''
@font-face {
font-family: 'Titillium Web';
font-style: normal;
font-weight: 300;
src: local('Titillium Web Light'), local('TitilliumWeb-Light'), url('http://X.X.X.X:8000/static/fonts/titillium_web.woff2') format('woff2');
}
*, div {font-family: 'Titillium Web';}
'''
template = get_template(template_src)
rendered_html = template.render(context_dict)
pdf_file = HTML(string=rendered_html).write_pdf(stylesheets=[
CSS(settings.BASE_DIR + '/gui/executive_summary.css'),
CSS(string=font_string)],font_config=font_config)
response = HttpResponse(pdf_file, content_type='application/pdf')
response['Content-Disposition'] = 'filename="report.pdf"'
return response
Any idea what am I doing wrong?
As you can read in documentation:
WeasyPrint should support any font format handled by FreeType (any format widely used except WOFF2)
SRC: http://weasyprint.readthedocs.io/en/latest/features.html#fonts