forked from akkana/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqpdfview.py
executable file
·298 lines (226 loc) · 10 KB
/
qpdfview.py
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#!/usr/bin/env python3
# How to view PDF in a Qt5 widget.
# Poppler has almost no documentation and Python-Qt5 isn't much better,
# so maybe this will help someone.
# Copyright 2018 by Akkana Peck: share and enjoy under the GPLv2 or later.
# Uses popplerqt5: https://pypi.org/project/python-poppler-qt5/
# or Debian package python3-poppler-qt5
# Poppler is theoretically available from gi (instead of popplerqt5),
# but I haven't found any way to get that Poppler to work with Qt5
# because it can only draw to a Cairo context.
# import gi
# gi.require_version('Poppler', '0.18')
# from gi.repository import Poppler
import sys
import traceback
from PyQt5.QtWidgets import QWidget, QApplication, QShortcut, \
QLabel, QScrollArea, QSizePolicy, QVBoxLayout
from PyQt5.QtGui import QPainter, QColor, QFont, QPixmap
from PyQt5.QtCore import Qt, QPoint, QSize, QUrl, QByteArray
from PyQt5.QtNetwork import QNetworkAccessManager, \
QNetworkReply, QNetworkRequest
from popplerqt5 import Poppler
# Poppler gives page sizes in points, so 72 DPI.
# If you want to use a DPI other than 72, you have to convert.
POINTS_PER_INCH = 72
class PDFWidget(QLabel):
'''
A widget showing one page of a PDF.
If you want to show multiple pages of the same PDF,
make sure you share the document (let the first PDFWidget
create the document, then pass thatPDFwidget.document to any
subsequent widgets you create) or use a ScrolledPDFWidget.
'''
def __init__(self, url, document=None, pageno=1, dpi=72, parent=None,
load_cb=None):
'''
load_cb: will be called when the document is loaded.
'''
super(PDFWidget, self).__init__(parent)
self.filename = url
self.load_cb = load_cb
self.network_manager = None
self.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
if not document:
self.document = None
if url:
self.start_load(url)
else:
self.document = document
self.page = None
self.pagesize = QSize(600, 800)
self.dpi = dpi
# Poppler page numbering starts from 0 but that's not what
# most PDF users will expect, so subtract:
if pageno > 0:
pageno -= 1
self.pageno = pageno
self.render()
def sizeHint(self):
if not self.page:
if not self.document:
return QSize(600, 800)
self.page = self.document.page(self.pageno)
if not self.pagesize:
self.pagesize = self.page.pageSize()
return self.pagesize
def render(self):
'''Render to a pixmap at the current DPI setting.
'''
if not self.document:
return
if not self.page:
self.page = self.document.page(self.pageno)
self.pagesize = self.page.pageSize()
self.document.setRenderHint(Poppler.Document.TextAntialiasing)
# self.document.setRenderHint(Poppler.Document.TextHinting)
# Most Qt5 programs seem to use setGeometry(x, y, w, h)
# to set initial window size. resize() is the only method I've
# found that doesn't force initial position as well as size.
# self.resize(self.pagesize.width() * self.dpi/POINTS_PER_INCH,
# self.pagesize.height() * self.dpi/POINTS_PER_INCH)
self.setWindowTitle('PDF Viewer')
img = self.page.renderToImage(self.dpi, self.dpi)
self.pixmap = QPixmap.fromImage(img)
self.setPixmap(self.pixmap)
def start_load(self, url):
'''Create a Poppler.Document from the given URL, QUrl or filename.
Return, then asynchronously call self.load_cb.
'''
# If it's not a local file, we'll need to load it.
# http://doc.qt.io/qt-5/qnetworkaccessmanager.html
qurl = QUrl(url)
if not qurl.scheme():
qurl = QUrl.fromLocalFile(url)
if not self.network_manager:
self.network_manager = QNetworkAccessManager();
self.network_manager.finished.connect(self.download_finished)
self.network_manager.get(QNetworkRequest(qurl))
def download_finished(self, network_reply):
qbytes = network_reply.readAll()
self.document = Poppler.Document.loadFromData(qbytes)
self.render()
if self.load_cb:
self.load_cb()
class PDFScrolledWidget(QScrollArea): # inherit from QScrollArea?
'''
Show all pages of a PDF, with scrollbars.
'''
def __init__(self, filename, dpi=72, parent=None):
super(PDFScrolledWidget, self).__init__(parent)
self.loaded = False
self.vscrollbar = None
self.setWidgetResizable(True)
# Try to eliminate the guesswork in sizing the window to content:
self.setFrameShape(self.NoFrame)
# I would like to set both scrollbars to AsNeeded:
# but if the vertical scrollbar is AsNeeded, the content
# gets loaded initially, then the QScrollArea decides it
# needs a vertical scrollbar, adds it and resizes the content
# inside, so everything gets scaled by 0.98.
self.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded)
self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
# Create a widget inside the scroller for the VBox layout to use:
self.scroll_content = QWidget()
self.setWidget(self.scroll_content)
# A VBox to lay out all the pages vertically:
self.scroll_layout = QVBoxLayout(self.scroll_content)
self.scroll_layout.setContentsMargins(0, 0, 0, 0)
# Create the widget for the first page of the PDF,
# which will also create the Poppler document we'll use
# to render the other pages.
self.pages = [ PDFWidget(filename, document=None, pageno=1, dpi=dpi,
load_cb = self.load_cb) ]
self.pages[0].setContentsMargins(0, 0, 0, 0)
self.pages[0].setFrameShape(self.NoFrame)
# Add page 1 to the vertical layout:
self.scroll_layout.addWidget(self.pages[0])
def load_cb(self):
page1 = self.pages[0]
width = page1.width()
height = page1.height()
if self.vscrollbar:
sbw = self.vscrollbar.width()
width += sbw
height += sbw
for p in range(2, page1.document.numPages()):
pagew = PDFWidget(page1.filename, document=page1.document,
pageno=p, dpi=page1.dpi)
pagew.setContentsMargins(0, 0, 0, 0)
# pagew.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored)
self.scroll_layout.addWidget(pagew)
self.pages.append(pagew)
self.scroll_layout.addStretch(1)
# Now there's a size. Set the initial page size to be big enough
# to show one page, including room for scrollbars, at 72 DPI.
self.resizeToFitContent()
# Don't set the loaded flag until after the first set of resizes,
# so resizeEvent() won't zoom.
self.loaded = True
def resizeToFitContent(self):
'''Resize to be wide enough not to show a horizontal scrollbar,
and just a little taller than the first page of PDF content.
'''
if not self.vscrollbar:
self.vscrollbar = self.verticalScrollBar()
if self.vscrollbar:
vscrollbarwidth = self.vscrollbar.width()
else:
vscrollbarwidth = 14
# Getting the size of a widget is tricky.
# self.widget().width(), for some reason, gives the width minus 12
# pixels, while self.widget().sizeHint().width() gives the
# correct size.
# So that means, apparently, if you want the margins of a widget
# you can get them by subtracting width() and height() from sizeHint().
# print("widget width is", self.widget().width(),
# ", sizehint", self.widget().sizeHint().width())
width = self.widget().sizeHint().width() + vscrollbarwidth
height = self.pages[0].pagesize.height() + vscrollbarwidth
self.resize(width, height)
# def showEvent(self, event):
def resizeEvent(self, event):
'''On resizes after the initial resize,
re-render the PDF to fit the new width.
'''
oldWidth = event.oldSize().width()
newWidth = event.size().width()
if oldWidth > 0 and self.loaded:
self.zoom(newWidth / oldWidth)
super(PDFScrolledWidget, self).resizeEvent(event)
def zoom(self, frac=1.25):
'''Zoom the page by the indicated fraction.
'''
for page in self.pages:
# Resize according to width, ignoring height.
page.dpi *= frac
page.render()
def unzoom(self, frac=.8):
'''Zoom the page by the indicated fraction.
Same as unzoom but with a default that zooms out instead of in.
'''
self.zoom(frac)
if __name__ == '__main__':
#
# PyQt is super crashy. Any little error, like an extra argument in a slot,
# causes it to kill Python with a core dump.
# Setting sys.excepthook works around this , and execution continues.
#
def excepthook(excType=None, excValue=None, tracebackobj=None, *,
message=None, version_tag=None, parent=None):
# print("exception! excValue='%s'" % excValue)
# logging.critical(''.join(traceback.format_tb(tracebackobj)))
# logging.critical('{0}: {1}'.format(excType, excValue))
traceback.print_exception(excType, excValue, tracebackobj)
sys.excepthook = excepthook
app = QApplication(sys.argv)
# It's helpful to know screen size, to choose appropriate DPI.
# XXX It's currently ignored but will eventually be used.
desktops = QApplication.desktop()
geometry = desktops.screenGeometry(desktops.screenNumber())
# print("screen geometry is", geometry.width(), geometry.height())
w = PDFScrolledWidget(sys.argv[1])
# w = PDFWidget(sys.argv[1])
QShortcut("Ctrl+Q", w, activated=w.close)
w.show()
sys.exit(app.exec_())