pdfkit||利用python实现网页转PDF方法总结

Python的第三方库pdfkit,可以将网页、html文件以及字符串生成pdf文件。
安装相关依赖库

1.python版本 3.x,在命令行输入pip install pdfkit
2.安装wkhtmltopdf.exe文件
下载地址:wkhtmltopdf
选择自己电脑的符合版本
在这里插入图片描述
下载完成后,一路next,将wkhtmltopdf安装好。务必要记住安装地址,找到wkhtmltopdf.exe文件所在的绝对路径,后面要用到。

使用pdfkit库生成pdf文件

前面说过pdfkit可以将网页、html文件、字符串生成pdf文件。

  • 网页url生成pdf【pdfkit.from_url()函数】
# 导入库
import pdfkit

'''将网页url生成pdf文件'''
def url_to_pdf(url, 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_url(url, to_file, configuration=config)
    print('完成')

# 这里传入我知乎专栏文章url,转换为pdf
url_to_pdf(r'https://blog.csdn.net/weixin_38924500/article/details/104767891', 'name.pdf')  #第二个为文件地址及命名   ./PDF/文件名.pdf
  • html文件生成pdf【pdfkit.from_file()函数】
# 导入库
import pdfkit

'''将html文件生成pdf文件'''
def html_to_pdf(html, 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_file(html, to_file, configuration=config)
    print('完成')

html_to_pdf('sample.html','name.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')
在项目中灵活应用pdfkit
url = " www.baidu.com"
title= "mypdf"
# print(url)
file_name = './PDF/'+title+'.pdf'  #定义相对路径存储文件,注意使用正斜杠,否则会出现转义问题。
# print(file_name)
myconfig = pdfkit.configuration(wkhtmltopdf='C:/Program Files/wkhtmltopdf/bin/wkhtmltopdf.exe')
#对打印的pdf进行设置,具体参数根据需要添加
options = {
			'page-size': 'A4',
			'margin-top': '0mm',
			'margin-right': '0mm',
			'margin-bottom': '0mm',
			'margin-left': '0mm',
			# 'orientation':'Landscape',#横向
			'encoding': "UTF-8",
			'no-outline': None,
			'footer-right':'[page]' #设置页码
}

pdfkit.from_url(url, file_name, options=options, configuration=myconfig)

多个请求造成浏览器堵塞问题

我们经常会需要将多个链接地址放进去解析pdf,但是需要一定的时间,如果上一个链接的请求打印没有结束,然后程序又开始请求下一条链接,就会造成问题。
所以我们可以使用

import time
try:
   #请求地址、解析程序
   ,,,,
excep:
     print('-------------xxxxx-----------')
                time.sleep(20)  #让进程睡眠一会,防止出现请求堵塞
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值