WeasyPrint和 Bootstrap

xuo3flqw  于 2022-12-07  发布在  Bootstrap
关注(0)|答案(3)|浏览(141)

所以我有一个非常简单的模板,我用WeasyPrint测试。如果把模板渲染成一个普通的网页,它就能很好地渲染。如果我尝试生成一个PDF,样式就消失了。只有当我删除 Bootstrap 引用时,PDF才能正确生成。有人有什么想法吗?为什么我一引入 Bootstrap 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'),
yquaqz18

yquaqz181#

我认为您需要将CSS呈现到html对象中:html = HTML(字符串=模板字符串,基本URL=请求.build_absolute_uri(),样式表=“文件名. css”)
一旦完成,Weasyprint就会将其呈现为HTML对象,以便在PDF和HTML中显示

xzabzqsa

xzabzqsa2#

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

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
fruv7luv

fruv7luv3#

解决方案1:使用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对象,以便在PDF和HTML中显示

请在此处查看weasyprint样式文档

解决方案2:在Django中使用常规的外部静态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

相关问题