【发布时间】:2020-04-08 02:15:32
【问题描述】:
我正在使用 Django + WeasyPrint 生成 HTML 到 PDF。
在 HTML 模板中,我有大约 2 个 SVG 文件。在开发(本地)服务器中运行时,它会在将 HTML 转换为 PDF 时呈现 SVG 图像。而在 TEST Server(使用 NGINX 和 Docker Container 运行)中,它无法渲染 SVG 文件。在 WeasePrint 中,我在 base_url 中提到了我的 TEST URL (http//test.sampleproject.com:8000)。我不知道为什么它在我的开发服务器中工作而在测试服务器中不起作用
我的代码。
html_string = render_to_string('sample.html', {'data': data})
result = HTML(string=html_string, base_url='http//test.sampleproject.com:8000').write_pdf(presentational_hints=True)
response = HttpResponse(result, content_type='application/pdf')
response['Content-Disposition'] = 'inline; filename={}'.format('sample.pdf')
return response
在我的模板文件中,我加载了大约 10 张图片。例如:“sample.html”模板文件:
{% load staticfiles %}
<img src="{% static 'svg/Logo1.svg' %}" alt="" />
<img src="{% static 'svg/Logo2.svg' %}" alt="" />
请帮我在输出 PDF 中渲染 SVG 文件。
【问题讨论】:
-
Weasyprint 需要在操作系统级别安装多个库才能运行。这些是否已安装在 TEST 服务器上?听起来您可能在 TEST 服务器上缺少 CairoSVG:weasyprint.readthedocs.io/en/stable/install.html
标签: django python-3.x docker nginx weasyprint