Как использовать пользовательские переменные в заголовке / нижнем колонтитуле wkhtmltopdf для Django? - PullRequest
0 голосов
/ 12 февраля 2019

Я пытаюсь преобразовать html-файлы в pdf, используя django-wkhtmltopdf.Но я не могу использовать пользовательские переменные с верхним и нижним колонтитулами, потому что я не нашел точный синтаксис или правильную реализацию, а также я не могу использовать титульную страницу в wkhtmltopdf v 0.12.5 (пропатчен qt). Выдает ошибку.неизвестный длинный аргумент --cover?

*VIEWS.PY*
from django.http import HttpResponse
from django.views.generic import View
from django.template.loader import get_template 
from django.template import Context
import pdfkit

class GeneratePdf(View):
    def get(self, request, *args, **kwargs):
        template = get_template("report.html")
        options = {
    'page-size': 'A4',
    'margin-top': '0.6in',
    'margin-right': '0.00in',
    'margin-bottom': '0.6in',
    'margin-left': '0.00in',
    'encoding': "UTF-8",
    'no-outline': None,
    'disable-smart-shrinking': False,

     'cover': 'cover.html',
    'header-html': 'header.html',
    'footer-html': 'footer.html',
    'header-spacing': 10,
    'footer-spacing': 5,
    'header-right': '[page]',
    }
    pdfkit.from_string(html, 'reports_demo.pdf', options=options)
    pdf = open("reports_demo.pdf")
    response = HttpResponse(pdf.read(), content_type='application/pdf') 
    return response

1 Ответ

0 голосов
/ 12 февраля 2019

Вам нужны только отдельные html-файлы для верхнего и нижнего колонтитула.Например.

header.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
</head>
<body>
Code of your header goes here.

Тогда вы можете использовать его.

import pdfkit

pdfkit.from_file('path/to/your/file.html', 'out.pdf', {
    '--header-html': 'path/to/header.html'
})
...