Ben Chuanlong Du's Blog

It is never too late to learn.

Convert Web Pages to PDF Using Python

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

python-pdfkit

Python wrapper for wkhtmltopdf utility to convert HTML to PDF using Webkit.

In [ ]:
!pip install pdfkit
!sudo apt-get install wkhtmltopdf
In [ ]:
import pdfkit 
pdfkit.from_url('https://www.google.co.in/', 'shaurya.pdf') 
In [ ]:
!pip3 install weasyprint
In [ ]:
pdf = weasyprint.HTML('http://www.google.com').write_pdf()
file('google.pdf', 'wb').write(pdf)
In [ ]:
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability("download.default_directory","C:");
cap.setCapability("download.prompt_for_download","false");
cap.setCapability("directory_upgrade","true");
cap.setCapability("plugins.plugins_disabled","Chrome PDF Viewer");

WebDriver driver = new ChromeDriver(cap);

Or you can add options.AddArgument("---printing") to automatically click the print button.

https://stackoverflow.com/questions/30452395/selenium-pdf-automatic-download-not-working

Sikuli

PyAutoGUI + WebBrowser

Other Solutions

Automate Web Page To PDF introduces a way of using PyAutoGUI to automate the convertion of web pages to PDF.

In [ ]:
 

Comments