1

I am developing a graphic interface to load automatically and display the pdf.

What I have done so far

I succeed to load images into Qlabel then display. I want to change and load pdf files, but it does not work, since this is an image instead of pdf file.

What I get with loading images:

enter image description here

What I desire to get.

I want to load pdf files similar to image loading.

The code:

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

I appreciate any help, thanks

-----Edit-----

I have tried to implement the code, but it does not really work. what I do wrong. I just get the blank page?

Edit code :

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

Visualization:

enter image description here

Pavel.D
  • 561
  • 1
  • 15
  • 41

0 Answers0