Python-PDFKit: HTML 轉(zhuǎn) PDF 封裝器
Python 2 和 3 的 wkhtmltopdf 工具封裝器使用了Webkit,且是由ruby PDFKit改造而來的。
安裝
- 安裝 python-pdfkit:
$ pip install pdfkit (or pip3 for python3)
- 安裝 wkhtmltopdf:
- Debian/Ubuntu:
$ sudo apt-get install wkhtmltopdf
- macOS:
$ brew install caskroom/cask/wkhtmltopdf
警告!Debian/ubuntu repos中的版本減少了這樣一些功能(因?yàn)樗鼪]有wkhtmltopdf Qt補(bǔ)丁就直接編譯了),比如添加輪廓、頁眉、頁腳、TOC等。要使用此選項(xiàng),得在這里安裝靜態(tài)二進(jìn)制文件 wkhtmltopdf ,或者可以用這個(gè)描述文件.
- Windows和其他選項(xiàng):檢查 wkhtmltopdf主頁里的二進(jìn)制程序
用法
簡單使用:
import pdfkit
pdfkit.from_url('http://google.com', 'out.pdf')
pdfkit.from_file('test.html', 'out.pdf')
pdfkit.from_string('Hello!', 'out.pdf')
可以使用網(wǎng)址或文件的列表作為傳遞的參數(shù):
pdfkit.from_url(['google.com', 'yandex.ru', 'engadget.com'], 'out.pdf')
pdfkit.from_file(['file1.html', 'file2.html'], 'out.pdf')
也可以傳入文件對象 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:
# 如果不想輸出路徑,那么可以使用false參數(shù),將pdf保存到變量中。
pdf = pdfkit.from_url('http://google.com', False)
你可以查看所有的wkhtmltopdf 設(shè)置參數(shù).
你可以在選項(xiàng)名中寫上“—”。如果選項(xiàng)沒有值,則將None、False或* " *用于dict value:。對于可重復(fù)選項(xiàng)(包括 allow, cookie, custom-header, post, postfile, run-script, replace),您可以使用列表或元組。使用需要多個(gè)值的選項(xiàng)(例如:custom-header認(rèn)證秘鑰),我們可以使用一個(gè)雙元組(參見下面的示例)。
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-name1', 'cookie-value1'),
('cookie-name2', 'cookie-value2'),
],
'no-outline': None
}
pdfkit.from_url('http://google.com', 'out.pdf', options=options)
默認(rèn)情況下, PDFKit 會顯示所有的wkhtmltopdf 輸出。不想要的話, 要在設(shè)置里傳入quiet :
options = {
'quiet': ''
}
pdfkit.from_url('google.com', 'out.pdf', options=options)
由于wkhtmltopdf命令語法,必須單獨(dú)指定** TOC 和 Cover **選項(xiàng)。 如果您在TOC之前需要加封面,請使用cover_first選項(xiàng):
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)
使用css選項(xiàng)轉(zhuǎn)換文件或字符串時(shí),可以指定外部CSS文件。
Warning 對于wkhtmltopdf里面的 這個(gè) bug 有一個(gè)變通的辦法??梢韵葒L試--user-style-sheet 選項(xiàng)。
# 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)
也可以傳遞HTML的元標(biāo)簽:
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
配置
每個(gè)API調(diào)用都采用可選的配置參數(shù)。 這應(yīng)該是pdfkit.configuration()API調(diào)用的一個(gè)實(shí)例. 它將配置選項(xiàng)作為初始參數(shù)。 可用選項(xiàng)包括:
-
wkhtmltopdf-wkhtmltopdf二進(jìn)制文件的位置. 默認(rèn)情況下pdfkit會使用which(UNIX) 或者where(Windows)來定位文件。 -
meta_tag_prefix-pdfkit特定元標(biāo)記的前綴 - 默認(rèn)情況下為pdfkit -。
實(shí)例 - 當(dāng) wkhtmltopdf 不在 $PATH上:
config = pdfkit.configuration(wkhtmltopdf='/opt/bin/wkhtmltopdf')
pdfkit.from_string(html_string, output_file, configuration=config)
故障排除
-
IOError: 'No wkhtmltopdf executable found':確保在$ PATH中有wkhtmltopdf或通過自定義配置設(shè)置(參見上一節(jié))。
where wkhtmltopdf(Windows) 或者which wkhtmltopdf(Linux) 應(yīng)當(dāng)能返回二進(jìn)制路徑。 -
IOError: 'Command Failed'此錯(cuò)誤意味著PDFKit無法處理輸入。您可以嘗試從錯(cuò)誤消息直接運(yùn)行命令,并查看哪些錯(cuò)誤導(dǎo)致了失敗(在某些wkhtmltopdf版本上,這可能是由于分段錯(cuò)誤造成的),從而使用Webkit將HTML轉(zhuǎn)換為PDF。