我想使用
PDF
(正在使用 pdfkit
)将网页转换为 wkhtmltopdf
,但是我也无法从网页获取图片。
这是我的代码:
import pdfkit
config = pdfkit.configuration(wkhtmltopdf='C:/Program Files/wkhtmltopdf/bin/wkhtmltopdf.exe')
options = {
# Allow access to local files (images)
'enable-local-file-access': None,
# Do not disable the inclusion of images
'no-images': None
}
pdfkit.from_url(
'https://stadt-bremerhaven.de/eve-flare-portables-stimmungslicht-mit-thread-ausprobiert/',
'out.pdf',
configuration=config,
options=options,
verbose=True
)
文章中的图片未包含在生成的
PDF
文件中。有没有办法也包含它们,或者也许有另一个库可以做到这一点?
无头打印远程站点存在几个问题,但是最好直接使用命令行引擎,因此在这种情况下
wkhtmltopdf.exe https://stadt-bremerhaven.de/eve-flare-portables-stimmungslicht-mit-thread-ausprobiert/ out.pdf
看到有问题的输出并不罕见,因为 Wkhtml 无法直接接受 cookie,而且问题是原始无头运行是非交互式的。
有一些方法可以解决这个问题,即从浏览器收集 cookie 并应用于 wkhtml
wkhtmltopdf.exe --cookie "__cmpconsentx47085" "CPuoyhgPuoyhgAfQ9BENDNCgAP_AAH_AAAigJSkR5D5MDWFBWX57QMskWYUX0MAVZyADChaAAaABCDAAcKQAkkEaIAyAAAACAQgAIBYBAAAADAlAAEAQQIhBAAHgAgAEoBAIIAAEABERQUIAAAoKAIgAEAAIAAExKECAkALQAobiREAAkIAiQIAAgAAAAIABAhMAAAAIAAACAAIAAACAAAAAAAAAAAACABAAAAAAAAAAIJSkR5D5MDWFBWX57QMskWYUX0MAVZyADChaAAaABCDAAcKQAkkEaIAyAAAACAQgAIBYBAAAADAlAAEAQQIhBAAHgAgAEoBAIIAAEABERQUIAAAoKAIgAEAAIAAExKECAkALQAobiREAAkIAiQIAAgAAAAIABAhMAAAAIAAACAAIAAACAAAAAAAAAAAACABAAAAAAAAAAIAA" --cookie "__cmpcccx47085" "aBPuqTTPgAACgALAAuABoAEoKWwAAA" https://stadt-bremerhaven.de/eve-flare-portables-stimmungslicht-mit-thread-ausprobiert/ out.pdf
提供此输出
我对 7 页输出的集中程度感到惊讶,因为替代方案是运行无头边缘,然后包括所有广告侧边栏等,但通过首先接受 cookie 更容易无头运行。
"C:\Program Files\Microsoft\Edge\Application\msedge.exe" --headless --print-to-pdf="%cd%\out.pdf" --enable-logging --no-pdf-header-footer --run-all-compositor-stages-before-draw "https://stadt-bremerhaven.de/eve-flare-portables-stimmungslicht-mit-thread-ausprobiert/"
注意,您的“程序文件”位置可能因语言或 64 位而异,因此请检查您使用的是哪一个
\Program Files (86)\
结果应该更符合浏览器查看器的情况。
如果您向下卷曲 html
curl -o get.html "https://stadt-bremerhaven.de/eve-flare-portables-stimmungslicht-mit-thread-ausprobiert/"
,当然您可以在打印之前根据需要更改 html。