Welcome folks today in this blog post we will be building pdf viewer
gui desktop app using pdf.js
library in pyqt5. All the full source code of the application is shown below.
Get Started
In order to get started you need to install the following library using the pip
command as shown below
pip install pyqt5
Now we need to download the stable
version of pdf.js
library from the below link
Just extract it inside your root
directory like this
And now just 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 18 19 20 21 22 23 24 |
#!/usr/bin/python3 # -*- coding: utf-8 -*- import sys import os from PyQt5.QtWidgets import QApplication from PyQt5.QtCore import QUrl from PyQt5.QtWebEngineWidgets import QWebEngineView PDFJS = f"file://{os.path.abspath('./web/viewer.html')}" print(PDFJS) PDF = f'file://{"%20".join(sys.argv[1:])}' print("loading PDF:", PDF) class Window(QWebEngineView): def __init__(self): super(Window, self).__init__() self.load(QUrl.fromUserInput(f'{PDFJS}?file={PDF}')) if __name__ == '__main__': app = QApplication(sys.argv) window = Window() window.showMaximized() sys.exit(app.exec_()) |
And now if you execute the above script by typing the below command as shown below
python app.py sample.pdf
So here we are providing the path of the input pdf
file inside the command as shown above