在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:JazzCore/python-pdfkit开源软件地址:https://github.com/JazzCore/python-pdfkit开源编程语言:Python 99.1%开源软件介绍:Python-PDFKit: HTML to PDF wrapperPython 2 and 3 wrapper for wkhtmltopdf utility to convert HTML to PDF using Webkit. This is adapted version of ruby PDFKit library, so big thanks to them! Installation
$ pip install pdfkit (or pip3 for python3)
$ sudo apt-get install wkhtmltopdf
$ brew install homebrew/cask/wkhtmltopdf Warning! Version in debian/ubuntu repos have reduced functionality (because it compiled without the wkhtmltopdf QT patches), such as adding outlines, headers, footers, TOC etc. To use this options you should install static binary from wkhtmltopdf site or you can use this script (written for CI servers with Ubuntu 18.04 Bionic, but it could work on other Ubuntu/Debian versions).
UsageFor simple tasks: import pdfkit
pdfkit.from_url('http://google.com', 'out.pdf')
pdfkit.from_file('test.html', 'out.pdf')
pdfkit.from_string('Hello!', 'out.pdf') You can pass a list with multiple URLs or files: pdfkit.from_url(['google.com', 'yandex.ru', 'engadget.com'], 'out.pdf')
pdfkit.from_file(['file1.html', 'file2.html'], 'out.pdf') Also you can pass an opened file: with open('file.html') as f:
pdfkit.from_file(f, 'out.pdf') If you wish to further process generated PDF, you can read it to a variable: # Without output_path, PDF is returned for assigning to a variable
pdf = pdfkit.from_url('http://google.com') You can specify all wkhtmltopdf options. You can drop '--' in option name. If option without value, use None, False or '' for dict value:. For repeatable options (incl. allow, cookie, custom-header, post, postfile, run-script, replace) you may use a list or a tuple. With option that need multiple values (e.g. --custom-header Authorization secret) we may use a 2-tuple (see example below). options = {
'page-size': 'Letter',
'margin-top': '0.75in',
'margin-right': '0.75in',
'margin-bottom': '0.75in',
'margin-left': '0.75in',
'encoding': "UTF-8",
'custom-header': [
('Accept-Encoding', 'gzip')
],
'cookie': [
('cookie-empty-value', '""')
('cookie-name1', 'cookie-value1'),
('cookie-name2', 'cookie-value2'),
],
'no-outline': None
}
pdfkit.from_url('http://google.com', 'out.pdf', options=options) By default, PDFKit will run pdfkit.from_url('google.com', 'out.pdf', verbose=True) Due to wkhtmltopdf command syntax, TOC and Cover options must be specified separately. If you need cover before TOC, use toc = {
'xsl-style-sheet': 'toc.xsl'
}
cover = 'cover.html'
pdfkit.from_file('file.html', options=options, toc=toc, cover=cover)
pdfkit.from_file('file.html', options=options, toc=toc, cover=cover, cover_first=True) You can specify external CSS files when converting files or strings using css option. Warning This is a workaround for this bug in wkhtmltopdf. You should try --user-style-sheet option first. # Single CSS file
css = 'example.css'
pdfkit.from_file('file.html', options=options, css=css)
# Multiple CSS files
css = ['example.css', 'example2.css']
pdfkit.from_file('file.html', options=options, css=css) You can also pass any options through meta tags in your HTML: body = """
<html>
<head>
<meta name="pdfkit-page-size" content="Legal"/>
<meta name="pdfkit-orientation" content="Landscape"/>
</head>
Hello World!
</html>
"""
pdfkit.from_string(body, 'out.pdf') #with --page-size=Legal and --orientation=Landscape ConfigurationEach API call takes an optional configuration parameter. This should be an instance of
Example - for when config = pdfkit.configuration(wkhtmltopdf='/opt/bin/wkhtmltopdf')
pdfkit.from_string(html_string, output_file, configuration=config) Also you can use try:
config = pdfkit.configuration()
pdfkit.from_string(html_string, output_file)
except OSError:
#not present in PATH TroubleshootingDebugging issues with PDF generationIf you struggling to generate correct PDF firstly you should check pdfkit.from_url('http://google.com', 'out.pdf', verbose=True) If you are getting strange results in PDF or some option looks like its ignored you should try to run import pdfkit
r = pdfkit.PDFKit('html', 'string', verbose=True)
print(' '.join(r.command()))
# try running wkhtmltopdf to create PDF
output = r.to_pdf() Common errors:
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论