Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use pdfkit's print-media-type option in python

Tags:

python

pdfkit

I want to print watermark on the document when printed using pdfkit.

when manually printing from browser using ctrl+p ,the watermark is shown but not when printed using pdfkit.

My assumption is that pdfkit prints the document exactly as it is rendered on the monitor and to make pdfkit print the document exactly like how a browser would print it, would by settings the print-media-type flag in pdfkit's options.

As per https://wkhtmltopdf.org/usage/wkhtmltopdf.txt documentation,

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

options for pdfkit, from my function:

        _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': ,
        }

I've tried following combinations but none of them worked :

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

question : 1. is my assumption correct ? if yes, 2. how do i set this flag ?

like image 568
Irfan Harun Avatar asked Jan 31 '26 03:01

Irfan Harun


1 Answers

This worked for me:

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

pdfkit.from_file('index.html', 'out.pdf', options=options)
like image 67
Ha21iko Avatar answered Feb 02 '26 15:02

Ha21iko