繁体   English   中英

WeasyPrint 和 Bootstrap

[英]WeasyPrint and Bootstrap

所以我有一个非常简单的模板,我正在使用 WeasyPrint 进行测试。 如果将模板呈现为常规网页,它会很好地呈现。 如果我尝试生成 PDF 样式消失。 PDF 正确生成的唯一时间是当我删除引导程序引用时。 一旦我引入引导程序 css 文件,是否有人对为什么样式不起作用有任何想法? 我尝试了一些 Bootstrap3 和 Bootstrap2 文件。 本地和 CDN 服务。

模板:

<!doctype html>
<html lang="en">
{%  load static from staticfiles %}
{%  block head %}
    <head>
    {% block css %}
        <link rel="stylesheet" href="{%  static "css/bootstrap.min.css" %}">
        <link rel="stylesheet" href="{%  static "css/main.css" %}">
    {% endblock css %}
    </head>
{% endblock head %}
<body>
{% block content %}
    <div id="content" class="container">
        <div id="logo" class="col-md-3">
            <img src="{%  static "images/logo_small.png" %}">
        </div>
        <div id="heading" class="col-md-6">
            <h1>Packing Slip</h1>
        </div>

        <div class="col-md-3">
            <h2>{{ packslip_id }}</h2>
        </div>
    </div>
{% endblock %}
</body>
</html>

我的观点:

class WeasyPDF(TemplateView):
template_name = 'jinja2/Shipping/test_pdf.html'


def get(self, request, *args, **kwargs):
    packslip_id = kwargs.get('packslip_id')
    context= {'packslip_id': packslip_id }
    template_string = render_to_string(self.template_name, context)
    html = HTML(string=template_string, base_url=request.build_absolute_uri())
    main_doc = html.render()
    pdf = main_doc.write_pdf()
    response = HttpResponse(pdf, content_type='application/pdf')
    #Download as attachment
    # response['Content-Disposition'] = 'attachment; filename=packslip-{0}.pdf'.format(packslip_id)
    # Display in browser
    response['Content-Disposition'] = 'filename=packslip-{0}.pdf'.format(packslip_id)
    return response

最后是 urls.py 条目

    url(r'^weasypdf/(?P<packslip_id>\d+)$', WeasyPDF.as_view(), name='weasypdf'),

我相信您需要将CSS渲染到html对象中:html = HTML(string = template_string,base_url = request.build_absolute_uri(),stylesheet =“ filename.css”)

完成后,Weasyprint会将其渲染到HTML对象中,以PDF和HTML形式显示

您应该将样式表的位置放在视图中,而不是模板中。

class WeasyPDF(TemplateView):
template_name = 'jinja2/Shipping/test_pdf.html'


def get(self, request, *args, **kwargs):
    packslip_id = kwargs.get('packslip_id')
    context= {'packslip_id': packslip_id }
    template_string = render_to_string(self.template_name, context)
    html = HTML(string=template_string, base_url=request.build_absolute_uri())
    main_doc = html.render()
    pdf = main_doc.write_pdf(
    stylesheets=[
        # Change this to suit your css path
        settings.BASE_DIR + 'css/bootstrap.min.css',
        settings.BASE_DIR + 'css/main.css',
    ],
    )
    response = HttpResponse(pdf, content_type='application/pdf')
    #Download as attachment
    # response['Content-Disposition'] = 'attachment; filename=packslip-{0}.pdf'.format(packslip_id)
    # Display in browser
    response['Content-Disposition'] = 'filename=packslip-{0}.pdf'.format(packslip_id)
    return response

解决方案一:使用 Weasyprint CSS 造型

styles = CSS(settings.STATIC_ROOT + "/css/bootstrap.min.css")
http_response = HttpResponse(content_type="application/pdf")
http_response[
    "Content-Disposition"
] = 'attachment;filename="pdf_file.pdf"'
HTML(string=html_string, base_url=request.build_absolute_uri()).write_pdf(
    http_response,
    stylesheets=[
        styles,
    ],
)
return http_response

完成后,Weasyprint 会将其渲染到 HTML object 中,以在 PDF 和 Z4C4AD5FCA2E007A3F7AA1CEDCEDCEDB1 中显示

此处查看 weasyprint 样式文档

解决方案2:在django中使用常规的外部static css

在您的 django 模板中使用

{% load static %}
<!doctype html>
.
.
.
    <link  href="{% static 'css/bootstrap.min.css' %}" rel="stylesheet" >
.
.
.

在您看来使用

HTML(string=html_string, base_url=request.build_absolute_uri()).write_pdf(
    http_response,
    presentational_hints=True,
)
return http_response

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM