pdfjs: actually wait until the file is loaded
Otherwise we get incomplete replies and thus no pdf to view.
This commit is contained in:
parent
8b141037ef
commit
3dfaab6194
@ -219,6 +219,12 @@ class BrowserPage(QWebPage):
|
|||||||
q.deleteLater()
|
q.deleteLater()
|
||||||
return q.answer
|
return q.answer
|
||||||
|
|
||||||
|
def _show_pdfjs(self, reply):
|
||||||
|
"""Show the reply with pdfjs."""
|
||||||
|
html_page = _generate_pdfjs(reply).encode('utf-8')
|
||||||
|
self.mainFrame().setContent(html_page, 'text/html', reply.url())
|
||||||
|
reply.deleteLater()
|
||||||
|
|
||||||
def shutdown(self):
|
def shutdown(self):
|
||||||
"""Prepare the web page for being deleted."""
|
"""Prepare the web page for being deleted."""
|
||||||
self._is_shutting_down = True
|
self._is_shutting_down = True
|
||||||
@ -309,12 +315,10 @@ class BrowserPage(QWebPage):
|
|||||||
elif (mimetype in {'application/pdf', 'application/x-pdf'} and
|
elif (mimetype in {'application/pdf', 'application/x-pdf'} and
|
||||||
config.get('content', 'enable-pdfjs')):
|
config.get('content', 'enable-pdfjs')):
|
||||||
# Use pdf.js to display the page
|
# Use pdf.js to display the page
|
||||||
html_page = _generate_pdfjs(reply)
|
if reply.isFinished():
|
||||||
url = reply.url()
|
self._show_pdfjs(reply)
|
||||||
#url = QUrl('qute://pdfjs/web/viewer.js')
|
else:
|
||||||
self.mainFrame().setContent(html_page.encode('utf-8'),
|
reply.finished.connect(lambda: self._show_pdfjs(reply))
|
||||||
'text/html', url)
|
|
||||||
reply.deleteLater()
|
|
||||||
else:
|
else:
|
||||||
# Unknown mimetype, so download anyways.
|
# Unknown mimetype, so download anyways.
|
||||||
download_manager.fetch(reply,
|
download_manager.fetch(reply,
|
||||||
@ -605,16 +609,17 @@ def _generate_pdfjs(reply):
|
|||||||
"""
|
"""
|
||||||
script = io.StringIO()
|
script = io.StringIO()
|
||||||
script.write('var data = new Uint8Array([\n')
|
script.write('var data = new Uint8Array([\n')
|
||||||
while True:
|
data = reply.readAll()
|
||||||
data = reply.read(1024 * 1024 * 1024) # read 1 MB in advance
|
|
||||||
if not data:
|
newline = 0
|
||||||
break
|
# Iterating over QByteArray directly will somehow try to decode as ASCII
|
||||||
newline = 0
|
# which will fail, so we need .data() here
|
||||||
for byte in data:
|
for byte in data.data():
|
||||||
newline += script.write('{},'.format(byte))
|
newline += script.write('{},'.format(byte))
|
||||||
if newline > 75:
|
if newline > 75:
|
||||||
script.write('\n')
|
script.write('\n')
|
||||||
newline = 0
|
newline = 0
|
||||||
|
|
||||||
script.write("""]);
|
script.write("""]);
|
||||||
PDFJS.getDocument(data).then(function(pdf) {
|
PDFJS.getDocument(data).then(function(pdf) {
|
||||||
PDFView.load(pdf);
|
PDFView.load(pdf);
|
||||||
|
Loading…
Reference in New Issue
Block a user