4

だから私はWeasyPrintでテストしている非常に単純なテンプレートを持っています. テンプレートを通常の Web ページとしてレンダリングすると、問題なくレンダリングされます。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'),
4

2 に答える 2