I was finding solution to print webpage into local file PDF, using Python. one of the good solution is to use Qt, found here, https://bharatikunal.wordpress.com/2010/01/.
It didn't work at the beginning as I had problem with the installation of PyQt4 because it gave error messages such as 'ImportError: No module named PyQt4.QtCore
', and 'ImportError: No module named PyQt4.QtCore
'.
It was because PyQt4's not installed properly. I used to have the libraries located at C:\Python27\Lib however it's not for PyQt4.
In fact, it simply needs to download from http://www.riverbankcomputing.com/software/pyqt/download (mind the correct Python version you are using), and install it to C:\Python27 (my case). That's it.
Now the scripts runs fine so I want to share it. for more options in using Qprinter, please refer to http://qt-project.org/doc/qt-4.8/qprinter.html#Orientation-enum.
The Python PDFKit module allows the programmers to convert a local HTML file into a PDF format using the from_file() function.
You can save a webpage as a PDF on a Windows computer using any installed web browser, including Microsoft Edge, Google Chrome, and Mozilla Firefox. To save any webpage as a PDF, you will need to access the browser's Print menu and select "Save as PDF" or "Microsoft Print to PDF" for your printing destination.
Fortunately, the Python ecosystem has some great packages for reading, manipulating, and creating PDF files.
You also can use pdfkit:
import pdfkit pdfkit.from_url('http://google.com', 'out.pdf')
MacOS: brew install Caskroom/cask/wkhtmltopdf
Debian/Ubuntu: apt-get install wkhtmltopdf
Windows: choco install wkhtmltopdf
See official documentation for MacOS/Ubuntu/other OS: https://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With