如何使用python-poppler-qt4显示PDF? [英] How to display PDF with python-poppler-qt4?

查看:266
本文介绍了如何使用python-poppler-qt4显示PDF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经下载并安装了 python-poppler-qt4 ,我是现在尝试一个简单的Qt应用程序以显示PDF页面.我遵循了从网上获得的功能,即将PDF转换为QImage,然后转换为QPixMap,但是它不起作用(我得到的只是一个没有可见内容的小窗口). /p>

我可能在某个时候失败了(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不熟悉,因此可能不适用于python,但是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.

解决方案

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.

这篇关于如何使用python-poppler-qt4显示PDF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆