strictdoc icon indicating copy to clipboard operation
strictdoc copied to clipboard

HTML2PDF: Set up instructions for generating PDF files

Open stanislaw opened this issue 4 years ago • 0 comments

The following links demonstrate how Selenium, chromedriver, and Google Chrome can be used for printing HTML pages to PDF files.

Working example:

url = 'file:///tmp/paginate/examples/simple.html'

import base64
import json


def send_devtools(driver, cmd, params={}):
    resource = "/session/%s/chromium/send_command_and_get_result" % driver.session_id
    url = driver.command_executor._url + resource
    body = json.dumps({'cmd': cmd, 'params': params})
    response = driver.command_executor._request('POST', url, body)
    return response.get('value')


def get_pdf_from_html(driver, url, print_options={}, output_file_path="example.pdf"):
    driver.get(url)

    calculated_print_options = {
        'landscape': False,
        'displayHeaderFooter': False,
        'printBackground': True,
        'preferCSSPageSize': True,
    }
    calculated_print_options.update(print_options)
    result = send_devtools(driver, "Page.printToPDF", calculated_print_options)
    data = base64.b64decode(result['data'])
    with open(output_file_path, "wb") as f:
        f.write(data)



# example
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

webdriver_options = Options()
webdriver_options.add_argument("--no-sandbox")
webdriver_options.add_argument('--headless')
webdriver_options.add_argument('--disable-gpu')

chromedriver_exec="./chromedriver"
driver = webdriver.Chrome(chromedriver_exec, options=webdriver_options)
get_pdf_from_html(driver, url)
driver.quit()

stanislaw avatar Apr 02 '21 13:04 stanislaw