主要使用的是wkhtmltopdf的Python封装——pdfkit
centos环境
安装:Install python-pdfkit
pip install pdfkit
安装:Install wkhtmltopdf
yum intsall wkhtmltopdf
windows环境下安装wkhtmltopdf参考这篇文章:
importpdfkit
pdfkit.from_string('hello,python','out.pdf') #通过文本直接进行转换
pdfkit.from_url('http://baidu.com','out.pdf') #通过网址进行转换
pdfkit.from_file('test.html', 'out.pdf') #通过html文件进行转换
我们也可以传递一个url或者文件名列表:
pdfkit.from_url(['google.com', 'yandex.ru', 'engadget.com'], 'out.pdf') pdfkit.from_file(['file1.html', 'file2.html'], 'out.pdf')
也可以传递一个打开的文件:
with open('file.html') as f:
pdfkit.from_file(f,'out.pdf')
如果想对生成的PDF作进一步处理,我们可以将其读取到一个变量中:
#设置输出文件为False,将结果赋给一个变量
pdf = pdfkit.form_url('http://google.com', False)
我们可以制定所有的 wkhtmltopdf 选项 http://wkhtmltopdf.org/usage/wkhtmltopdf.txt. 我们可以移除选项名字前面的 '--' .如果选项没有值, 使用None, Falseor * 作为字典值:
options ={'page-size': 'Letter','margin-top': '0.75in','margin-right': '0.75in','margin-bottom': '0.75in','margin-left': '0.75in','encoding': "UTF-8",'no-outline': None
}
pdfkit.from_url('http://google.com', 'out.pdf', options=options)
默认情况下, PDFKit 将会显示所有的 wkhtmltopdf 输出. 如果不想看到这些信息,你需要传递一个 quiet 选项:
options ={'quiet': ''}
pdfkit.from_url('google.com', 'out.pdf', options=options)
由于wkhtmltopdf的命令语法 , TOC 和 Cover 选项必须分开指定:
toc ={'xsl-style-sheet': 'toc.xsl'}
cover= 'cover.html'pdfkit.from_file('file.html', options=options, toc=toc, cover=cover)
当我们转换文件、或字符串的时候,可以通过css选项指定扩展的 CSS 文件。
#单个 CSS 文件
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中的meta tags传递任意选项:
body = """
Hello World!
"""pdfkit.from_string(body,'out.pdf') #with --page-size=Legal and --orientation=Landscape