File: demo.py

package info (click to toggle)
python-poppler-qt4 0.16.3-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 168 kB
  • ctags: 39
  • sloc: python: 206; makefile: 5
file content (49 lines) | stat: -rw-r--r-- 1,023 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import sys
from PyQt4 import QtGui
import popplerqt4

usage = """
Demo to load a PDF and display the first page.

Usage:

    python demo.py file.pdf

"""


def pdf_view(filename):
    """Return a Scrollarea showing the first page of the specified PDF file."""
    
    label = QtGui.QLabel()
    
    doc = popplerqt4.Poppler.Document.load(filename)
    doc.setRenderHint(popplerqt4.Poppler.Document.Antialiasing)
    doc.setRenderHint(popplerqt4.Poppler.Document.TextAntialiasing)
    
    page = doc.page(0)
    image = page.renderToImage()
    
    label.setPixmap(QtGui.QPixmap.fromImage(image))
    
    area = QtGui.QScrollArea()
    area.setWidget(label)
    area.setWindowTitle(filename)
    return area


def main():
    app = QtGui.QApplication(sys.argv)
    argv = QtGui.QApplication.arguments()
    if len(argv) < 2:
        sys.stderr.write(usage)
        sys.exit(2)
    
    filename = argv[-1]
    view = pdf_view(filename)
    view.show()
    sys.exit(app.exec_())


if __name__ == "__main__":
    main()