繁体   English   中英

如何用python-poppler-qt4显示PDF?

[英]How to display PDF with python-poppler-qt4?

我已经下载并安装了python-poppler-qt4 ,现在我正在尝试一个简单的 Qt 应用程序来显示 PDF 页面。 我已经按照我能够从网络上获得的内容进行操作,即将 PDF 转换为 QImage,然后转换为 QPixMap,但它不起作用(我得到的只是一个没有可见内容的小窗口)。

我可能在某些时候失败了( QImage.width()返回我输入的宽度, QPixMap.width()返回 0)。

这是代码:

#!/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_())

这里哪里出了问题? 谢谢。

我不熟悉python,因此这可能无法直接应用,但是QPixmap::fromImage是返回QPixmap的静态函数。 因此,您的代码应为:

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

换句话说, self.pixmap.fromImage不会更改self.pixmap ,它返回从您提供的图像作为参数生成的新像素图。

是的,我知道这个问题有答案。

但是当我在 PyQt5 中做同样的事情以将 PDF 文件显示为图像时,我遇到了一个错误。

我找到了我的问题的解决方案。

我发布这个答案是为了帮助遇到同样问题的人。


如果你想在你的 PyQt5 程序中显示一个 pdf 文件,你有 2 个选择

1 - 第一个是使用网络引擎(但它需要从 ram 获取大量资源) 2 - 第二个是将 pdf 转换为图像并将其显示在标签上

我选择了第二个选择

这是我将 pdf 文件显示为图像并解决问题的代码:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM