pdfjs: don't embed data in html

We don't need more than one copy of the pdf.
This commit is contained in:
Daniel Schadt 2015-11-12 22:49:50 +01:00
parent d89b1f4125
commit 0811418746
2 changed files with 9 additions and 63 deletions

View File

@ -221,8 +221,7 @@ class BrowserPage(QWebPage):
def _show_pdfjs(self, reply):
"""Show the reply with pdfjs."""
data = reply.readAll().data()
script = _generate_pdfjs_script(data)
script = _generate_pdfjs_script(reply.url())
viewer = utils.read_file('pdfjs/web/viewer.html')
html_page = viewer.replace('%% QUTE_SCRIPT_CONTENT %%', script)
html_page = html_page.encode('utf-8')
@ -319,10 +318,7 @@ class BrowserPage(QWebPage):
elif (mimetype in {'application/pdf', 'application/x-pdf'} and
config.get('content', 'enable-pdfjs')):
# Use pdf.js to display the page
if reply.isFinished():
self._show_pdfjs(reply)
else:
reply.finished.connect(lambda: self._show_pdfjs(reply))
self._show_pdfjs(reply)
else:
# Unknown mimetype, so download anyways.
download_manager.fetch(reply,
@ -605,27 +601,14 @@ class BrowserPage(QWebPage):
return True
def _generate_pdfjs_script(data):
def _generate_pdfjs_script(url):
"""Generate the script that shows the pdf with pdf.js.
Args:
data: The binary data of the pdf page.
url: The url of the pdf page as QUrl.
"""
script = io.StringIO()
script.write('var data = new Uint8Array([\n')
newline = 0
for byte in data:
newline += script.write('{},'.format(byte))
if newline > 75:
script.write('\n')
newline = 0
script.write("]);\n"
"PDFJS.getDocument(data).then(function(pdf) {\n"
" PDFView.load(pdf);\n"
"});"
)
source = script.getvalue()
script.close()
return source
return (
'PDFJS.getDocument("{url}").then(function(pdf) {{\n'
' PDFView.load(pdf);\n'
'}});'
).format(url=url.toString())

View File

@ -1,37 +0,0 @@
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
# Copyright 2015 Daniel Schadt
#
# This file is part of qutebrowser.
#
# qutebrowser is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# qutebrowser is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
"""Tests for browser.webpage."""
import textwrap
from qutebrowser.browser import webpage
def test_generate_pdfjs_script():
data = b'\x00foobar\xFF'
expected = textwrap.dedent("""
var data = new Uint8Array([
0,102,111,111,98,97,114,255,]);
PDFJS.getDocument(data).then(function(pdf) {
PDFView.load(pdf);
});
""")
actual = webpage._generate_pdfjs_script(data)
assert actual.strip() == expected.strip()