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中的版本減少了這樣一些功能(因為它沒有wkhtmltopdf Qt補丁就直接編譯了)曙博,比如添加輪廓拥刻、頁眉、頁腳父泳、TOC等泰佳。要使用此選項盼砍,得在這里安裝靜態(tài)二進制文件 wkhtmltopdf ,或者可以用這個描述文件.
- Windows和其他選項:檢查 wkhtmltopdf主頁里的二進制程序
用法
簡單使用:
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ù).
你可以在選項名中寫上“—”黔宛。如果選項沒有值近刘,則將None、False或* " *用于dict value:臀晃。對于可重復(fù)選項(包括 allow, cookie, custom-header, post, postfile, run-script, replace)觉渴,您可以使用列表或元組。使用需要多個值的選項(例如:custom-header認(rèn)證秘鑰)徽惋,我們可以使用一個雙元組(參見下面的示例)案淋。
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命令語法踢京,必須單獨指定** TOC 和 Cover **選項。 如果您在TOC之前需要加封面宦棺,請使用cover_first
選項:
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
選項轉(zhuǎn)換文件或字符串時瓣距,可以指定外部CSS文件。
Warning 對于wkhtmltopdf里面的 這個 bug 有一個變通的辦法代咸〉竿瑁可以先嘗試--user-style-sheet
選項。
# 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
配置
每個API調(diào)用都采用可選的配置參數(shù)呐芥。 這應(yīng)該是pdfkit.configuration()
API調(diào)用的一個實例. 它將配置選項作為初始參數(shù)逻杖。 可用選項包括:
-
wkhtmltopdf
-wkhtmltopdf
二進制文件的位置. 默認(rèn)情況下pdfkit
會使用which
(UNIX) 或者where
(Windows)來定位文件。 -
meta_tag_prefix
-pdfkit
特定元標(biāo)記的前綴 - 默認(rèn)情況下為pdfkit -
思瘟。
實例 - 當(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)能返回二進制路徑。 -
IOError: 'Command Failed'
此錯誤意味著PDFKit無法處理輸入潮太。您可以嘗試從錯誤消息直接運行命令管搪,并查看哪些錯誤導(dǎo)致了失敗(在某些wkhtmltopdf版本上,這可能是由于分段錯誤造成的)铡买,從而使用Webkit將HTML轉(zhuǎn)換為PDF更鲁。