pythonpython-2.7mechanize-pythonpython-pdfkit

How to get cookies from mechanize to use in pdfkit


Does anyone know how to get a cookie that I have logged into using mechanize browser, so that I can use those urls in pdfkit? I have looked around a bit, but can't manage to see how to get a cookie in the right format and pass it to pdfkit.from_url.


Solution

  • wkhtmltopdf support set cookie with --cookie option, extract cookies from mechanize browser, pass to wkhtmltopdf with argument options of pdfkit.from_url:

    cj = br.cookiejar # assume `br` is a `browser` intance
    ops = {'cookie': [(c.name, urllib.quote(c.value)) for c in cj]}
    pdfkit.from_url('url', options=ops)