我一直在网上搜索使用pdfkit(python wrapper)实现页眉和页脚的人的例子,并且找不到任何例子。
任何人都能够展示如何使用pdfkit python包装来实现wkhtmltopdf中的选项的一些例子吗?pdfkit页眉和页脚
1
A
回答
4
我使用它只与标题,但我认为它将与页脚相同。
您需要为标题分开的html文件。
了header.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
Code of your header goes here.
</body>
</html>
然后,如果你使用一些后端像Django和希望使用模板,您可以使用它像在Python
import pdfkit
pdfkit.from_file('path/to/your/file.html', 'out.pdf', {
'--header-html': 'path/to/header.html'
})
棘手的部分是,你可以” t将标题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
结尾并手动删除。
1
提高@V Stoykov答案,因为它使用瓶,与自定义标题中瓶渲染功能将如下帮我:我使用了'--header-html'
和'--footer-html'
符号作为
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
公告它匹配wkhtmltopdf选项格式。
相关问题
- 1. 如何添加页眉和页脚内容到pdfkit for node.js
- 2. 页眉和页脚徽标
- 3. 粘性页眉和页脚
- 4. 页眉和页脚在
- 5. 页眉和页脚冲突
- 6. 通用页眉和页脚
- 7. PHP页眉和页脚
- 8. 页眉和页脚XSL FO
- 9. NSCollectionView与页眉和页脚
- 10. ReactJS页眉和页脚
- 11. 固定页眉和页脚
- 12. listview与页眉和页脚
- 13. iText 5页眉和页脚
- 14. FPDF页眉和页脚
- 15. 页眉和页脚布局
- 16. TraceListener页眉和页脚
- 17. 剥离页眉和页脚
- 18. Cognos页眉/页脚
- 19. 页脚到页眉
- 20. 网站优化和页眉/页脚页
- 21. Freemarker,PDF,页眉/页脚和分页符
- 22. ReportViewer - 修复页面页眉和页脚
- 23. 提取页面的页眉和页脚
- 24. 页眉和页脚和自由标记
- 25. 在VerticalFieldManager中设置页眉和页脚
- 26. 褪色页眉和页脚没有jquerymobile
- 27. 页眉和页脚有白色边框?
- 28. gridview固定页眉和固定页脚
- 29. MVC iTextSharp页眉和页脚c#
- 30. 包括常见的页眉和页脚
如果您认为答案适合您,那么您可以将其标记为已接受的答案。 –