How to Render local PDFs with QWebEngine6?
Unsolved
General and Desktop
-
Re: [QWebEngineView Crbug/1173575](non-JS module files deprecated when trying to load a PDF file)
How do you render local PDFs with QWebEngine? I have a feeling I'll get a much nicer rendering than rasterizing as an image then displaying via PyMuPDF/
fitz
(which right now is terribly ugly/does not show fine lines correctly despite messing with dpi settings).I'm getting the exact same
js: crbug/1173575, non-JS module files deprecated.
errors that OP did in the post above, including the occasional crash/long freezes.This is the key part of the code I'm using to try to render the PDF (full code at bottom):
directory = os.path.join(current_script_path, "PDF Templates") self.pdfFiles = [f for f in os.listdir(directory) if f.endswith('.pdf')] ... pdf_path = os.path.join("PDF Templates", self.pdfFiles[index]) full_path = QUrl.fromLocalFile(os.path.abspath(pdf_path)) self.viewer.setUrl(full_path)
import sys import os from PyQt6.QtCore import Qt, QUrl from PyQt6.QtGui import QPixmap from PyQt6.QtWidgets import QMainWindow, QApplication, QMainWindow, QLabel, QComboBox, QVBoxLayout, QWidget, QGraphicsView, QGraphicsScene from PyQt6.QtWebEngineWidgets import QWebEngineView from PyQt6.QtWebEngineCore import QWebEngineSettings import fitz class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("PDF Viewer and Editor") self.setGeometry(100, 100, 800, 600) # Set up the layout layout = QVBoxLayout() centralWidget = QWidget(self) self.setCentralWidget(centralWidget) centralWidget.setLayout(layout) # Combo box for selecting a PDF self.comboBox = QComboBox(self) self.loadPDFs() layout.addWidget(self.comboBox) # Setup QWebEngineView for displaying PDFs self.viewer = QWebEngineView(self) layout.addWidget(self.viewer) # Connect the combo box to change the display self.comboBox.currentIndexChanged.connect(self.updatePDFDisplay) def loadPDFs(self): current_script_path = os.path.dirname(os.path.abspath(__file__)) directory = os.path.join(current_script_path, "PDF Templates") self.pdfFiles = [f for f in os.listdir(directory) if f.endswith('.pdf')] self.comboBox.addItems(self.pdfFiles) def updatePDFDisplay(self, index): if index == -1: return pdf_path = os.path.join("PDF Templates", self.pdfFiles[index]) full_path = QUrl.fromLocalFile(os.path.abspath(pdf_path)) print("Attempting to render PDF at path:", full_path.toString()) self.viewer.setUrl(full_path) if __name__ == "__main__": app = QApplication(sys.argv) mainWindow = MainWindow() mainWindow.show() sys.exit(app.exec())