Welcome folks today in this blog post we will be opening
and capturing
screenshot of url as pdf document
in browser using pyqt5
framework in python. All the full source code of the application is shown below.
Get Started
In order to get started you need to install the below libraries using the pip
command as shown below
pip install pyqt5
pip install pyqtwebengine
And after that you need to make an app.py
file and copy paste the following code
app.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import sys from PyQt5.QtCore import QUrl from PyQt5.QtWebEngineWidgets import QWebEngineView from PyQt5.QtWidgets import QApplication app = QApplication(sys.argv) view = QWebEngineView() view.load(QUrl("http://google.com")) view.show() def save_pdf(): view.page().printToPdf("document.pdf") view.loadFinished.connect(save_pdf) sys.exit(app.exec_()) |
As you can see we are importing the pyqt5
and pyqtwebengine
libraries at the very top and then we are opening
a new url
inside the browser which is https://www.google.com
and then we are converting the url to pdf document
and then we are saving the pdf document with the filename called document.pdf
. We are loading the remote url
using the QUrl widget. Now if you open the app.py
file inside the command line as shown below
python app.py