简体   繁体   中英

Print page as pdf selenium python

How can I print a webpage as a PDF with headers and footers so it looks like it has been printed from google chrome.

This is what I have tried so far using PhantomJS but it doesn't have the headers and footers.

from selenium import webdriver
import selenium

def execute(script, args):
    driver.execute('executePhantomScript', {'script': script, 'args' : args })

driver = webdriver.PhantomJS('phantomjs')

# hack while the python interface lags
driver.command_executor._commands['executePhantomScript'] = ('POST', '/session/$sessionId/phantom/execute')

driver.get('http://stackoverflow.com')

# set page format
# inside the execution script, webpage is "this"
pageFormat = '''this.paperSize = {format: "A4", orientation: "portrait" };'''
execute(pageFormat, [])

# render current page
render = '''this.render("test.pdf")'''
execute(render, [])

This can be done using Selenium v4.1.3 by following pyppeteer examples.

You will need to be able to send commands to chromium and call Page.printToPDF Devtools API as shown in the following snip:

# ...
result = send_cmd(driver, "Page.printToPDF", params={
            'landscape': True
        ,'margin':{'top':'1cm', 'right':'1cm', 'bottom':'1cm', 'left':'1cm'}
        ,'format': 'A4'
        ,'displayHeaderFooter': True
        ,'headerTemplate': '<span style="font-size:8px; margin-left: 5px">Page 1 of 1</span>'
        ,'footerTemplate': f'<span style="font-size:8px; margin-left: 5px">Generated on {datetime.now().strftime("%-m/%-d/%Y at %H:%M")}</span>'
        ,'scale': 1.65
        ,'pageRanges': '1'
})

with open(out_path_full, 'wb') as file:
    file.write(base64.b64decode(result['data']))

# ...

def send_cmd(driver, cmd, params={}):
  response = driver.command_executor._request(
     'POST'
    ,f"{driver.command_executor._url}/session/{driver.session_id}/chromium/send_command_and_get_result"
    ,json.dumps({'cmd': cmd, 'params': params}))
  if response.get('status'): raise Exception(response.get('value'))
  return response.get('value')

I've included a full example in my GitHub Repo .

pdfkit may help you out. It will render a PDF from an html page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM