Merge branch 'pdfjs-fix'

This commit is contained in:
Florian Bruhin 2023-12-04 10:49:30 +01:00
commit 6e6e9d285e
4 changed files with 50 additions and 11 deletions

View File

@ -61,11 +61,11 @@ def generate_pdfjs_page(filename, url):
html = html.replace('</body>',
'</body><script>{}</script>'.format(script))
# WORKAROUND for the fact that PDF.js tries to use the Fetch API even with
# qute:// URLs.
pdfjs_script = '<script src="../build/pdf.js"></script>'
html = html.replace(pdfjs_script,
'<script>window.Response = undefined;</script>\n' +
pdfjs_script)
# qute:// URLs, this is probably no longer needed in PDFjs 4+. See #4235
html = html.replace(
'<head>',
'<head>\n<script>window.Response = undefined;</script>\n'
)
return html
@ -202,10 +202,24 @@ def _read_from_system(system_path, names):
return (None, None)
def get_pdfjs_js_path():
"""Checks for pdf.js main module availability and returns the path if available."""
paths = ['build/pdf.js', 'build/pdf.mjs']
for path in paths:
try:
get_pdfjs_res(path)
except PDFJSNotFound:
pass
else:
return path
raise PDFJSNotFound(" or ".join(paths))
def is_available():
"""Return true if a pdfjs installation is available."""
"""Return true if certain parts of a pdfjs installation are available."""
try:
get_pdfjs_res('build/pdf.js')
get_pdfjs_js_path()
get_pdfjs_res('web/viewer.html')
except PDFJSNotFound:
return False

View File

@ -480,7 +480,7 @@ def _pdfjs_version() -> str:
A string with the version number.
"""
try:
pdfjs_file, file_path = pdfjs.get_pdfjs_res_and_path('build/pdf.js')
pdfjs_file, file_path = pdfjs.get_pdfjs_res_and_path(pdfjs.get_pdfjs_js_path())
except pdfjs.PDFJSNotFound:
return 'no'
else:

View File

@ -193,6 +193,33 @@ def test_is_available(available, mocker):
assert pdfjs.is_available() == available
@pytest.mark.parametrize('found_file', [
"build/pdf.js",
"build/pdf.mjs",
])
def test_get_pdfjs_js_path(found_file: str, monkeypatch: pytest.MonkeyPatch):
def fake_pdfjs_res(requested):
if requested.endswith(found_file):
return
raise pdfjs.PDFJSNotFound(requested)
monkeypatch.setattr(pdfjs, 'get_pdfjs_res', fake_pdfjs_res)
assert pdfjs.get_pdfjs_js_path() == found_file
def test_get_pdfjs_js_path_none(monkeypatch: pytest.MonkeyPatch):
def fake_pdfjs_res(requested):
raise pdfjs.PDFJSNotFound(requested)
monkeypatch.setattr(pdfjs, 'get_pdfjs_res', fake_pdfjs_res)
with pytest.raises(
pdfjs.PDFJSNotFound,
match="Path 'build/pdf.js or build/pdf.mjs' not found"
):
pdfjs.get_pdfjs_js_path()
@pytest.mark.parametrize('mimetype, url, enabled, expected', [
# PDF files
('application/pdf', 'http://www.example.com', True, True),

View File

@ -885,9 +885,7 @@ class TestPDFJSVersion:
def test_real_file(self, data_tmpdir):
"""Test against the real file if pdfjs was found."""
try:
pdfjs.get_pdfjs_res_and_path('build/pdf.js')
except pdfjs.PDFJSNotFound:
if not pdfjs.is_available():
pytest.skip("No pdfjs found")
ver = version._pdfjs_version()
assert ver.split()[0] not in ['no', 'unknown'], ver