strictdoc
strictdoc copied to clipboard
HTML2PDF: Set up instructions for generating PDF files
The following links demonstrate how Selenium, chromedriver, and Google Chrome can be used for printing HTML pages to PDF files.
- https://github.com/maxvst/python-selenium-chrome-html-to-pdf-converter
- How to convert webpage into PDF by using Python
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()