Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to Render local PDFs with QWebEngine6?

How to Render local PDFs with QWebEngine6?

Scheduled Pinned Locked Moved Unsolved General and Desktop
1 Posts 1 Posters 315 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    wad11656
    wrote on last edited by
    #1

    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)
    

    042c72ca-073d-4022-b403-03a810f2f1bb-image.png

    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())
    
    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved