官网https://wkhtmltopdf.org/
wkhtmltopdf是一个开源项目,用于将HTML文件或网页转换为PDF格式。它基于WebKit引擎,可以将网页的内容、样式和布局准确地转换为PDF文档。wkhtmltopdf支持多种操作系统,包括Windows、Linux和Mac OS。它可以通过命令行参数进行配置,以满足不同的转换需求。这个工具在许多场景中被广泛应用,例如生成报告、生成电子书、批量转换网页等,下面将使用Python结合wkhtmltopdf实现转换功能。
首先下载并安装一下wkhtmltopdf
这里的安装目录我选择放在E:wkhtmltopdfbinwkhtmltopdf.exe
在使用Python写代码之前我们需要安装pdfkit库(这是一个对wkhtmltopdf进行封装的Python库),通过它来调用wkhtmltopdf命令实现各种转换需求。首先安装pdfkit库:
pip install pdfkit
Html转PDF工具代码:
import tkinter as tk
from tkinter import filedialog, messagebox
import pdfkit
import tkinter.font as tkFont
from tkinter import ttk
def select_wkhtmltopdf():
global path_wkthmltopdf
path_wkthmltopdf = filedialog.askopenfilename(title="选择 wkhtmltopdf.exe")
def select_save_directory():
global save_directory
save_directory = filedialog.askdirectory(title="选择保存目录")
def generate_pdf():
url = url_entry.get()
save_path = save_directory + "/output.pdf"
config = pdfkit.configuration(wkhtmltopdf=path_wkthmltopdf)
try:
pdfkit.from_url(url, save_path, configuration=config)
messagebox.showinfo("成功", "PDF 生成成功!")
except Exception as e:
messagebox.showerror("错误", "生成PDF时出现错误:" + str(e))
root = tk.Tk()
root.title("Html转PDF工具---微信公众号:蓝胖子之家")
root.geometry("500x250")
# 设置中文字体
font_style = tkFont.Font(family="微软雅黑", size=12)
# 创建样式
style = ttk.Style()
style.configure("TButton", font=font_style, padding=6)
style.configure("TLabel", font=font_style, padding=6)
style.configure("TFrame", background="#f0f0f0")
# 设置主题样式
style.theme_use("clam")
# 选择 wkhtmltopdf.exe 按钮
select_wkhtmltopdf_button = ttk.Button(root, text="选择 wkhtmltopdf.exe", command=select_wkhtmltopdf, style="TButton")
select_wkhtmltopdf_button.pack()
# URL 输入框
url_label = ttk.Label(root, text="URL:", style="TLabel")
url_label.pack()
url_entry = ttk.Entry(root)
url_entry.pack()
# 选择保存目录按钮
select_save_directory_button = ttk.Button(root, text="选择保存目录", command=select_save_directory, style="TButton")
select_save_directory_button.pack()
# 生成 PDF 按钮
generate_pdf_button = ttk.Button(root, text="生成 PDF", command=generate_pdf, style="TButton")
generate_pdf_button.pack()
root.mainloop()
-
select_wkhtmltopdf_button = ttk.Button(root, text="选择 wkhtmltopdf.exe", command=select_wkhtmltopdf, style="TButton")
:创建了一个按钮对象,显示文本为"选择 wkhtmltopdf.exe",点击按钮时会执行select_wkhtmltopdf
函数,该函数用于选择wkhtmltopdf.exe文件。 -
select_save_directory_button = ttk.Button(root, text="选择保存目录", command=select_save_directory, style="TButton")
:创建了一个按钮对象,显示文本为"选择保存目录",点击按钮时会执行select_save_directory
函数,该函数用于选择保存PDF文件的目录。 -
generate_pdf_button = ttk.Button(root, text="生成 PDF", command=generate_pdf, style="TButton")
:创建了一个按钮对象,显示文本为"生成 PDF",点击按钮时会执行generate_pdf
函数,该函数用于生成PDF文件。 -
url_entry = ttk.Entry(root)
:创建了一个输入框对象,用于用户输入URL。 -
pdfkit.from_url(url, save_path, configuration=config)
:使用pdfkit库的from_url
函数将指定URL的网页转换为PDF文件,并保存在指定的保存路径中。 -
messagebox.showinfo("成功", "PDF 生成成功!")
:使用messagebox模块的showinfo
函数弹出一个信息框,显示"PDF 生成成功!"的提示信息。 -
messagebox.showerror("错误", "生成PDF时出现错误:" + str(e))
:使用messagebox模块的showerror
函数弹出一个错误框,显示"生成PDF时出现错误:"加上具体的错误信息。
使用方法:
当您首次运行工具时候,您需要选择你安装wkhtmltopdf.exe程序的位置(例如我的位置是:E:wkhtmltopdfbinwkhtmltopdf.exe)
请按照以下步骤操作:
-
点击"选择 wkhtmltopdf.exe"按钮,选择您安装的wkhtmltopdf.exe文件。这是一个用于将网页转换为PDF的工具。如果您还没有安装wkhtmltopdf,请先下载并安装它。
2.选择完毕后,程序将自动保存您选择的wkhtmltopdf.exe文件位置到配置文件config.ini
中。
下次打开应用程序时,它将自动加载上次保存的配置,无需再次选择wkhtmltopdf.exe文件。
如果您想更改wkhtmltopdf.exe文件的位置,只需再次点击"选择 wkhtmltopdf.exe"按钮,选择新的文件即可。程序将自动更新配置文件中的位置。
在选择完wkhtmltopdf.exe文件后,您可以按照以下步骤生成PDF文件:
-
在URL输入框中,输入您要转换为PDF的网页的URL。
-
点击"选择保存目录"按钮,选择您要保存生成的PDF文件的目录。
-
点击"生成 PDF"按钮,程序将使用选择的wkhtmltopdf.exe文件和URL生成PDF文件,并将其保存到选择的保存目录中。
-
如果PDF生成成功,将显示一个提示框,生成成功。
运行效果:
后台回复html转pdf获取文章工具
原文始发于微信公众号(蓝胖子之家):Python实现HTML转PDF
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论