【问题标题】:trying to output to pdf using weasyprint fails css尝试使用 weasyprint 输出到 pdf 失败 css
【发布时间】:2016-01-27 15:12:29
【问题描述】:

我正在尝试从 django 输出 pdf。我正在使用 weasyprint。这是我的看法:

def fleet_report_pdf(request):
    template = loader.get_template("Reports/fleetreport.html")
    context = {
     'crews':  models.Unit.objects.all().annotate(c=Count('memberunit__Member')),
     's31': models.Member.objects.filter(memberunit__isnull=True)
    }
    html = template.render(RequestContext(request, context))
    response = HttpResponse(content_type="application/pdf")
    weasyprint.HTML(string=html, base_url=request.build_absolute_uri(), url_fetcher=request).write_pdf(response)
    return response

pdf 加载但是,页面没有样式。进一步挖掘时,我注意到控制台中有以下错误:

Failed to load stylesheet at http://127.0.0.1:8000/static/css/layout.css : TypeError: 'WSGIRequest' object is not callable
Failed to load stylesheet at http://127.0.0.1:8000/static/css/darkblue.css : TypeError: 'WSGIRequest' object is not callable
Failed to load stylesheet at http://127.0.0.1:8000/static/css/custom.css : TypeError: 'WSGIRequest' object is not callable

这告诉我我错过了一些东西。如何使样式表可调用?谢谢。

【问题讨论】:

    标签: css django pdf weasyprint


    【解决方案1】:

    您在 weasyprint.HTML 调用 (url_fetcher=request) 中使用 request 对象作为 url 提取器。您不需要提供此参数来获取不需要身份验证等的简单资源。

    有关 URL 提取器的详细信息,请参阅 this

    【讨论】:

    • 非常感谢。这在很大程度上解决了问题。现在,我需要做的就是修复一些 css 着色问题。再次感谢。
    • 404 File not found 上面的链接......讽刺的是。
    猜你喜欢
    • 1970-01-01
    • 2020-03-16
    • 2020-02-17
    • 2010-12-17
    • 1970-01-01
    • 2018-03-26
    • 1970-01-01
    • 2012-05-25
    • 1970-01-01
    相关资源
    最近更新 更多