18910140161

Django项目中WeazyPrint生成的未显示为PDF格式的图像

顺晟科技

2021-07-26 10:56:37

357

我有一个关于使用Weazy Print在Django项目中生成PDF的问题

所以我所做的件事是确保PDF按照下面的函数中包含的CSS文件正确显示在views.py:

def admin_order_pdf(request, info_id):
    info = get_object_or_404(Info, id=info_id)
    html = render_to_string('businessplan/pdf.html', {'info': info})
    response = HttpResponse(content_type='application/pdf')
    response['Content-Disposition'] = 'filename="order_{}.pdf"'.format(
        Info.id)
    weasyprint.HTML(string=html,base_url=request.build_absolute_uri()).write_pdf(response,
                                           stylesheets=[weasyprint.CSS(settings.STATICFILES_DIRS[0] + '\css\\report.css')], presentational_hints=True)
    return response

其次,我已经在report.css中包含了{% load static %},并且我正在尝试使用以下方式访问图像文件:

@page :first {
  background: url('{% static "img/report-cover.jpg" %}') no-repeat center;
  background-size: cover;
  margin: 0; }

我查看了以下问题及其答案,但这些问题无助于实现显示图像的目标:

使用Weasyprint不显示图像的PDF输出(Django) 在pdf weasyprint中附加img文件

我的试验:

我可以通过在html页面中@page :first之间添加<style></style>来获得显示的图像,但我的问题是我需要链接其他东西,比如字体和SVG,所以我想知道我做错了什么,不允许css文件显示静态文件中的内容。

我的问题要更加明确:

我希望能够链接静态文件夹中的文件,以便从report.css文件中显示


顺晟科技:

  • TAG:
相关文章
我们已经准备好了,你呢?
2024我们与您携手共赢,为您的企业形象保驾护航