本文介绍了Django-Weasyprint 图像未呈现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have used Weasyprint to render my HTML file into a PDF. However,the image is not being displayed. I tried the solution for a similar problem posted here and rewrote my code as so:

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 标签,如下所示:

I have define the img tag in my template like below:

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

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

The PDF does not display the image and displays "Not Found" instead of it.Is there a solution to this? Other html to pdf converters don't render my template as accurately as this, so I would prefer something that uses WeasyPrint only.

推荐答案

您可能希望 base_url 成为

request.build_absolute_uri('/')

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

– if you elide the location, you get the current request's absolute path, which is probably not something you can append /static/app/nineleaps.png to.

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

If that fails, you should be able to look at your runserver's console output. The request Weasyprint attempts to make (and which 404s) should be visible there...

这篇关于Django-Weasyprint 图像未呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-05 01:20