reorder pdfjs generation code

This commit is contained in:
Daniel Schadt 2015-11-12 21:25:10 +01:00
parent 3dfaab6194
commit e6548dd9e7

View File

@ -221,7 +221,11 @@ class BrowserPage(QWebPage):
def _show_pdfjs(self, reply): def _show_pdfjs(self, reply):
"""Show the reply with pdfjs.""" """Show the reply with pdfjs."""
html_page = _generate_pdfjs(reply).encode('utf-8') data = reply.readAll().data()
script = _generate_pdfjs_script(data)
viewer = utils.read_file('pdfjs/web/viewer.html')
html_page = viewer.replace('%% QUTE_SCRIPT_CONTENT %%', script)
html_page = html_page.encode('utf-8')
self.mainFrame().setContent(html_page, 'text/html', reply.url()) self.mainFrame().setContent(html_page, 'text/html', reply.url())
reply.deleteLater() reply.deleteLater()
@ -601,31 +605,27 @@ class BrowserPage(QWebPage):
return True return True
def _generate_pdfjs(reply): def _generate_pdfjs_script(data):
"""Generate a HTML page that includes pdf.js to show the given data. """Generate the script that shows the pdf with pdf.js.
Args: Args:
reply: The QNetworkReply. data: The binary data of the pdf page.
""" """
script = io.StringIO() script = io.StringIO()
script.write('var data = new Uint8Array([\n') script.write('var data = new Uint8Array([\n')
data = reply.readAll()
newline = 0 newline = 0
# Iterating over QByteArray directly will somehow try to decode as ASCII for byte in data:
# which will fail, so we need .data() here
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("]);\n"
PDFJS.getDocument(data).then(function(pdf) { "PDFJS.getDocument(data).then(function(pdf) {\n"
PDFView.load(pdf); " PDFView.load(pdf);\n"
}); "});"
""") )
viewer = utils.read_file('pdfjs/web/viewer.html') source = script.getvalue()
source = viewer.replace('%% QUTE_SCRIPT_CONTENT %%', script.getvalue())
script.close() script.close()
return source return source