繁体   English   中英

pdfkit 页眉和页脚

[英]pdfkit headers and footers

我一直在 web 中搜索使用 pdfkit(python 包装器)实现页眉和页脚的示例,但找不到任何示例。
谁能展示一些示例,说明如何使用 pdfkit python 包装器在 wkhtmltopdf 中实现这些选项?

我只使用它与标题,但我认为它将与页脚一样。

您需要为标题分别使用html文件。

了header.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
</head>
<body>

    Code of your header goes here.

</body>
</html>

然后你可以在Python中使用它

import pdfkit

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

如果您使用像Django这样的后端并且想要使用模板,那么棘手的部分是您无法将标头html作为呈现的字符串传递。 你需要一个文件。

这就是我用Django渲染PDF的方法。

import os
import tempfile
import pdfkit

from django.template.loader import render_to_string


def render_pdf(template, context, output, header_template=None):
    """
    Simple function for easy printing of pdfs from django templates

    Header template can also be set
    """
    html = render_to_string(template, context)
    options = {
        '--load-error-handling': 'skip',
    }
    try:
        if header_template:
            with tempfile.NamedTemporaryFile(suffix='.html', delete=False) as header_html:
                options['header-html'] = header_html.name
                header_html.write(render_to_string(header_template, context).encode('utf-8'))

        return pdfkit.from_string(html, output, options=options)
    finally:
        # Ensure temporary file is deleted after finishing work
        if header_template:
            os.remove(options['header-html'])

在我的示例中,我创建了临时文件,其中放置了渲染内容。 重要的是,临时文件需要以.html结尾并手动删除。

提高@V Stoykov答案,因为它使用 ,在自定义标题将如下渲染功能帮助我:

import os
import tempfile

import pdfkit
from flask import render_template, make_response


@app.route('/generate_pdf')
def render_pdf_custom_header(foo, bar):
    main_content = render_template('main_pdf.html', foo=foo)
    options = {
        '--encoding': "utf-8"
    }

    add_pdf_header(options, bar)
    add_pdf_footer(options)

    try:
        pdf = pdfkit.from_string(main_content, False, options=options)
    finally:
        os.remove(options['--header-html'])
        os.remove(options['--footer-html'])

    response = build_response(pdf)
    return response

def add_pdf_header(options, bar):
    with tempfile.NamedTemporaryFile(suffix='.html', delete=False) as header:
        options['--header-html'] = header.name
        header.write(
            render_template('header.html', bar=bar).encode('utf-8')
        )
    return

def add_pdf_footer(options):
    # same behaviour as add_pdf_header but without passing any variable
    return

def build_response(pdf):
    response = make_response(pdf)
    response.headers['Content-Type'] = 'application/pdf'
    filename = 'pdf-from-html.pdf'
    response.headers['Content-Disposition'] = ('attachment; filename=' + filename)
    return response

请注意,我使用'--header-html''--footer-html'表示法,因为它符合wkhtmltopdf选项格式。

options = {
'page-size': 'Letter',
'margin-top': '0.9in',
'margin-right': '0.9in',
'margin-bottom': '0.9in',
'margin-left': '0.9in',
'encoding': "UTF-8",
'header-center': 'YOUR HEADER',
'custom-header' : [
    ('Accept-Encoding', 'gzip')
],
'no-outline':None
}

您可以在header-center的值中添加header-center

这个问题及其答案已经很老了,对我不起作用。

wkhtmltopdf 版本:$ wkhtmltopdf --version

wkhtmltopdf 0.12.6(带补丁的 qt)

python 3.8

对于 wkhtmltopdf, header-htmlfooter-html只能是 URI,例如 html url 或文件路径,不能是字符串。 所以我们的想法是将每个 html 文件保存在云中,或者在本地创建一个临时文件作为页脚和 header 以供参考。

要求:

  1. 内容为urlhtml
  2. header 为html urlhtml string
  3. 页脚为html urlhtml string

例子:

import logging
import os
import tempfile
import pdfkit
from flask import Flask, Response, make_response
from flask_restx import Resource, Api, fields

Request = api.model('Request', {
    'url': fields.String(
        required=False,
        description='url',
        example='https://www.w3schools.com/html/html5_svg.asp',
    ),
    'html': fields.String(
        required=False,
        description='content html string',
        example=example_content_html
    ),
    'header_html': fields.String(
        required=False,
        description='pdf header html string',
        example=example_header_html
    ),
    'footer_html': fields.String(
        required=False,
        description='pdf footer html string',
        example=example_footer_html
    ),
})
@api.route("/convert_html_to_pdf", endpoint = 'html2pdf')
@api.representation('application/octet-stream')
class PdfConverter(Resource):
    @api.doc(body=Request)
    def post(self):
        logging.info(request.json)
        url = request.json.get('url')
        html = request.json.get('html')
        header_html = request.json.get('header_html')
        footer_html = request.json.get('footer_html')
        header_uri = 'https://xxxxx/header.html' # default header
        footer_uri = 'https://xxxxx/footer.html' # default footer

        if header_html:
            fph = tempfile.NamedTemporaryFile(suffix='.html')
            fph.write(header_html.encode('utf-8'))
            fph.flush()
            header_uri = fph.name

        if footer_html:
            fpf = tempfile.NamedTemporaryFile(suffix='.html')
            fpf.write(footer_html.encode('utf-8'))
            fpf.flush()
            footer_uri = fpf.name

        options = {
          'page-size': 'A4',
          'margin-top': '32mm',
          'header-spacing': 6,
          'footer-spacing': 6,
          'header-html': header_uri,
          'footer-html': footer_uri,
          'margin-right': '0',
          'margin-bottom': '16mm',
          'margin-left': '0',
          'encoding': "UTF-8",
          'cookie': [
            ('cookie-empty-value', '""'),
            ('cookie-name1', 'cookie-value1'),
            ('cookie-name2', 'cookie-value2'),
          ],
          'no-outline': None
        }
        logging.info(options)

        if url:
            # api.payload['requestedBlobUrl'] = url
            # return api.payload
            pdf = pdfkit.from_url(url, options=options)
        else:
            pdf = pdfkit.from_string(html, options=options)

        if header_html:
            fph.close()
        if footer_html:
            fpf.close() # close will delete the temp file

        response = make_response(pdf)
        response.headers['Content-Type'] = 'application/pdf'
        response.headers['Content-Disposition'] = 'attachment;filename=report.pdf'

        return response

Python pdfkit 包装器仅支持 html 文件作为 header 和页脚。 为了能够支持字符串,您需要做的就是生成一个临时文件并在关闭时将其删除。 这是我的代码示例。

使用 delete=True 和 suffix='.html' arguments 的 tempfile 将在 temp.close() 上生成一个可删除文件

import tempfile

temp = tempfile.NamedTemporaryFile(delete=True,suffix='.html')
with open(temp.name, 'w') as f:
  f.write("""
          <!DOCTYPE html>
          <html>
          <head>
              <meta charset="UTF-8">
          </head>
          <body>

              Code of your header goes here.

          </body>
          </html>
          """)
options = {
    'page-size': 'A4',
    'margin-top': '1in',
    'margin-bottom': '0.75in',
    'margin-right': '0.75in',
    'margin-left': '0.75in',
    'encoding': "UTF-8",
    'header-html': temp.name,
    'footer-center': "Page [page] of [topage]",
    'footer-font-size': "9",
     'custom-header': [
        ('Accept-Encoding', 'gzip')
      ],
     'enable-local-file-access': False,
     'no-outline': None
}
pdf = pdfkit.from_string(html_string, options=options)
temp.close()

暂无
暂无

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

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