首页 > 解决方案 > 如何在 python 中使用 pdfkit 的 print-media-type 选项

问题描述

使用 pdfkit 打印时,我想在文档上打印水印。

使用浏览器手动打印时ctrl+p,会显示水印,但使用 pdfkit 打印时不显示。

我的假设是 pdfkit 完全按照它在显示器上呈现的方式打印文档,并通过print-media-type在 pdfkit 的选项中设置标志来使 pdfkit 打印文档的方式与浏览器打印它的方式完全相同。

根据https://wkhtmltopdf.org/usage/wkhtmltopdf.txt文档,

--print-media-type              Use print media-type instead of screen
--no-print-media-type           Do not use print media-type instead of screen (default)

pdfkit的选项,来自我的功能:

        _options = {
            'cookie': [
                ('csrftoken', options.get('csrftoken','none')),
                ('sessionid', options.get('session_key','none')),
            ],
            'footer-center': 'Page [page] of [topage]',
            'footer-right': DOC_VERSION.get(doctype,''),
            'footer-font-size': '9',
            'print-media-type': ,
        }

我尝试了以下组合,但没有一个有效:

'print-media-type': 'True',
'print-media-type': True,
'print-media-type': '',

问题:1.我的假设是否正确?如果是,2.我如何设置这个标志?

标签: pythonpdfkit

解决方案


这对我有用:

options = {
  "disable-local-file-access": None,
  "enable-local-file-access": None,
  "print-media-type": None
}

pdfkit.from_file('index.html', 'out.pdf', options=options)

推荐阅读