使用Python将HTML转成PDF
主要使用的是wkhtmltopdf的Python封装——pdfkit
安装
1. Install python-pdfkit:
$ pip install pdfkit
2. Install wkhtmltopdf:
- Debian/Ubuntu:
$ sudo apt-get install wkhtmltopdf
- Redhat/CentOS
sudo yum intsall wkhtmltopdf
- MacOS
brew install Caskroom/cask/wkhtmltopdf
使用
一个简单的例子:
import pdfkit
pdfkit.from_url('http://google.com', 'out.pdf')
pdfkit.from_file('test.html', 'out.pdf')
pdfkit.from_string('Hello!', 'out.pdf')
你也可以传递一个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.from_url('http://google.com', False)
你可以制定所有的 wkhtmltopdf 选项 <http://wkhtmltopdf.org/usage/wkhtmltopdf.txt>
. 你可以移除选项名字前面的 '--' .如果选项没有值, 使用None, False
or *
作为字典值:
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 = """
<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调用都有一个可选的参数。这应该是pdfkit.configuration()
API 调用的一个实例. 采用configuration 选项作为初始化参数。可用的选项有:
wkhtmltopdf
——wkhtmltopdf
二进制文件所在的位置。默认情况下pdfkit
会尝试使用which
(在类UNIX系统中) 或where
(在Windows系统中)来判断.meta_tag_prefix
--pdfkit
的前缀指定 meta tags(元标签) - 默认情况是pdfkit-
示例 :针对wkhtmltopdf
不在系统路径中(不在$PATH
里面):
config = pdfkit.configuration(wkhtmltopdf='/opt/bin/wkhtmltopdf'))
pdfkit.from_string(html_string, output_file, configuration=config)
问题
IOError: 'No wkhtmltopdf executable found'
:
确保 wkhtmltopdf 在你的系统路径中($PATH
), 会通过 configuration进行了配置 (详情看上文描述)。 在Windows系统中使用where wkhtmltopdf
命令 或 在 linux系统中使用 which wkhtmltopdf
会返回 wkhtmltopdf二进制可执行文件所在的确切位置.
IOError: 'Command Failed'
如果出现这个错误意味着 PDFKit不能处理一个输入。你可以尝试直接在错误信息后面直接运行一个命令来查看是什么导致了这个错误 (某些版本的 wkhtmltopdf会因为段错误导致处理失败)
正常生成,但是出现中文乱码
确保两项:
1)、你的系统中有中文字体
2)、在html中加入<meta charset="UTF-8">
下面是我随便写的一个HTML表格:
<html>
<head><meta charset="UTF-8"></head>
<body>
<table width="400" border="1">
<tr>
<th align="left">Item....</th>
<th align="right">1</th>
</tr>
<tr>
<td align="left">衣服</td>
<td align="right">$241.10</td>
</tr>
<tr>
<td align="left">化妆品</td>
<td align="right">$30.00</td>
</tr>
<tr>
<td align="left">食物</td>
<td align="right">$730.40</td>
</tr>
<tr>
<th align="left">tOTAL</th>
<th align="right">$1001.50</th>
</tr>
</table>
</body>
</html>
下面是生成的PDF截图
另:https://pdfcrowd.com/#convert_by_input
最新文章
- android键盘
- Python学习日志(二)
- mybatis报错invalid types () or values ()解决方法
- HDU 1222
- BOM 和DOM
- Zend studio 10.6 配置XDEBUG
- IOS基础面试题
- A potentially dangerous Request.Form value was detected from the client
- javascript:void(0)与#整理
- 12、SQL基础整理(运算符与优先级)
- 一机运行多个resin的配置
- 聚集索引、非聚集索引、聚集索引组织表、堆组织表、Mysql/PostgreSQL对比、联合主键/自增长、InnoDB/MyISAM(引擎方面另开一篇)
- 部署在IIS服务器的asp.net 网站,禁止访问指定类型文件
- The _imageingft C module is not installed
- cf D. Xenia and Hamming
- OpenCV——常用函数查询
- ReactiveCocoa &; FRP &; MVVM
- Lists.newArrayListWithExpectedSize( int estimatedSize)
- Linux ip forward
- 判断一个正整数是否是2的N次方的简洁算法及其证明
热门文章
- 图解js中常用的判断浏览器窗体、用户屏幕可视区域大小位置的方法
- AlwaysON同步的原理及可用模式
- 基于java的设计模式入门(1)——为什么要学习设计模式
- java 锁3
- Linux内核TCP/IP参数分析与调优
- Java-面向对象基础练习
- PHP面向对象07_PDO
- Atitit usrqbg1821 Tls 线程本地存储(ThreadLocal Storage&#160;规范标准化草案解决方案ThreadStatic
- Atitit usbQb212 oo 面向对象封装的标准化与规范解决方案java c# php js
- 为什么项目的jar包会和tomcat的jar包冲突?