Python網(wǎng)頁內(nèi)容轉(zhuǎn)pdf封裝器的使用說明

Python-PDFKit: HTML 轉(zhuǎn) PDF 封裝器

Python 2 和 3 的 wkhtmltopdf 工具封裝器使用了Webkit,且是由ruby PDFKit改造而來的。

安裝

  1. 安裝 python-pdfkit:
$ pip install pdfkit  (or pip3 for python3)
  1. 安裝 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è)描述文件.

用法

簡單使用:

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。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • feisky云計(jì)算、虛擬化與Linux技術(shù)筆記posts - 1014, comments - 298, trac...
    不排版閱讀 4,356評論 0 5
  • Spring Web MVC Spring Web MVC 是包含在 Spring 框架中的 Web 框架,建立于...
    Hsinwong閱讀 22,955評論 1 92
  • 有一種婊,叫綠茶婊,心機(jī)婊,泰迪婊!只要是個(gè)男的,她定是極盡風(fēng)騷之能,微信上撩,扣扣上約,男女單獨(dú)出去吃飯看電影,...
    毛不言閱讀 1,975評論 0 0
  • 昨天下午我寫了一個(gè)作業(yè)計(jì)劃。剛開始的時(shí)候,我寫的不好。后來,媽媽又給我拿的本,結(jié)果我寫的還是不滿意!我哇哇大哭起來...
    崔珂豪本人閱讀 300評論 0 0
  • 今天一天的生活結(jié)束了。 女兒今領(lǐng)通知書,將近8點(diǎn)去送她去學(xué)校。也沒吃早飯,到街上再吃吧。女兒直接去學(xué)校了,我去吃早...
    天道酬勤_苑閱讀 233評論 0 0

友情鏈接更多精彩內(nèi)容