pdfkit,把 HTML+CSS 格式的文件轉換成 PDF 格式文檔的一個工具。
其實,pdfkit 是 html 轉成 pdf 工具包 wkhtmltopdf 的 Python 封裝。所以,首先安裝 wkhtmltopdf 。 一般情況下,wkhtmltopdf需要手動安裝,網站是 https://wkhtmltopdf.org/downloads.html,根據自己的操作系統下載對應的版本即可。ps:記住安裝目錄啊,下面要用到。
上面說到了pdfkit這個模塊,這個是第三方模塊,需要安裝,使用pip安裝即可。
pip install pdfkit
示例
pdfkit 可以將網頁、html文件以及字符串生成pdf文件
?
import pdfkitconfg = pdfkit.configuration(wkhtmltopdf='C:\Python35\wkhtmltopdf.exe')# 這里指定一下wkhtmltopdf的路徑,這就是我為啥在前面讓記住這個路徑
url = 'https://blog.csdn.net/fenglepeng/article/details/103670893'
pdfkit.from_url(url, 'aaa.pdf', configuration=confg)
# from_url這個函數是從url里面獲取內容
# 這有3個參數,第一個是url,第二個是文件名,第三個就是khtmltopdf的路徑pdfkit.from_file('my.html', 'bbb.pdf', configuration=confg)
# from_file這個函數是從文件里面獲取內容
# 這有3個參數,第一個是一個html文件,第二個是文生成的pdf的名字,第三個就是khtmltopdf的路徑html = '''
<div>
<h1>title</h1>
<p>content</p>
</div>
'''
pdfkit.from_string(html, 'ccc.pdf', configuration=confg)
# from_file這個函數是從一個字符串里面獲取內容
# 這有3個參數,第一個是一個字符串,第二個是文生成的pdf的名字,第三個就是khtmltopdf的路徑
API
def from_url(url, output_path, options=None, toc=None, cover=None, configuration=None, cover_first=False): """ 把從URL獲取文件轉換為PDF文件 :param url: URL 或 URL列表 :param output_path: 輸出PDF文件的路徑。如果是參數等于False,意味著文件將會以字符串的形式返回,得到文本文件。:param options: (可選) dict with wkhtmltopdf global and page options, with or w/o '--' :param toc: (可選) dict with toc-specific wkhtmltopdf options, with or w/o '--' :param cover: (可選) string with url/filename with a cover html page :param configuration: (可選)實例化 pdfkit.configuration.Configuration() :param configuration_first: (可選) if True, cover always precedes TOC Returns:成功返回True """ def from_file(input, output_path, options=None, toc=None, cover=None, css=None, configuration=None, cover_first=False): """ Convert HTML file or files to PDF document :param input: path to HTML file or list with paths or file-like object :param output_path: path to output PDF file. False means file will be returned as string. :param options: (optional) dict with wkhtmltopdf options, with or w/o '--':param toc: (optional) dict with toc-specific wkhtmltopdf options, with or w/o '--' :param cover: (optional) string with url/filename with a cover html page :param css: (optional) string with path to css file which will be added to a single input file :param configuration: (optional) instance of pdfkit.configuration.Configuration() :param configuration_first: (optional) if True, cover always precedes TOC Returns: True on success """ def from_string(input, output_path, options=None, toc=None, cover=None, css=None, configuration=None, cover_first=False):"""Convert given string or strings to PDF document:param input: string with a desired text. Could be a raw text or a html file:param output_path: path to output PDF file. False means file will be returned as string.:param options: (optional) dict with wkhtmltopdf options, with or w/o '--':param toc: (optional) dict with toc-specific wkhtmltopdf options, with or w/o '--':param cover: (optional) string with url/filename with a cover html page:param css: (optional) string with path to css file which will be added to a input string:param configuration: (optional) instance of pdfkit.configuration.Configuration():param configuration_first: (optional) if True, cover always precedes TOCReturns: True on success"""
?
?