我正在开发图形界面以自动加载并显示 pdf 。
我到目前为止所做的事情
我成功负载图像转换成Qlabel
,则显示。我想更改和加载pdf文件,但是它不起作用,因为这是图像而不是pdf文件。
加载图片会得到什么:
我想要得到的东西。
我想加载类似于图像加载的pdf文件。
代码:
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication,QMainWindow,QLabel,QSizePolicy,QAction
from PyQt5.QtPrintSupport import QPrintDialog,QPrinter
from PyQt5.QtGui import QImage,QIcon,QPixmap
class MainWindow(QtWidgets.QMainWindow):
def __init__(self,parent=None):
super(MainWindow, self).__init__(parent)
self.setWindowTitle('PDF loading')
self.loadLabel=QtWidgets.QLabel()
self.loadLabel.setSizePolicy(QtWidgets.QSizePolicy.Ignored,QtWidgets.QSizePolicy.Ignored)
self.setCentralWidget(self.loadLabel)
self.image = QtGui.QImage()
if self.image.load('image\test.jpg'):
self.loadLabel.setPixmap(QtGui.QPixmap.fromImage(self.image))
self.resize(self.image.width(),self.image.height())
self.createActions()
self.createMenus()
self.createToolBars()
def createActions(self):
self.PrintAction=QAction(QIcon('image\test.jpg'),self.tr('Test'),self)
self.PrintAction.triggered.connect(self.slotPrint)
def createMenus(self):
PrintMenu=self.menuBar().addMenu(self.tr('Test'))
PrintMenu.addAction(self.PrintAction)
def createToolBars(self):
fileToolBar=self.addToolBar('Print')
fileToolBar.addAction(self.PrintAction)
def slotPrint(self):
printer=QPrinter()
printDialog=QPrintDialog(printer,self)
if printDialog.exec_():
painter=QPainter(printer)
rect=painter.viewport()
size=self.image.size()
size.scale(rect.size(),Qt.KeepAspectRatio)
painter.setViewport(rect.x(),rect.y(),size.width(),size.height())
painter.setWindow(self.image.rect)
painter.drawImage(0,0,self.image)
if __name__ == '__main__':
app=QApplication(sys.argv)
main=MainWindow()
main.show()
sys.exit(app.exec_())
感谢您的帮助,谢谢
-----编辑-----
我已经尝试实现代码,但是它实际上没有用。我做什么错。我刚得到空白页吗?
修改代码:
import sys
from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets
PDFJS = 'file:///C:/Users/se/Desktop/Python_Coding/Stackoverflow/QPrint and PDF Preview/123.pdf'
# PDFJS = 'file:///usr/share/pdf.js/web/viewer.html'
PDF = 'file:///C:/Users/se/Desktop/4444.pdf'
class Window(QtWebEngineWidgets.QWebEngineView):
def __init__(self):
super(Window, self).__init__()
self.load(QtCore.QUrl.fromUserInput('%s?file=%s' % (PDFJS, PDF)))
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = Window()
window.setGeometry(600, 50, 800, 600)
window.show()
sys.exit(app.exec_())
可视化: