station
station

Reputation: 7145

Python pass request headers to pdfkit

I am trying to convert a URL to pdf using pdfkit in python as follows.

import pdfkit
pdfkit.from_url(url, file_path)

I wanted to know is there some way to pass custom request headers with this URL such as X-Proxy-REMOTE-USER to something.

Upvotes: 2

Views: 2764

Answers (1)

Chris Haas
Chris Haas

Reputation: 55427

python-pdfkit is just a wrapper around wkhtmltopdf. Looking at the fifth example from the usage section of the docs there's a third parameter that you can specify for additional options:

options = {
    'page-size': 'Letter',
    'margin-top': '0.75in',
    'margin-right': '0.75in',
    'margin-bottom': '0.75in',
    'margin-left': '0.75in',
    'encoding': "UTF-8",
    'no-outline': None
}

pdfkit.from_url('http://google.com', 'out.pdf', options=options)

The options are specified here and you are specifically interest in --custom-header <name> <value>. Unfortunately they don't say how to pass an option that takes multiple parameters but since the command line wants a space between the two and looking at the code they don't appear to be changing the value parameter I'd try just passing the name and value as the value of the option with a space between the two.

options = {
    'custom-header': 'X-Proxy-REMOTE-USER STEVE'
}

pdfkit.from_url('http://google.com', 'out.pdf', options=options)

Upvotes: 1

Related Questions