Created
March 26, 2022 20:33
-
-
Save Axel-Erfurt/e9b7cda9e4da355ad7905186d11a22bb to your computer and use it in GitHub Desktop.
Show PDF with PyQt6 QWebengineView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
from PyQt6.QtCore import QUrl | |
from PyQt6.QtWidgets import QApplication, QMainWindow, QWidget | |
from PyQt6.QtWebEngineWidgets import QWebEngineView #, QWebEngineSettings | |
from os import path | |
class MainWindow(QMainWindow): | |
def __init__(self): | |
super(QMainWindow, self).__init__() | |
self.setWindowTitle("PDF Viewer") | |
self.setGeometry(0, 28, 1000, 750) | |
self.webView = QWebEngineView() | |
self.webView.urlChanged.connect(self.url_changed) | |
self.webView.settings().setAttribute(self.webView.settings().WebAttribute.PluginsEnabled, True) | |
self.webView.settings().setAttribute(self.webView.settings().WebAttribute.PdfViewerEnabled, True) | |
self.setCentralWidget(self.webView) | |
def url_changed(self): | |
self.setWindowTitle(self.webView.title()) | |
def go_back(self): | |
self.webView.back() | |
if __name__ == '__main__': | |
import sys | |
app = QApplication(sys.argv) | |
win = MainWindow() | |
win.show() | |
if len(sys.argv) > 1: | |
win.webView.load(QUrl(f"file://{sys.argv[1]}")) | |
sys.exit(app.exec()) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment