【发布时间】:2019-03-13 13:17:27
【问题描述】:
我有一个 django 应用程序,我想从我的 django 视图创建一个 pdf。 我使用 weasyprint,由于某种原因它不接受我的自定义字体。 字体 url 正在工作,当我使用相同的字体呈现相同的 html 时,我看到了正确的字体,但我的 pdf 使用错误的字体呈现。我也尝试了base64字体字符串,但没有运气。我的渲染代码是:
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
知道我做错了什么吗?
【问题讨论】:
标签: python django weasyprint