如何将带有图像的微信公众号文章转换为PDF



对于一般的html,当使用pdfkit将html转换为pdf时,html中的图像可以保存在pdf中。

而对于微信公众号文章,我发现URL中的图像丢失了。 以下代码是一个实例。

如何将带有图片的微信公众号文章保存为PDF?

import pdfkit
url='https://mp.weixin.qq.com/s?__biz=MzA3NDMyOTcxMQ==&mid=2651249314&idx=1&sn=5338576a80a4145b9808ff06cc980c14'
path_wkthmltopdf = 'C:/Anaconda3/Lib/site-packages/wkhtmltopdf/bin/wkhtmltopdf.exe'
pdfkit.from_url(url=url,output_path='c:/test.pdf',configuration=pdfkit.configuration(wkhtmltopdf=path_wkthmltopdf))'

我认为解决方案之一是向下滚动 url 以加载所有图像,然后将其转换为 pdf。 如何向下滚动以加载pdfkit中的所有图像?

以下内容应该在不修改窗口环境变量的情况下工作:

import pdfkit
path_wkthmltopdf = r'C:Python27wkhtmltopdfbinwkhtmltopdf.exe'
config = pdfkit.configuration(wkhtmltopdf=path_wkthmltopdf)
url = 'https://mp.weixin.qq.com/s?timestamp=1515570589&src=3&ver=1&signature=xsZdozV1JPS2K8SuXJ8TKeqfuczP2z78*LCVu32ljt1NSa8oF41X88W0JYguTbLUwHHyt0ksUy8l9ljM5*uGOSH-GBjlVipz4a1aIeg9xNQgwlxuCV*9dURcg-U8UvR78C2RV6B5CIeA0n1jIaiFiqrQTIuel5IW-HYAcQsOT0g='
pdfkit.from_url(url, "out.pdf", configuration=config)

假设路径是正确的(例如,在我的例子中它是r'C:\Program Files (x86(\wkhtmltopdf\bin\wkhtmltopdf.exe'(。

结果:

Loading page (1/2)
Printing pages (2/2)                                               
Done   

PDF链接

最新更新