gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 11:16:34 26 4
gpt4 key购买 nike

直接问题,当 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_())

显示:

enter image description here

知道如何解决这个问题吗?

最佳答案

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_())

关于python - Pdfjs 打印按钮不适用于 PyQt5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56872960/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com