Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade viewer to PDF.js v1.6.210 #10

Merged
merged 10 commits into from
Mar 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
220 changes: 4 additions & 216 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,231 +43,19 @@ Include `viewer.html` using [SSI](http://httpd.apache.org/docs/2.4/howto/ssi.htm

## Upgrading the source

Normally mozilla's PDF js viewer, will only run as standalone. This version is patched so you can include it within a
Normally mozilla's PDF js viewer, will only run as standalone. We forked the project and patched it, so you can include it within a
page.

To update this version, get the pdf.js source code and build the project
To update this version, get the patched pdf.js source code and build the project

git clone https://github.com/mozilla/pdf.js.git
git https://github.com/legalthings/pdf.js.git
cd pdf.js
npm install
node make generic
gulp generic
cd ..

And update the files from source and patch them

cd pdf.js-viewer
npm install
./build.sh ../pdf.js/build/generic/

### Manual patching

When updating to a new minor (or major) version, it's likely than one or more of the chunks can't be applied. This
means you need to do these modifications manually.


#### function getL10nData()

The viewer uses `l10n.js` with a `<link rel="resource" type="application/l10n">` header for internationalization. This
chunk makes using that optional.

function getL10nData(key, args, fallback) {
var data = gL10nData[key];
if (!data) {
- console.warn('#' + key + ' is undefined.');
+ if (Object.keys(gL10nData).length > 0) {
+ console.warn('#' + key + ' is undefined.');
+ }
if (!fallback) {
return null;
}

#### Dynamic paths

The viewer uses relative paths to JavaScript files. This doesn't work when the viewer is embedded on a web page.
Instead the paths are determined based on the path of the current JavaScript file.

-PDFJS.imageResourcesPath = './images/';
- PDFJS.workerSrc = '../build/pdf.worker.js';
- PDFJS.cMapUrl = '../web/cmaps/';
- PDFJS.cMapPacked = true;
+var scriptTagContainer = document.body ||
+ document.getElementsByTagName('head')[0];
+var pdfjsSrc = scriptTagContainer.lastChild.src;
+
+if (pdfjsSrc) {
+ PDFJS.imageResourcesPath = pdfjsSrc.replace(/pdf\.js$/i, 'images/');
+ PDFJS.workerSrc = pdfjsSrc.replace(/pdf\.js$/i, 'pdf.worker.js');
+ PDFJS.cMapUrl = pdfjsSrc.replace(/pdf\.js$/i, 'cmaps/');
+}
+
+PDFJS.cMapPacked = true;

#### Explicitly load a PDF document

The viewer shouldn't start loading a (default) document when it's loaded. Instead we want to expose the initialization,
so it can be called in JavaScript with `PDFJS.webViewerLoad()`.

-document.addEventListener('DOMContentLoaded', webViewerLoad, true);
+// document.addEventListener('DOMContentLoaded', webViewerLoad, true);
+PDFJS.webViewerLoad = function (src) {
+ if (src) DEFAULT_URL = src;
+
+ webViewerLoad();
+}

On several places the code assumes that a PDF is loaded, which (because of the explicit load) might not be the case. We
need to check if `pdfDocument` is set before using it.

##### PDFViewerApplication.pagesCount() and PDFLinkService.pagesCount()

get pagesCount() {
- return this.pdfDocument.numPages;
+ return this.pdfDocument ? this.pdfDocument.numPages : 0;
},

_The pagesCount method for both `PDFViewerApplication` and `PDFLinkService`. Both need to be patched._

##### PDFViewerApplication.cleanup()

cleanup: function pdfViewCleanup() {
this.pdfViewer.cleanup();
this.pdfThumbnailViewer.cleanup();
- this.pdfDocument.cleanup();
+ if (this.pdfDocument) {
+ this.pdfDocument.cleanup();
+ }
},

#### overlayManagerRegister

The overlay is registered when the viewer is loaded. The original code will only do this once and give an error on each
subsequent call. Escpecially with single-page applications (eg an Angular app), the viewer may be loaded multiple times.
This patch causes pdf.js to unregister an unusued (closed) overlay.

register: function overlayManagerRegister(name, callerCloseMethod, canForceClose) {
return new Promise(function (resolve) {
var element, container;
if (!name || !(element = document.getElementById(name)) ||
!(container = element.parentNode)) {
throw new Error('Not enough parameters.');
} else if (this.overlays[name]) {
- throw new Error('The overlay is already registered.');
+ if (this.active !== name) {
+ this.unregister(name);
+ } else {
+ throw new Error('The overlay is already registered and active.');
+ }
}

#### webViewerChange trigger on file upload dialog

Whenever a file dialog is used (so with any `<input type="file">` on the page), the file is loaded into the pdf.js
viewer. We don't want this behaviour, so comment it out.

-window.addEventListener('change', function webViewerChange(evt) {
+/*window.addEventListener('change', function webViewerChange(evt) {
var files = evt.target.files;
if (!files || files.length === 0) {
return;
@@ -17627,7 +17647,7 @@
setAttribute('hidden', 'true');
document.getElementById('download').setAttribute('hidden', 'true');
document.getElementById('secondaryDownload').setAttribute('hidden', 'true');
-}, true);
+}, true);*/

#### handleMouseWheel()

The JavaScript pdf.js file might be loaded while the viewer isn't being displayed. This causes an error on mouse move.
We need to check if the viewer is initialized, before handling the event.

function handleMouseWheel(evt) {
+ // Ignore mousewheel event if pdfViewer isn't loaded
+ if (!PDFViewerApplication.pdfViewer) return;

#### Load code for worker using AJAX if needed

A [Web Worker](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers) can't use code from
a same-origin domain. The CORS headers don't apply.

he patch will cause pdf.js to first try to create the Worker the regular way, with a URL to the JavaScript source. If
this fails, the source if fetched using AJAX and used to create an
[object url](https://developer.mozilla.org/en-US/docs/Web/API/URL/createObjectURL). If this also fails, pdf.js will go
onto it's last resort by calling `setupFakeWorker()`.

+ /**
+ * Needed because workers cannot load scripts outside of the current origin (as of firefox v45).
+ * This patch does require the worker script to be served with a (Access-Control-Allow-Origin: *) header
+ * @patch
+ */
+ var loadWorkerXHR = function(){
+ var url = PDFJS.workerSrc;
+ var jsdfd = PDFJS.createPromiseCapability();
+
+ if (url.match(/^blob:/) || typeof URL.createObjectURL === 'undefined') {
+ jsdfd.reject(); // Failed loading using blob
+ }
+
+ var xmlhttp;
+ xmlhttp = new XMLHttpRequest();
+
+ xmlhttp.onreadystatechange = function(){
+ if (xmlhttp.readyState != 4) return;
+
+ if (xmlhttp.status == 200) {
+ info('Loaded worker source through XHR.');
+ var workerJSBlob = new Blob([xmlhttp.responseText], { type: 'text/javascript' });
+ jsdfd.resolve(window.URL.createObjectURL(workerJSBlob));
+ } else {
+ jsdfd.reject();
+ }
+ };
+
+ xmlhttp.open('GET', url, true);
+ xmlhttp.send();
+ return jsdfd.promise;
+ }
+
+ var workerError = function() {
+ loadWorkerXHR().then(function(blob) {
+ PDFJS.workerSrc = blob;
+ loadWorker();
+ }, function() {
+ this.setupFakeWorker();
+ }.bind(this));
+ }.bind(this);
+

- if (!globalScope.PDFJS.disableWorker && typeof Worker !== 'undefined') {
+ var loadWorker = function() {
var workerSrc = PDFJS.workerSrc;
if (!workerSrc) {
error('No PDFJS.workerSrc specified');
@@ -3559,6 +3603,8 @@
// Some versions of FF can't create a worker on localhost, see:
// https://bugzilla.mozilla.org/show_bug.cgi?id=683280
var worker = new Worker(workerSrc);
+ worker.onerror = workerError;
+
var messageHandler = new MessageHandler('main', worker);
this.messageHandler = messageHandler;

@@ -3589,11 +3635,16 @@
return;
} catch (e) {
info('The worker has been disabled.');
+ workerError();
}
- }
+ }.bind(this);
// Either workers are disabled, not supported or have thrown an exception.
// Thus, we fallback to a faked worker.
- this.setupFakeWorker();
+ if (!globalScope.PDFJS.disableWorker && typeof Worker !== 'undefined') {
+ loadWorker();
+ } else {
+ this.setupFakeWorker();
+ }
}

13 changes: 6 additions & 7 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,16 @@ if [[ ! -f "$source/build/pdf.js" ]]; then
exit 1
fi

cat "$source/web/l10n.js" "$source/build/pdf.js" "$source/web/compatibility.js" "$source/web/debugger.js" "$source/web/viewer.js" > pdf.js
patch pdf.js < pdf.js.patch
cat "$source/web/l10n.js" "$source/build/pdf.js" "$source/web/debugger.js" "$source/web/viewer.js" > pdf.js

cp "$source/build/pdf.worker.js" .
patch pdf.worker.js < pdf.worker.js.patch

uglifycss "$source/web/viewer.css" > viewer.css
cat "$source/web/viewer.html" | tr '\n' '\f' | sed -r 's/^.+<body [^>]*>/<pdfjs-wrapper>/' | sed -r 's/<\/body>.+$/<\/pdfjs-wrapper>/' | tr '\f' '\n' > ./viewer.html

node node_modules/uglifycss/uglifycss "$source/web/viewer.css" > viewer.css
node grunt/css-prefix.js viewer.css viewer.css pdfjs
cat viewer-overwrites.css >> viewer.css;

sed -r 's/url\((")?images\//url\(\1@pdfjsImagePath\//g' < "$source/web/viewer.css" | uglifycss > viewer.less
node grunt/css-prefix.js viewer.less viewer.less pdfjs
sed -r 's/url\((")?images\//url\(\1@pdfjsImagePath\//g' < viewer.css > viewer.less

cp "$source/web/cmaps/" "$source/web/images/" "$source/web/locale/" . -a

Binary file modified images/texture.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/toolbarButton-menuArrows.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/toolbarButton-viewAttachments@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/toolbarButton-viewThumbnail@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/treeitem-collapsed-rtl.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/treeitem-collapsed-rtl@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/treeitem-collapsed.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/treeitem-collapsed@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/treeitem-expanded.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/treeitem-expanded@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 25 additions & 11 deletions locale/ach/viewer.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@ previous_label=Mukato
next.title=Pot buk malubo
next_label=Malubo

# LOCALIZATION NOTE (page_label, page_of):
# These strings are concatenated to form the "Page: X of Y" string.
# Do not translate "{{pageCount}}", it will be substituted with a number
# representing the total number of pages.
page_label=Pot buk:
page_of=pi {{pageCount}}
# LOCALIZATION NOTE (page.title): The tooltip for the pageNumber input.
page.title=Pot buk
# LOCALIZATION NOTE (of_pages): "{{pagesCount}}" will be replaced by a number
# representing the total number of pages in the document.
of_pages=pi {{pagesCount}}
# LOCALIZATION NOTE (page_of_pages): "{{pageNumber}}" and "{{pagesCount}}"
# will be replaced by a number representing the currently visible page,
# respectively a number representing the total number of pages in the document.
page_of_pages=({{pageNumber}} me {{pagesCount}})

zoom_out.title=Jwik Matidi
zoom_out_label=Jwik Matidi
Expand Down Expand Up @@ -67,28 +70,39 @@ document_properties.title=Jami me gin acoya…
document_properties_label=Jami me gin acoya…
document_properties_file_name=Nying pwail:
document_properties_file_size=Dit pa pwail:
# LOCALIZATION NOTE (document_properties_kb): "{{size_kb}}" and "{{size_b}}"
# will be replaced by the PDF file size in kilobytes, respectively in bytes.
document_properties_kb={{size_kb}} KB ({{size_b}} bytes)
# LOCALIZATION NOTE (document_properties_mb): "{{size_mb}}" and "{{size_b}}"
# will be replaced by the PDF file size in megabytes, respectively in bytes.
document_properties_mb={{size_mb}} MB ({{size_b}} bytes)
document_properties_title=Wiye:
document_properties_author=Ngat mucoyo:
document_properties_subject=Lok:
document_properties_keywords=Lok mapire tek:
document_properties_creation_date=Nino dwe me cwec:
document_properties_modification_date=Nino dwe me yub:
# LOCALIZATION NOTE (document_properties_date_string): "{{date}}" and "{{time}}"
# will be replaced by the creation/modification date, and time, of the PDF file.
document_properties_date_string={{date}}, {{time}}
document_properties_creator=Lacwec:
document_properties_producer=Layub PDF:
document_properties_version=Kit PDF:
document_properties_page_count=Kwan me pot buk:
document_properties_close=Lor

print_progress_message=Yubo coc me agoya…
# LOCALIZATION NOTE (print_progress_percent): "{{progress}}" will be replaced by
# a numerical per cent value.
print_progress_percent={{progress}}%
print_progress_close=Juki

# Tooltips and alt text for side panel toolbar buttons
# (the _label strings are alt text for the buttons, the .title strings are
# tooltips)
toggle_sidebar.title=Lok gintic ma inget
toggle_sidebar_label=Lok gintic ma inget
outline.title=Nyut rek pa gin acoya
outline_label=Pek pa gin acoya
document_outline_label=Pek pa gin acoya
attachments.title=Nyut twec
attachments_label=Twec
thumbs.title=Nyut cal
Expand Down Expand Up @@ -149,7 +163,7 @@ loading_error_indicator=Bal
loading_error=Bal otime kun cano PDF.
invalid_file_error=Pwail me PDF ma pe atir onyo obale woko.
missing_file_error=Pwail me PDF tye ka rem.
unexpected_response_error=Lagam mape kigeno pa lapok tic
unexpected_response_error=Lagam mape kigeno pa lapok tic.

# LOCALIZATION NOTE (text_annotation_type.alt): This is used as a tooltip.
# "{{type}}" will be replaced with an annotation type from a list defined in
Expand All @@ -159,9 +173,9 @@ text_annotation_type.alt=[{{type}} Lok angea manok]
password_label=Ket mung me donyo me yabo pwail me PDF man.
password_invalid=Mung me donyo pe atir. Tim ber i tem doki.
password_ok=OK
password_cancel=Juk
password_cancel=Juki

printing_not_supported=Ciko: Layeny ma pe teno goyo liweng.
printing_not_ready=Ciko: PDF pe ocane weng me agoya.
web_fonts_disabled=Kijuko dit pa coc me kakube woko: pe romo tic ki dit pa coc me PDF ma kiketo i kine.
document_colors_disabled=Pe ki ye ki gin acoya me PDF me tic ki rangi gi kengi: 'Ye pot buk me yero rangi mamegi kengi' kijuko woko i layeny.
document_colors_not_allowed=Pe ki yee ki gin acoya me PDF me tic ki rangi gi kengi: Kijuko woko “Yee pot buk me yero rangi mamegi kengi” ki i layeny.
Loading