Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert webpage into PDF by using Python

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.

like image 526
Mark K Avatar asked Apr 29 '14 08:04

Mark K


People also ask

Can we convert HTML to PDF in Python?

The Python PDFKit module allows the programmers to convert a local HTML file into a PDF format using the from_file() function.

Can I convert a web page to PDF?

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.

Can Python produce a PDF?

Fortunately, the Python ecosystem has some great packages for reading, manipulating, and creating PDF files.


1 Answers

You also can use pdfkit:

Usage

import pdfkit pdfkit.from_url('http://google.com', 'out.pdf') 

Install

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

like image 117
NorthCat Avatar answered Sep 29 '22 09:09

NorthCat