Трудности преобразования html-страницы с CSS и изображениями в PDF в Django

Я пытаюсь преобразовать HTML-страницу, которая использует довольно много bootstrap и стилизации. Я перепробовал все, от reportlab до weasyprint и xhtml2pdf, и ничего не работает! Я не нашел в Интернете ничего, что помогло бы мне выполнить эту простую задачу. Я даже пытался искать платные API, которые выполняют такую работу. Какой лучший способ достичь этого?

Руководство по использованию шаблона HTML для создания PDF с помощью функции render_to_pdf.

  1. Откройте свой проект Django или создайте пустой

    .
  2. Установите xhtml2pdf docs:

Использование Python 3

pip install --pre xhtml2pdf 

Использование Python 2

pip install xhtml2pdf
  1. Добавьте модуль utils.py в ваш проект:

  2. Напишите функцию render_to_pdf:

from io import BytesIO
from django.http import HttpResponse
from django.template.loader import get_template

from xhtml2pdf import pisa

def render_to_pdf(template_src, context_dict={}):
    template = get_template(template_src)
    html  = template.render(context_dict)
    result = BytesIO()
    pdf = pisa.pisaDocument(BytesIO(html.encode("ISO-8859-1")), result)
    if not pdf.err:
        return HttpResponse(result.getvalue(), content_type='application/pdf')
    return None

  1. Создайте html-шаблон, такой как invoice.html в templates/pdf Рекомендуется использовать внутренние/строчные таблицы стилей :
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <title>Title</title>
        <style type="text/css">
            body {
                font-weight: 200;
                font-size: 14px;
            }
            .header {
                font-size: 20px;
                font-weight: 100;
                text-align: center;
                color: #007cae;
            }
            .title {
                font-size: 22px;
                font-weight: 100;
               /* text-align: right;*/
               padding: 10px 20px 0px 20px;  
            }
            .title span {
                color: #007cae;
            }
            .details {
                padding: 10px 20px 0px 20px;
                text-align: left !important;
                /*margin-left: 40%;*/
            }
            .hrItem {
                border: none;
                height: 1px;
                /* Set the hr color */
                color: #333; /* old IE */
                background-color: #fff; /* Modern Browsers */
            }
        </style>
    </head>
    <body>
        <div class='wrapper'>
            <div class='header'>
                <p class='title'>Invoice # </p>
            </div>
        <div>
        <div class='details'>
            Bill to: <br/>
            Amount:  <br/>
            Date: 
            <hr class='hrItem' />
        </div>
    </div>
    </body>
</html>
  1. Использовать в представлении:
from django.http import HttpResponse
from django.views.generic import View

from yourproject.utils import render_to_pdf #created in step 4

class GeneratePdf(View):
    def get(self, request, *args, **kwargs):
        data = {
             'today': datetime.date.today(), 
             'amount': 39.99,
            'customer_name': 'Cooper Mann',
            'order_id': 1233434,
        }
        pdf = render_to_pdf('pdf/invoice.html', data)
        return HttpResponse(pdf, content_type='application/pdf')

  1. Force PDF Download:
class GeneratePDF(View):
    def get(self, request, *args, **kwargs):
        template = get_template('invoice.html')
        context = {
            "invoice_id": 123,
            "customer_name": "John Cooper",
            "amount": 1399.99,
            "today": "Today",
        }
        html = template.render(context)
        pdf = render_to_pdf('invoice.html', context)
        if pdf:
            response = HttpResponse(pdf, content_type='application/pdf')
            filename = "Invoice_%s.pdf" %("12341231")
            content = "inline; filename='%s'" %(filename)
            download = request.GET.get("download")
            if download:
                content = "attachment; filename='%s'" %(filename)
            response['Content-Disposition'] = content
            return response
        return HttpResponse("Not found")
Вернуться на верх