简体   繁体   English

如何在Django的页眉/页脚wkhtmltopdf中使用自定义变量?

[英]how to use custom variables in header/footer wkhtmltopdf for django?

I'm trying to convert html files into pdf using django-wkhtmltopdf. 我正在尝试使用django-wkhtmltopdf将html文件转换为pdf。 But I'm unable to use custom variables with header and footer because I didn't find the exact synatax or correct implementation and also I'm not able to use cover page in wkhtmltopdf v 0.12.5 (patched qt) It throws an error unknown long argument --cover? 但是我无法将自定义变量与页眉和页脚一起使用,因为我找不到确切的语法或正确的实现方式,而且我也无法在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

You only need to have separate html files for the header and footer. 您只需要为页眉和页脚设置单独的html文件。 for Example. 例如。

header.html 了header.html

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

Then you can use it. 然后就可以使用它了。

import pdfkit

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM