【问题标题】:Django-Weasyprint image not renderedDjango-Weasyprint 图像未呈现
【发布时间】:2018-12-19 15:23:54
【问题描述】:

我使用 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 的东西。

【问题讨论】:

    标签: python html css django weasyprint


    【解决方案1】:

    您可能希望 base_url 成为

    request.build_absolute_uri('/')
    

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

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

    【讨论】:

    • 也解决了我的问题。
    猜你喜欢
    • 2020-09-03
    • 2020-04-08
    • 2016-01-01
    • 2013-10-07
    • 2014-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多