How to display PDF with python-poppler-qt4?

Question:

I have downloaded and installed python-poppler-qt4 and I am now trying out a simple Qt application to display a PDF page. I’ve followed what I’ve been able to get from the web, i.e. convert the PDF to a QImage, then to a QPixMap, but it doesn’t work (all I get is a small window with no visible content).

I may have failed at some point (QImage.width() returns the width I have input, QPixMap.width() returns 0).

Here is the code:

#!/usr/bin/env python

import sys
from PyQt4 import QtGui, QtCore
import popplerqt4

class Application(QtGui.QApplication):
    def __init__(self):
        QtGui.QApplication.__init__(self, sys.argv)     
        self.main = MainWindow()
        self.main.show()

    class MainWindow(QtGui.QFrame):
        def __init__(self, parent=None):
            QtGui.QWidget.__init__(self, parent)
            self.layout = QtGui.QVBoxLayout()
            self.doc = popplerqt4.Poppler.Document.load('/home/benjamin/test.pdf')
            self.page = self.doc.page(1)
# here below i entered almost random dpi, position and size, just to test really 
            self.image = self.page.renderToImage(150, 150, 0, 0, 210, 297)
            self.pixmap = QtGui.QPixmap()
            self.pixmap.fromImage(self.image)
            self.label = QtGui.QLabel(self)
                    self.label.setPixmap(self.pixmap)
            self.layout.addWidget(self.label)
            self.setLayout(self.layout)

    if __name__ == "__main__":
            application = Application()
            sys.exit(application.exec_())

Where does it go wrong here? Thanks.

Asked By: neydroydrec

||

Answers:

I’m not familiar with python, so this might not apply directly, but QPixmap::fromImage is a static function that returns a QPixmap. So your code should read something like:

 self.pixmap = QtGui.QPixmap.fromImage(self.image)

In other words, self.pixmap.fromImage doesn’t change self.pixmap, it returns a new pixmap generated from the image you give it as a parameter.

Answered By: Mat

yes i know that the question has an answer.

but i faced an error when i do the same thing in PyQt5 to show a PDF file as an image.

and i found the solution of my problem.

i posted this answer to help who faced the same problem.


if you want to show a pdf file in your PyQt5 program you have 2 choices

1 – the first one is to use web engine (but it takes a lot of resources from the ram)
2 – the second it to convert the pdf into an image and show it on label

i chose the second choice

and this is my code to show a pdf file as an image and to solve the problem :

from PyQt5 import QtWidgets,QtCore,QtGui
import pypdfium2 as pdfium

the_file = "My_PDF_File.pdf"

application = QtWidgets.QApplication([])
window = QtWidgets.QWidget()
window.resize(700,600)
window_layout = QtWidgets.QGridLayout()

label_to_display_the_page = QtWidgets.QLabel()
label_to_display_the_page.setAlignment(QtCore.Qt.AlignCenter)
label_to_display_the_page_geometry = label_to_display_the_page.geometry()
pdf = pdfium.PdfDocument(the_file)
page = pdf.get_page(1)
pil_image = page.render_topil(scale=1,rotation=0,crop=(0, 0, 0, 0),greyscale=False,optimise_mode=pdfium.OptimiseMode.NONE)
image = pil_image.toqimage()
label_pixmap = QtGui.QPixmap.fromImage(image)
size = QtCore.QSize(label_to_display_the_page_geometry.width()-50,label_to_display_the_page_geometry.height()-50)
label_to_display_the_page.setPixmap(label_pixmap.scaled(size,QtCore.Qt.KeepAspectRatio, QtCore.Qt.SmoothTransformation))


window_layout.addWidget(label_to_display_the_page)
window.setLayout(window_layout)
window.show()
application.exec()
Answered By: Mohammed almalki
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.