Django-CBV:以编程方式将HTML页面保存到

2024-09-27 23:21:09 发布

您现在位置:Python中文网/ 问答频道 /正文

访问相关的url,就会呈现一个html页面。我需要以编程方式将这个html页面保存到服务器上的pdf文件中。在

视图.py

class PdfView(DetailView):
    model = TheModel
    template_name = 'pdf.html'

网址.py

^{pr2}$

Tags: 文件py服务器视图urlmodelpdfhtml
2条回答

以下解决方案在HTTP请求期间,在HTTP响应提供给浏览器之前,保存web页面的pdf版本。在

我使用weasyprint是因为它能够处理开箱即用的unicode字符。在

视图.py

from weasyprint import HTML

class PdfView(DetailView):
    model = TheModel
    template_name = 'pdf.html'

    def get(self, request, *args, **kwargs):
        template_response = super().get(self, request, *args, **kwargs)
        HTML(string=template_response.rendered_content).write_pdf(
            '/path/to/test.pdf',
            stylesheets=['/path/to/pdf.css']
        )
        return template_response

在视图之外,一个更好的解决方案是使用requests获取HTML页面,然后创建pdf文件。这样做更好,因为服务器不必等待创建pdf,这可能会阻止其他等待服务器的请求:

^{pr2}$

尝试下面这样的方法。虽然它通过响应向最终用户呈现服务文件,但将其修改为在服务器上写入文件对您来说应该很容易:

功能视图:

def pdf_sticker(request, pk):
    spot = get_object_or_404(Spot, pk=pk)
    if spot.is_certificated:
        pdf, result = render_to_pdf(
            'www/pdf_sticker.html',
            {
                'pass_smth': 'needed_in_render',
                'MEDIA_ROOT': settings.MEDIA_ROOT,
                'STATIC_ROOT': settings.STATICFILES_DIRS[0],
                'pagesize': 'A6',
            }
        )
        if not pdf.err:
            return HttpResponse(result.getvalue(), content_type='application/pdf')
        return HttpResponse('We had some errors')
    else:
        raise Http404

helper方法:

^{pr2}$

模板:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>


        <title>Sticker for certificated spot</title>
        <style type="text/css">
                 @font-face {
                    font-family: "Lobster";
                    src: url("{{ STATIC_ROOT }}/font/Lobster-32.ttf");
                    font-weight: normal;
                    font-style: normal;
                }
                @font-face {
                    font-family: "Lato";
                    src: url("{{ STATIC_ROOT }}/font/Lato-Hairline.ttf");
                    font-weight: 100;
                    font-style: thin;
                }
                @font-face {
                    font-family: "Nobile";
                    src: url("{{ STATIC_ROOT }}/font/Nobile-34.ttf");
                    font-weight: normal;
                    font-style: normal;
                }

                @page {
                    size: {{ pagesize }};
                    margin: 0.5cm;
                }

        </style>
    </head>
    <body>
        <center>
            <img src="{{ STATIC_ROOT }}/{{ SPOT_PROJECT_NAME }}/certificate.png" height="260px">
            <p style="font-family:Lobster;">
                <span style="font-size:60px;">
                    {% settings_value "SPOT_PROJECT_SUBJECT" %}<br>
                </span>
                <span style=" font-size:32px;">
                    friendly spot!
                </span>
                <br>
                <table>
                    <tr>
                        <td colspan=2>
                             <img src="http://{{BASE_HOST}}{% url 'www:qrencode_link' pk=spot.pk size=4 %}">
                        </td>
                    </tr>
                    <tr>
                        <td colspan=2 height=5></td>
                    </tr>
                    <tr>
                        <td colspan=2>
                            <span style="font-family:Nobile; font-size:15px;">
                            Powered by: <img src="{{ STATIC_ROOT }}/{{ SPOT_PROJECT_NAME }}/logo.png" height="50px"> </span>
                            <span style="font-family:Lobster; font-size:25px;">
                                    {{ SPOT_PROJECT_NAME }}
                            </span>
                        </td>
                    </tr>
                </table>
            </p>
        </center>
    </body>
</html>

为Python3.6、Django 2.0工作。在

库版本:

Django==2.0.2
xhtml2pdf==0.2b1

关于将其写入服务器上的本地文件的部分,这可能有助于:

http://xhtml2pdf.readthedocs.io/en/latest/usage.html#using-with-python-standalone

相关问题 更多 >

    热门问题