温馨提示:本文翻译自stackoverflow.com,查看原文请点击:python - Pdfjs print button does not work with PyQt5

python - Pdfjs打印按钮不适用于PyQt5

发布于 2020-03-27 11:41:32

直接发布,当pdf与pdfjs一起加载到pyqt5中时,似乎打印按钮无法正常工作,下载按钮也是如此。

如何解决此错误?

代码:

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

PDFJS = 'file:///pdfjs/web/viewer.html'
PDF = 'file:///file0.pdf'
class PdfReport(QtWebEngineWidgets.QWebEngineView):
    def __init__(self, parent=None):
        super(PdfReport, self).__init__(parent)
        self.load(QtCore.QUrl.fromUserInput('%s?file=%s' % (PDFJS, PDF)))  

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

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    im = PdfReport()
    im.show()
    sys.exit(app.exec_())

显示:

在此处输入图片说明

任何想法如何解决?

查看更多

查看更多

提问者
Pavel.D
被浏览
47
eyllanesc 2019-07-04 13:27

Qt WebEngine中未启用打印任务,因此显示了错误(我仍在尝试获取数据)。但是使用PDF的下载按钮是可能的,为此,您必须使用的downloadRequested信号QWebEngineProfile

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 __init__(self, parent=None):
        super(PdfReport, self).__init__(parent)
        QtWebEngineWidgets.QWebEngineProfile.defaultProfile().downloadRequested.connect(
            self.on_downloadRequested
        )

    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)

    @QtCore.pyqtSlot(QtWebEngineWidgets.QWebEngineDownloadItem)
    def on_downloadRequested(self, download):
        path, _ = QtWidgets.QFileDialog.getSaveFileName(
            self, "Save File", "sample.pdf", "*.pdf"
        )
        if path:
            download.setPath(path)
            download.accept()


if __name__ == "__main__":

    app = QtWidgets.QApplication(sys.argv)
    w = PdfReport()
    path = os.path.join(CURRENT_DIR, "file0.pdf")
    w.load_pdf(path)
    w.show()
    sys.exit(app.exec_())

发布
问题

分享
好友

手机
浏览

扫码手机浏览