需要用到两个东西,一个pdfkit 的 python 依赖,另一个是名为wkhtmltopdf的软件,
ps:我感觉这个软件比一些脚本/依赖好用很多,而且这个软件有mac版本,windows版本,linux版本,所以这个软件用于生产环境都可以,不过这种软件一般产生一定的内存消耗,一定要考虑服务器性能酌情使用
import pdfkit
def url_to_pdf(url, to_file):
# 将wkhtmltopdf.exe程序绝对路径传入config对象
path_wkthmltopdf = r'C:Program Fileswkhtmltopdfinwkhtmltopdf.exe'
config = pdfkit.configuration(wkhtmltopdf=path_wkthmltopdf)
# 生成pdf文件,to_file为文件路径
pdfkit.from_url(url, to_file, configuration=config)
print('完成')
url_to_pdf(r'https://zhuanlan.zhihu.com/p/69869004', 'out_1.pdf')