1

I am porting my small project from pyqt5 to pyqt6 which deals with displaying PDFs using QWebEngineView.

I used this PyQt5 snippet to display the PDF below:

import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *

from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEngineSettings

class Window(QWidget):
    def __init__(self):
        super().__init__()

        self.webView = QWebEngineView()
        self.webView.settings().setAttribute(QWebEngineSettings.PluginsEnabled, True)
        self.webView.settings().setAttribute(QWebEngineSettings.PdfViewerEnabled, True)

        url = QUrl.fromLocalFile(r"C:/Users/Eliaz/Desktop/qt5cadaquesPart14.pdf")
        self.webView.setUrl(url)

        self.webView.show()

app = QApplication(sys.argv)
ex = Window()
sys.exit(app.exec())

enter image description here

Now when I ported it into a PyQt6 snippet given in the code below, the program is just crashing without any error messages:

import sys
from PyQt6.QtGui import *
from PyQt6.QtCore import *
from PyQt6.QtWidgets import *

from PyQt6.QtWebEngineWidgets import QWebEngineView
from PyQt6.QtWebEngineCore import QWebEngineSettings

class Window(QWidget):
    def __init__(self):
        super().__init__()

        self.webView = QWebEngineView()
        self.webView.settings().setAttribute(QWebEngineSettings.WebAttribute.PluginsEnabled, True)
        self.webView.settings().setAttribute(QWebEngineSettings.WebAttribute.PdfViewerEnabled, True)

        url = QUrl.fromLocalFile(r"C:/Users/Eliaz/Desktop/qt5cadaquesPart14.pdf")
        self.webView.setUrl(url)

        self.webView.show()

app = QApplication(sys.argv)
ex = Window()
sys.exit(app.exec())

Before exiting though, it shows these information on the console:

qt.webenginecontext:
GL Type: desktop
Surface Type: OpenGL
Surface Profile: CompatibilityProfile
Surface Version: 4.6
QSG RHI Backend: OpenGL
Using Supported QSG Backend: yes
Using Software Dynamic GL: no
Using Multithreaded OpenGL: yes

Init Parameters:
  *  application-name python
  *  browser-subprocess-path C:\Users\Eliaz\AppData\Local\Programs\Python\Python310\lib\site-packages\PyQt6\Qt6\bin\QtWebEngineProcess.exe 
  *  create-default-gl-context
  *  disable-es3-gl-context
  *  disable-features ConsolidatedMovementXY,InstalledApp,BackgroundFetch,WebOTP,WebPayments,WebUSB,PictureInPicture
  *  disable-speech-api
  *  enable-features NetworkServiceInProcess,TracingServiceInProcess
  *  enable-threaded-compositing
  *  in-process-gpu
  *  use-gl desktop

Even with the additional information above, I can't still find what's wrong with my code but I'm certain that the path of the PDFs exists.

Also, I am on Windows 10 These are the versions of PyQt6 that I have. (Updated)

PyQt6                     6.3.1
PyQt6-Qt6                 6.3.1
PyQt6-sip                 13.4.0
PyQt6-WebEngine           6.3.1
PyQt6-WebEngine-Qt6       6.3.1

Update: I tried updating the pyqt6 packages installed in my system to 6.3.1, and testing it in a fresh virtual box but it still crashes as seen in this GIF.

alt

Eliazar
  • 301
  • 3
  • 13
  • Odd that your version numbers don't quite match. The example works fine for me on arch-linux using qt-6.3.1 and pyqt-6.3.1. – ekhumoro Aug 14 '22 at 11:58
  • I just tried to update all of the PyQt5 Packages but it's still not working. Also, I'm on windows 10, do you think it is a bug on the windows side? – Eliazar Aug 15 '22 at 06:27
  • That's what I'm suggesting, yes. Another thing to notice with your screenshot, is that there's some missing interface components. There should be some zoom buttons in the qt5 version. However, from your gif, it's very hard to see what's actually happening. There's no real evidence that the app is actually crashing. You really need to run the script from a command-window, rather than an IDE, and show any the error output. It might also help if you edit the script and set an explicit geometry for the window. – ekhumoro Aug 15 '22 at 09:29
  • PS: if you just need a functioning pdf-viewer for web-engine, see [this answer](https://stackoverflow.com/a/48053017/984421) for an alternative. – ekhumoro Aug 15 '22 at 09:30
  • Thank You, I just updated the gif. Also, I already tried using PDF.js with QWebEngineView but It fails to load the pdf as [shown in this gist](https://gist.github.com/eliazar-sll/4d9fc116b4e6333a97eccdf6fef749e4). – Eliazar Aug 15 '22 at 10:27
  • Can you provide a sample pdf that won't load? – ekhumoro Aug 15 '22 at 10:36
  • Yes, [This is](https://www.dropbox.com/s/ft7cjxjrl6h6eyo/qt5cadaquesPart14.pdf?dl=0) the dropbox link of the pdf that won't load. – Eliazar Aug 15 '22 at 10:47
  • Well, that's frustrating, because a couple of days ago pdfjs was working fine for me (as can be seen from the screenshot in the answer I linked to). However, I updated my system yesterday, and now I am also seeing javascript errors in PyQt5. It still works fine with PyQt6, but I had to [download the legacy pdfjs version](https://mozilla.github.io/pdf.js/getting_started/) to get things working again with PyQt5. – ekhumoro Aug 15 '22 at 12:03
  • Thank You, It actually works! PyQt5 just needs the legacy pdfjs version to work. I guess I will use this for now while this bug is unresolved. Please go ahead and post this as an answer so others would find this as an alternative. – Eliazar Aug 15 '22 at 12:41

1 Answers1

1

This seems to be caused by a Windows-specific bug, since eveything works as expected on Linux using both PyQt-5.15.7 and PyQt-6.1.3. It's hard to say what the cause might be, given that there's no relevant output.

As a work-around, an alternative is to use PDF.js. See here for more details:

But note that it may be necessary to use the legacy version with PyQt5.

ekhumoro
  • 115,249
  • 20
  • 229
  • 336