Create PDF of a https webpage which requires login using pdfkit

*爱你&永不变心* 提交于 2019-12-24 07:57:58

问题


I am trying to generate a PDF of a webpage from a https website (Salesforce). I have so far tried using simple_salesforce, which returns a sessionID (cookie) to no avail.

from simple_salesforce import Salesforce
import pdfkit

sf = Salesforce(username='my username'
            ,password='my password'
            ,security_token= 'my API security token')

path_wkthmltopdf = r'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe'
config = pdfkit.configuration(wkhtmltopdf=bytes(path_wkthmltopdf, 'utf8'))
options1 = {
    'page-size': None,
    'margin-top': None,
    'margin-right': None,
    'margin-bottom': None,
    'margin-left': None,
    'encoding': None,
    'custom-header' : None, 
    'cookie': sf.session_id,
    'no-outline': None
}
pdfkit.from_url('https://thiess.my.salesforce.com/0069000000IZH71','out.pdf', 
                configuration=config, options=options1)

Anyone knows what's the best way to pass a cookie argument into pdfkit?


回答1:


Check this :) You probably need requests, if you don't have. I don't know much about the salesforce library.

import requests
import pdfkit

session = requests.session()


def download(session,username,password):
    session.get('https://bneadf.thiess.com.au/adfs/ls/')

    ua = 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36'
    session.headers = {'User-Agent': self.ua}
    payload = {'UserName':username,
        'Password':password,
        'AuthMethod':'FormsAuthentication'}

    session.post('https://bneadf.thiess.com.au/adfs/ls/', data = payload, headers = session.headers)
    my_html = session.get('https://thiess.my.salesforce.com/0069000000IZH71')
    my_pdf = open('myfile.html','wb+')
    my_pdf.write(my_html.content)
    my_pdf.close()

    path_wkthmltopdf = 'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe'
    config = pdfkit.configuration(wkhtmltopdf=bytes(path_wkthmltopdf, 'utf8'))


    pdfkit.from_file('myfile.html', 'out.pdf')

download(session,"yourusername","yourpass")



回答2:


I am having a similar issue as well. I noticed there is an advanced options setting on the github/documetation where you can pass cookies and a cookiejar along with a username and password. I know that this seems to be something that you tried but it looks like you did not set the cookie name correctly. Here is the docs from https://github.com/JazzCore/python-pdfkit

options = {
  'page-size': 'Letter',
  'margin-top': '0.75in',
  'margin-right': '0.75in',
  'margin-bottom': '0.75in',
  'margin-left': '0.75in',
  'encoding': "UTF-8",
  'custom-header' : [
      ('Accept-Encoding', 'gzip')
  ]
  'cookie': [
      ('cookie-name1', 'cookie-value1'),
      ('cookie-name2', 'cookie-value2'),
  ],
  'no-outline': None
}

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

It seems that for the 'cookie' object you need to pass in a list with the cookie name and value as tuples. I havent been able to get it to work on my example, I think because my webpage uses a lot of javascript/css. However, using the requests method that Attila mentioned you might be able to do something with the salesforce login?

cookie_list = session.cookies.items()

then maybe try your example again?

options1 = {
  'page-size': None,
  'margin-top': None,
  'margin-right': None,
  'margin-bottom': None,
  'margin-left': None,
  'encoding': None,
  'custom-header' : None, 
  'cookie': cookie_list,
  'no-outline': None
}



回答3:


For using pdfkit in Django to access a protected view, just pass along the cookies in the from_url() function call.

cookie_list = request.COOKIES
# pass the cookies. You can add whatever other options you want to use
options = {
        'cookie' : [
            ('csrftoken', cookie_list['csrftoken']),
            ('sessionid', cookie_list['sessionid']),
            ]
        }

# Generate the pdf
pdf = pdfkit.from_url(url_to_page,False, options=options)

I also had a hard time with it because just passing in the cookie dictionary doesn't work.



来源:https://stackoverflow.com/questions/40644929/create-pdf-of-a-https-webpage-which-requires-login-using-pdfkit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!