查看原文
其他

使用 Python 制作属于自己的 PDF 电子书

编程派 2020-09-13

之前介绍过一篇如何将 PDF 转换为图片的文章(点击这里查看),今天分享的则是如何将 HTML 保存为 PDF。


熟练利用好这招,可以试着自己把精品网页内容做成 PDF 电子书。


作者:taceywong

来源:http://www.cnblogs.com/taceywong/p/5643978.html

主要使用的是wkhtmltopdf的Python封装——pdfkit

安装

1. Install python-pdfkit:

  1. pip install pdfkit

2. Install wkhtmltopdf:

  1. sudo apt-get install wkhtmltopdf

  2. sudo yum intsall wkhtmltopdf

  3. brew install Caskroom/cask/wkhtmltopdf

使用

一个简单的例子:

  1.    import pdfkit

  2.    pdfkit.from_url('http://google.com', 'out.pdf')

  3.    pdfkit.from_file('test.html', 'out.pdf')

  4.    pdfkit.from_string('Hello!', 'out.pdf')

你也可以传递一个url或者文件名列表:

  1.    pdfkit.from_url(['google.com', 'yandex.ru', 'engadget.com'], 'out.pdf')

  2.    pdfkit.from_file(['file1.html', 'file2.html'], 'out.pdf')

也可以传递一个打开的文件:

  1.    with open('file.html') as f:

  2.        pdfkit.from_file(f, 'out.pdf')

如果你想对生成的PDF作进一步处理, 你可以将其读取到一个变量中:

  1.    # 设置输出文件为False,将结果赋给一个变量

  2.    pdf = pdfkit.from_url('http://google.com', False)

你可以制定所有的 wkhtmltopdf 选项. 你可以移除选项名字前面的 '--' .如果选项没有值, 使用 None, Falseor * 作为字典值:

  1.    options = {

  2.        'page-size': 'Letter',

  3.        'margin-top': '0.75in',

  4.        'margin-right': '0.75in',

  5.        'margin-bottom': '0.75in',

  6.        'margin-left': '0.75in',

  7.        'encoding': "UTF-8",

  8.        'no-outline': None

  9.    }

  10.    pdfkit.from_url('http://google.com', 'out.pdf', options=options)

默认情况下, PDFKit 将会显示所有的 wkhtmltopdf 输出. 如果你不想看到这些信息,你需要传递一个 quiet 选项:

  1.    options = {

  2.        'quiet': ''

  3.        }

  4.    pdfkit.from_url('google.com', 'out.pdf', options=options)

由于wkhtmltopdf的命令语法 , TOC 和 Cover 选项必须分开指定:

  1.    toc = {

  2.        'xsl-style-sheet': 'toc.xsl'

  3.    }

  4.    cover = 'cover.html'

  5.    pdfkit.from_file('file.html', options=options, toc=toc, cover=cover)

当你转换文件、或字符串的时候,你可以通过css选项指定扩展的 CSS 文件。

  1.    # 单个 CSS 文件

  2.    css = 'example.css'

  3.    pdfkit.from_file('file.html', options=options, css=css)

  4.    # Multiple CSS files

  5.    css = ['example.css', 'example2.css']

  6.    pdfkit.from_file('file.html', options=options, css=css)

你也可以通过你的HTML中的meta tags传递任意选项:

  1.    body = """

  2.          Hello World!

  3.        """

  4.    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里面):

  1.    config = pdfkit.configuration(wkhtmltopdf='/opt/bin/wkhtmltopdf'))

  2.    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中加入**

下面是我随便写的一个HTML表格:

  1. <html>

  2. <head><meta charset="UTF-8"></head>

  3. <body>

  4. <table width="400" border="1">

  5. <tr>

  6.  <th align="left">Item....</th>

  7.  <th align="right">1</th>

  8. </tr>

  9. <tr>

  10.  <td align="left">衣服</td>

  11.  <td align="right">$241.10</td>

  12. </tr>

  13. <tr>

  14.  <td align="left">化妆品</td>

  15.  <td align="right">$30.00</td>

  16. </tr>

  17. <tr>

  18.  <td align="left">食物</td>

  19.  <td align="right">$730.40</td>

  20. </tr>

  21. <tr>

  22.  <th align="left">tOTAL</th>

  23.  <th align="right">$1001.50</th>

  24. </tr>

  25. </table>

  26. </body>

  27. </html>

下面是生成的PDF截图 


另也可直接使用:https://pdfcrowd.com/#convertbyinput


题图:pexels,CC0 授权。

点击阅读原文,查看更多 Python 教程和资源。

    您可能也对以下帖子感兴趣

    文章有问题?点此查看未经处理的缓存