python把网页保存成pdf_Python实现将HTML转成PDF的方法分析

本文实例讲述了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 选项 . 你可以移除选项名字前面的 '--' .如果选项没有值, 使用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

配置

每个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中加入

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

Item....1
衣服$241.10
化妆品$30.00
食物$730.40
tOTAL$1001.50

下面是生成的PDF截图

希望本文所述对大家Python程序设计有所帮助。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Python中的多个库来实现将爬取到的网页内容转换PDF格式并保存。以下是一种可能的实现方法,使用了`pdfkit`和`BeautifulSoup`库: 首先,确保你已经安装了`pdfkit`和`BeautifulSoup`库。可以使用以下命令进行安装: ``` pip install pdfkit beautifulsoup4 ``` 接下来,你需要安装一个HTMLPDF的工具,例如`wkhtmltopdf`。你可以在`wkhtmltopdf`的官方网站上下载并安装适合你操作系统的版本。 安装完后,你可以使用以下代码将爬取到的网页内容转换PDF保存: ```python import pdfkit from bs4 import BeautifulSoup import requests # 爬取网页内容 url = 'https://www.example.com' # 替换你要爬取的网页URL response = requests.get(url) html_content = response.content # 解析HTML内容 soup = BeautifulSoup(html_content, 'html.parser') # 将HTML内容保存为临时文件 with open('temp.html', 'w', encoding='utf-8') as f: f.write(str(soup)) # 将临时文件转换为PDF保存 pdfkit.from_file('temp.html', 'output.pdf') # 删除临时文件 os.remove('temp.html') ``` 在上面的代码中,我们首先使用`requests`库获取网页的内容,然后使用`BeautifulSoup`库解析HTML内容。接下来,我们将HTML内容保存为临时文件`temp.html`,然后使用`pdfkit`库将临时文件转换为PDF格式并保存为`output.pdf`。最后,我们删除临时文件。 请注意,使用`pdfkit`进行HTMLPDF时,你需要提前安装并配置好`wkhtmltopdf`工具。具体安装和配置方式可以参考`wkhtmltopdf`的官方文档。 希望这个例子能帮到你!如果还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值