Python使用pdfkit将HTML转换为PDF的操作指南

程一诺
程一诺
发布时间:2026-10-16| 4人浏览过
原创

本文详细介绍Python中pdfkit库的使用,该库封装了wkhtmltopdf工具实现HTML到PDF的转换。内容涵盖pdfkit与wkhtmltopdf的安装步骤(Windows、Ubuntu、CentOS),代码示例演示如何通过URL或字符串生成PDF文件,并重点解决“No wkhtmltopdf executable found”报错,提供路径配置与环境变量设置方案。

Python包pdfkit(wkhtmltopdf) 将HTML转换为PDF的操作方法

什么是pdfkit

pdfkit是一个用于将HTML和CSS格式文件转换为PDF格式文档的工具。它本质上是html转pdf工具包wkhtmltopdf的Python封装接口。因此,使用pdfkit之前必须手动安装wkhtmltopdf底层工具。

安装

首先需要安装pdfkit库,执行pip install pdfkit命令即可。此外,还需要安装wkhtmltopdf工具,因为pdfkit仅作为接口调用该底层工具进行实际转换。本文基于python 3.x版本,在命令行中进行操作。

$sudo apt-get install wkhtmltopdf

工具下载地址:wkhtmltopdf官网:https://wkhtmltopdf.org/downloads.html

Ubuntu系统可以直接使用以下命令安装:

$sudo yum intsall wkhtmltopdf

CentOS系统可以直接使用以下命令安装:

$sudo yum intsall wkhtmltopdf

使用

将url生成pdf文件

如果不指定wkhtmltopdf的具体路径,程序会从系统的默认执行路径下查找wkhtmltopdf。

import pdfkit
'''将url生成pdf文件'''
def url_to_pdf(url, to_file):
    pdfkit.from_url(url, to_file,verbose=True)
url_to_pdf('http://www.baidu.com','out_3.pdf')

如果需要指定wkhtmltopdf的位置,可以通过configuration参数传入路径。

import pdfkit
'''将url生成pdf文件'''
def url_to_pdf(url, to_file):
    config = pdfkit.configuration(wkhtmltopdf='/usr/local/bin/wkhtmltopdf')
    pdfkit.from_url(url, to_file,configuration=config,verbose=True)
url_to_pdf('http://www.baidu.com','out_3.pdf')

字符串生成pdf【pdfkit.from_string()函数】

# 导入库
import pdfkit

'''将字符串生成pdf文件'''
def str_to_pdf(string, to_file):
    # 将wkhtmltopdf.exe程序绝对路径传入config对象
    path_wkthmltopdf = r'C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe'
    config = pdfkit.configuration(wkhtmltopdf=path_wkthmltopdf)
    # 生成pdf文件,to_file为文件路径
    pdfkit.from_string(string, to_file, configuration=config)
    print('完成')
str_to_pdf('This is test!','out_3.pdf')

报错

报错OSError: No wkhtmltopdf executable found

在使用pdfkit.from_string、pdfkit.from_file或pdfkit.from_url将字符串、文件或网页内容转化为PDF时,可能会遇到以下报错:

OSError: No wkhtmltopdf executable found

原因很明显,就是没找到可执行的wkhtmltopdf文件,也就是未找到wkhtmltopdf.exe文件。python的pdfkit扩展包使用时需要基于wkhtmltopdf.exe这个可执行文件才可运行,因此需要先安装wkhtmltopdf。对于windows系统,可以在(https://wkhtmltopdf.org/downloads.html)下载安装,然后将该程序的执行文件路径添加到环境变量中(这样即可直接用pdfkit扩展包,否则需要在使用pdfkit时,指明该程序的路径)

Ubuntu系统可以直接使用以下命令安装:

$sudo apt-get install wkhtmltopdf

CentOS系统可以直接使用以下命令安装:

$sudo yum intsall wkhtmltopdf

到此这篇关于python包pdfkit(wkhtmltopdf) 将HTML转换为PDF的文章就介绍到这了,更多相关python pdfkit将HTML转换为PDF内容请搜索本站以前的文章或继续浏览下面的相关文章希望大家以后多多支持本站!

相关标签:

免责声明:本文内容仅供参考,不代表本站观点。转载或引用请注明出处。

Python 相关产品和应用

全部下载
Selenium

Selenium

4.51.0 · Windows

用于浏览器自动化和 Web 测试的 Python 工具。

scikit-learn

scikit-learn

1.9.1 · Windows

常用 Python 机器学习算法与模型评估工具库。

Black

Black

26.10.1 · Windows

强调一致性的 Python 自动代码格式化工具。

Ruff

Ruff

0.17.1 · macOS

由 Rust 编写的高速 Python 代码检查和格式化工具。

pytest

pytest

9.1.1 · Windows

简洁而强大的 Python 自动化测试框架。

FastAPI

FastAPI

0.146.0 · Windows

高性能 Python API 框架,支持类型提示和自动接口文档。

Flask

Flask

3.1.3 · Windows

轻量灵活的 Python Web 应用框架。

Django

Django

6.1.2 · Windows

成熟的 Python Web 框架,适合构建内容站点和业务系统。

Spyder

Spyder

6.1.7 · Windows

面向科学计算与数据分析的 Python 集成开发环境。

SciPy

SciPy

1.18.1 · Windows

面向科学、工程和技术计算的 Python 算法库。