Package: selenium-print
selenium-printselenium-printis an open-source Python package that enables printing HTML to PDF using the Selenium framework. This package is especially useful when you want to render dynamic content that requires a JS engine or ajax calls to a server or complex layouts using state-of-the-art CSS like grid, flexbox, media queries or transform.Note: This library usesseleniumwhich in turn requires the installation of the Google Chrome browser as well as a chromedriver. This librarydoes not support any other browser at the moment. This makes itnot a lightweight solutionfor generating PDF files. But it does make an easy to setup PDF generation solution that supports ALL browser functionality. Also note that starting a selenium browser is not very fast either, so if your priority is not ease-of-use, but performance, this is not the library for you. Although in benchmarks withweasyprint, which seems to also have one of the most impressive HTML/CSS support, this framework beat it by an average of 33% time decrease.FeaturesUtilizes the full flexibility of the JS engine of the browser.Supports ajax calls to the server.Renders HTML to PDF.Easy to install and use.InstallationYou can installselenium-printusing pip:pipinstallselenium-printUsageSimple usageThe simplest way to use selenium-print is to use the utility method that wraps all logic.fromseleniumprintimportfile_to_pdfinput_html_file_path="/Users/user/path/to/input.html"output_pdf_file_path="/Users/user/path/to/output.pdf"file_to_pdf(input_html_file_path,output_pdf_file_path)input_html_url="http://localhost:8000/report/1"output_pdf_file_path="./report_pdfs/report_1.pdf"url_to_pdf(input_html_url,output_pdf_file_path)Class-Based UsageAnother way to use SeleniumPDF is to call the url_to_pdf method with a URL and an optional output file path:fromseleniumprintimportSeleniumPDFurl="https://www.example.com"pdf_file_path="example.pdf"# Initialize SeleniumPDFselenium_pdf=SeleniumPDF()# Generate PDF from URLselenium_pdf.url_to_pdf(url,pdf_file_path)`In this example,SeleniumPDFis initialized with the default options and theurl_to_pdfmethod is called with a URL and an output file path. The page at the specified URL is loaded and then converted to a PDF, which is saved to the specified file path, if an output file path was provided. If not it will return the raw bytes that you can work with.Custom WaitingIf you need to wait for some time after loading the page before converting it to a PDF, you can use theload_pageandconvert_current_page_to_pdfmethods separately and add a sleep in between or some other kind of waiting logic, e.g. a JS based logic. Here's an example:fromseleniumprintimportSeleniumPDFimporttimeurl="https://www.example.com"pdf_file_path="example.pdf"# Initialize SeleniumPDFselenium_pdf=SeleniumPDF()# Load page and wait for 5 secondsselenium_pdf.load_page(url)time.sleep(5)# Convert page to PDF and save to fileselenium_pdf.convert_current_page_to_pdf(pdf_file_path)## Alternative JS-based waiting (needs appropriate HTML-side implementation)selenium_pdf.load_page(url)# wait manually until page is ready or some other condition is truewhileselenium_pdf.driver.driver.execute_js("return window.readyState !== 'complete'"):time.sleep(0.5)# Convert page to pdf nowselenium_pdf.convert_current_page_to_pdf(pdf_file_path)In this example,load_pageis called to load the page at the specified URL, and then a 5-second wait is added using thetime.sleepfunction. Finally,convert_current_page_to_pdfis called to convert the loaded page to a PDF and save it to the specified file path.Specifying chromedriver locationThe constructor ofSeleniumPDFas well as the utility methodfile_to_pdfandurl_to_pdftake a chrome-driver specific argument calledchrome_driver_path.# Build path to HTML file relative to current scripthtml_file_path=os.path.join(script_dir,"test.html")# Build path to output PDF filepdf_file_path=os.path.join(script_dir,"test_direct.pdf")# Generate PDF from HTML File Pathfile_to_pdf(html_file_path,pdf_file_path,chrome_driver_path=CHROME_DRIVER_PATH)ContributingContributions are welcome! Here are some ways you can contribute:Report issues and bugs.Suggest new features or enhancements.Submit pull requests for bug fixes or new features.Before submitting a pull request, please make sure that your code follows PEP 8 guidelines and includes tests.SupportThis is a personal project, so support may be limited. However, I will do my best to answer any issues that are reported.LicenseThis project is published under MIT license which means its basically free to use for everyone in any way whatsoever.