George Orwell
George Orwell

Reputation: 303

How to send cookies with PDFKit in Python?

I'm trying to download some pages as PDF files. However, the pages require me to log in, so I simply sent some cookies along with my request (using the requests module). This worked. However, I'm not sure how to send cookies with PDFKit to achieve the same thing.

Here is the code I tried. I also tried to incorperate headers (to prevent a 403 error), but it didn't work. I can't seem to find this in the documentation, either. Does anyone know how I can send cookies to download the pages?

import pdfkit

url = r'www.someurl.com'

cookies = {
            "cookie1": "cookie"
          }

headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.76 Safari/537.36'}

config = pdfkit.configuration(wkhtmltopdf="C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe")

pdfkit.from_url(url, 'out.pdf', configuration=config, options=cookies)

Upvotes: 2

Views: 1334

Answers (1)

Eduard Ilyasov
Eduard Ilyasov

Reputation: 3308

According to the PDFkit project description you can set cookies using this approach:

options = {'cookie': [('cookie-name1', 'cookie-value1'),
                      ('cookie-name2', 'cookie-value2')]}

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

Upvotes: 2

Related Questions