2

我使用 Wea​​syprint 将我的 HTML 文件呈现为 PDF。但是,图像未显示。我尝试了针对此处发布的类似问题的解决方案并将我的代码重写为:

response = HttpResponse(content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename=%s.pdf' % emp_id
pdf_doc = HTML(string=render_output,base_url=request.build_absolute_uri()).render()
pdf_doc.write_pdf(response)
return response

我在我的模板中定义了 img 标签,如下所示:

<img class="logo" src="static/app/nineleaps.png" alt="Not Found">

PDF 不显示图像,而是显示“未找到”。有针对这个的解决方法吗?其他 html 到 pdf 转换器不会像这样准确地呈现我的模板,所以我更喜欢只使用 Wea​​syPrint 的东西。

4

1 回答 1

3

你可能希望base_url这样

request.build_absolute_uri('/')

– 如果你省略了位置,你会得到当前请求的绝对路径,这可能不是你可以附加/static/app/nineleaps.png的。

如果失败,您应该能够查看您runserver的控制台输出。Weasyprint 尝试发出的请求(以及哪些 404)应该在那里可见......

于 2018-07-11T14:25:11.110 回答