要使用wkhtmltopdf生成具有可选择和可搜索文本的PDF,请按照以下步骤操作:
import subprocess
html_file = 'input.html'
pdf_file = 'output.pdf'
command = f'wkhtmltopdf {html_file} {pdf_file}'
subprocess.run(command, shell=True)
import subprocess
html_file = 'input.html'
pdf_file = 'output.pdf'
command = f'wkhtmltopdf --print-media-type --javascript-delay 5000 --no-background {html_file} {pdf_file}'
subprocess.run(command, shell=True)
在这个示例中,我们添加了以下参数:
--print-media-type
:将CSS媒体类型设置为“print”,以确保正确的页面布局。--javascript-delay
:指定在生成PDF之前等待的毫秒数,以确保JavaScript执行完成。--no-background
:不要在PDF中包含背景图像。请注意,生成可选择和可搜索文本的PDF可能会导致生成速度变慢,因为wkhtmltopdf需要渲染整个页面。根据您的需求和网页复杂性,生成时间可能会有所不同。
领取专属 10元无门槛券
手把手带您无忧上云