0

I am trying to view a pdf file with QtWebEngineWidgets but I get "your file was not found" in the window. First time trying QtWebEngineWidgets. Please see the result in pic below.

import os
import sys
from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets

CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))

PDFJS = QtCore.QUrl.fromLocalFile(os.path.join(CURRENT_DIR, "pdfjs/web/viewer.html")).toString()

class PdfReport(QtWebEngineWidgets.QWebEngineView):
    def load_pdf(self, filename):
        url = QtCore.QUrl.fromLocalFile(filename).toString()
        self.load(QtCore.QUrl.fromUserInput("%s?file=%s" % (PDFJS, url)))

    def sizeHint(self):
        return QtCore.QSize(640, 480)


class Foo(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(Foo, self).__init__(parent)

        self.pdf = PdfReport()
        filename = "summary_equipment_evaluation.pdf"
        self.pdf.load_pdf(filename)

        lay = QtWidgets.QVBoxLayout(self)
        lay.addWidget(self.pdf)


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    w = Foo()
    w.show()
    sys.exit(app.exec_())

enter image description here

UPDATE: After downloading pdfjs, I get this empty window:

enter image description here

jabbarlee
  • 110
  • 2
  • 11
  • I have tested these codes and they work, they allow to show a pdf in a Qt window, maybe you have had problems in the implementation so you should show in detail what you have tried. Please use `@username` – eyllanesc Aug 03 '21 at 16:37
  • I tried to run code in your reply here https://stackoverflow.com/questions/56866298/qwebengineview-update-with-pdf-path by changing pdf path but I get "Your file was not found" in window. – jabbarlee Aug 04 '21 at 16:01
  • @eyllanesc added pic above – jabbarlee Aug 04 '21 at 16:03
  • Show the code, an image does not help. Please read [ask] and review the [tour] – eyllanesc Aug 04 '21 at 16:07
  • @eyllanesc just updated the question with details. Thanks! – jabbarlee Aug 04 '21 at 16:41
  • You have an error, you have to use the fullpath (the file name is not enough) – eyllanesc Aug 04 '21 at 16:45
  • @eyllanesc I tried that as well but get the same error. – jabbarlee Aug 04 '21 at 16:47
  • add `print(QtCore.QUrl.fromUserInput("%s?file=%s" % (PDFJS, url)))` after `self.load(QtCore.QUrl.fromUserInput("%s?file=%s" % (PDFJS, url)))` and tell me what you get (obviously using the fullpath) – eyllanesc Aug 04 '21 at 16:48
  • @eyllanesc PyQt5.QtCore.QUrl('file:///G:/QC Report Generation/pdfjs/web/viewer.html?file=file:///G:/QC Report Generation/PDFs/summary_equipment_evaluation.pdf') – jabbarlee Aug 04 '21 at 16:50
  • Do you have the unzipped pdfjs folder next to your script? – eyllanesc Aug 04 '21 at 16:54
  • @eyllanesc No, do I need to download it from somewhere? – jabbarlee Aug 04 '21 at 16:56
  • @eyllanesc Sorry to bother too much but after downloading pdfjs I get the result shown above. It looks like I am close but can't still open the pdf. PyQt5.QtCore.QUrl('file:///G:/QC_Report_Generation/pdfjs/web/viewer.html?file=file:///G:/QC_Report_Generation/PDFs/summary_equipment_evaluation.pdf') – jabbarlee Aug 04 '21 at 17:17
  • 1
    Try the following example (which no longer needs jspdf): https://gist.github.com/eyllanesc/7566bab2f8a91593c460015ee2151717 – eyllanesc Aug 04 '21 at 17:33
  • @eyllanesc I got empty window, the same even I select pdf manually. – jabbarlee Aug 04 '21 at 17:50
  • In that code I have added some print, what version of Qt and PyQt are printed? – eyllanesc Aug 04 '21 at 17:50
  • PyQt5 version: 5.9.2, Qt version: 5.9.6 – jabbarlee Aug 04 '21 at 17:51
  • 1
    There is the problem, you have a very old version. Use the latest versions: `python -m pip install pyqt5==5.15.4 pyqtwebengine==5.15.4` – eyllanesc Aug 04 '21 at 17:52
  • I get this error but it works: "[73952:43052:0830/170726.780:ERROR:extension_function_dispatcher.cc(487)] Permission denied for metricsPrivate.recordValue " How can I suppress it? – jabbarlee Aug 30 '21 at 21:26
  • See my comments on: https://stackoverflow.com/questions/57733039/errorpermission-manager-qt-cpp82-unsupported-permission-type-13/57740149#57740149 – eyllanesc Aug 30 '21 at 21:30

0 Answers0