Skip to content

Commit ce65ecb

Browse files
committedAug 9, 2019
- enhance operations synchronization (see pendingOperation Promise)
- update pdfjs API usage - fix annotationLayer scale - enhance canvas style management
1 parent 595b516 commit ce65ecb

File tree

2 files changed

+87
-63
lines changed

2 files changed

+87
-63
lines changed
 

‎src/componentFactory.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ export default function(pdfjsWrapper) {
1010
render: function(h) {
1111
return h('span', {
1212
attrs: {
13-
style: 'position: relative; display: inline-block'
13+
style: 'position: relative; display: block'
1414
}
1515
}, [
1616
h('canvas', {
1717
attrs: {
18-
style: 'display: inline-block; width: 100%; vertical-align: top',
18+
style: 'display: inline-block; width: 100%; height: 100%; vertical-align: top',
1919
},
2020
ref:'canvas'
2121
}),
2222
h('span', {
23-
style: 'display: inline-block; width: 100%',
23+
style: 'display: inline-block; width: 100%; height: 100%',
2424
class: 'annotationLayer',
2525
ref:'annotationLayer'
2626
}),
@@ -75,7 +75,7 @@ export default function(pdfjsWrapper) {
7575
if ( resolutionScale < 0.85 || resolutionScale > 1.15 )
7676
this.pdf.renderPage(this.rotate);
7777

78-
this.$refs.annotationLayer.style.transform = 'scale('+resolutionScale+')';
78+
// this.$refs.annotationLayer.style.transform = 'scale('+resolutionScale+')';
7979
},
8080
print: function(dpi, pageList) {
8181

‎src/pdfjsWrapper.js

+83-59
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { PDFLinkService } from 'pdfjs-dist/lib/web/pdf_link_service';
22

3+
var pendingOperation = Promise.resolve();
4+
35
export default function(PDFJS) {
46

57
function isPDFDocumentLoadingTask(obj) {
@@ -19,6 +21,10 @@ export default function(PDFJS) {
1921
else
2022
throw new TypeError('invalid src type');
2123

24+
// source.verbosity = PDFJS.VerbosityLevel.INFOS;
25+
// source.pdfBug = true;
26+
// source.stopAtErrors = true;
27+
2228
var loadingTask = PDFJS.getDocument(source);
2329
loadingTask.__PDFDocumentLoadingTask = true; // since PDFDocumentLoadingTask is not public
2430

@@ -56,7 +62,9 @@ export default function(PDFJS) {
5662

5763
if ( pdfDoc === null )
5864
return;
59-
pdfDoc.destroy();
65+
66+
// Aborts all network requests and destroys worker.
67+
pendingOperation = pdfDoc.destroy();
6068
pdfDoc = null;
6169
}
6270

@@ -105,7 +113,7 @@ export default function(PDFJS) {
105113
return pdfDoc.getPage(1)
106114
.then(function(page) {
107115

108-
var viewport = page.getViewport(1);
116+
var viewport = page.getViewport({ scale: 1 });
109117
win.document.head.appendChild(win.document.createElement('style')).textContent =
110118
'@supports ((size:A4) and (size:1pt 1pt)) {' +
111119
'@page { margin: 1pt; size: ' + ((viewport.width * PRINT_UNITS) / CSS_UNITS) + 'pt ' + ((viewport.height * PRINT_UNITS) / CSS_UNITS) + 'pt; }' +
@@ -137,7 +145,7 @@ export default function(PDFJS) {
137145
pdfDoc.getPage(pageNumber)
138146
.then(function(page) {
139147

140-
var viewport = page.getViewport(1);
148+
var viewport = page.getViewport({ scale: 1 });
141149

142150
var printCanvasElt = win.document.body.appendChild(win.document.createElement('canvas'));
143151
printCanvasElt.width = (viewport.width * PRINT_UNITS);
@@ -190,8 +198,8 @@ export default function(PDFJS) {
190198

191199
rotate = (pdfPage.rotate === undefined ? 0 : pdfPage.rotate) + (rotate === undefined ? 0 : rotate);
192200

193-
var scale = canvasElt.offsetWidth / pdfPage.getViewport(1).width * (window.devicePixelRatio || 1);
194-
var viewport = pdfPage.getViewport(scale, rotate);
201+
var scale = canvasElt.offsetWidth / pdfPage.getViewport({ scale: 1 }).width * (window.devicePixelRatio || 1);
202+
var viewport = pdfPage.getViewport({ scale, rotation:rotate });
195203

196204
emitEvent('page-size', viewport.width, viewport.height);
197205

@@ -216,36 +224,44 @@ export default function(PDFJS) {
216224
linkService.setDocument(pdfDoc);
217225
linkService.setViewer(viewer);
218226

219-
pdfPage.getAnnotations({ intent: 'display' })
220-
.then(function(annotations) {
227+
pendingOperation = pendingOperation.then(function() {
221228

222-
PDFJS.AnnotationLayer.render({
223-
viewport: viewport.clone({ dontFlip: true }),
224-
div: annotationLayerElt,
225-
annotations: annotations,
226-
page: pdfPage,
227-
linkService: linkService,
228-
renderInteractiveForms: false
229-
});
230-
});
229+
var getAnnotationsOperation =
230+
pdfPage.getAnnotations({ intent: 'display' })
231+
.then(function(annotations) {
231232

232-
pdfRender
233-
.then(function() {
234-
annotationLayerElt.style.visibility = '';
235-
canceling = false;
236-
pdfRender = null;
237-
})
238-
.catch(function(err) {
233+
PDFJS.AnnotationLayer.render({
234+
viewport: viewport.clone({ dontFlip: true }),
235+
div: annotationLayerElt,
236+
annotations: annotations,
237+
page: pdfPage,
238+
linkService: linkService,
239+
renderInteractiveForms: false
240+
});
241+
});
239242

240-
pdfRender = null;
241-
if ( err instanceof PDFJS.RenderingCancelledException ) {
243+
var pdfRenderOperation =
244+
pdfRender.promise
245+
.then(function() {
242246

247+
annotationLayerElt.style.visibility = '';
243248
canceling = false;
244-
this.renderPage(rotate);
245-
return;
246-
}
247-
emitEvent('error', err);
248-
}.bind(this))
249+
pdfRender = null;
250+
})
251+
.catch(function(err) {
252+
253+
pdfRender = null;
254+
if ( err instanceof PDFJS.RenderingCancelledException ) {
255+
256+
canceling = false;
257+
this.renderPage(rotate);
258+
return;
259+
}
260+
emitEvent('error', err);
261+
}.bind(this))
262+
263+
return Promise.all([getAnnotationsOperation, pdfRenderOperation]);
264+
}.bind(this));
249265
}
250266

251267

@@ -273,7 +289,10 @@ export default function(PDFJS) {
273289
if ( pdfDoc === null )
274290
return;
275291

276-
pdfDoc.getPage(pageNumber)
292+
pendingOperation = pendingOperation.then(function() {
293+
294+
return pdfDoc.getPage(pageNumber);
295+
})
277296
.then(function(page) {
278297

279298
pdfPage = page;
@@ -303,40 +322,45 @@ export default function(PDFJS) {
303322
return;
304323
}
305324

306-
if ( isPDFDocumentLoadingTask(src) ) {
325+
// wait for pending operation ends
326+
pendingOperation = pendingOperation.then(function() {
307327

308-
if ( src.destroyed ) {
309-
310-
emitEvent('error', new Error('loadingTask has been destroyed'));
311-
return
312-
}
328+
var loadingTask;
329+
if ( isPDFDocumentLoadingTask(src) ) {
313330

314-
var loadingTask = src;
315-
} else {
331+
if ( src.destroyed ) {
316332

317-
var loadingTask = createLoadingTask(src, {
318-
onPassword: function(updatePassword, reason) {
333+
emitEvent('error', new Error('loadingTask has been destroyed'));
334+
return
335+
}
319336

320-
var reasonStr;
321-
switch (reason) {
322-
case PDFJS.PasswordResponses.NEED_PASSWORD:
323-
reasonStr = 'NEED_PASSWORD';
324-
break;
325-
case PDFJS.PasswordResponses.INCORRECT_PASSWORD:
326-
reasonStr = 'INCORRECT_PASSWORD';
327-
break;
337+
loadingTask = src;
338+
} else {
339+
340+
loadingTask = createLoadingTask(src, {
341+
onPassword: function(updatePassword, reason) {
342+
343+
var reasonStr;
344+
switch (reason) {
345+
case PDFJS.PasswordResponses.NEED_PASSWORD:
346+
reasonStr = 'NEED_PASSWORD';
347+
break;
348+
case PDFJS.PasswordResponses.INCORRECT_PASSWORD:
349+
reasonStr = 'INCORRECT_PASSWORD';
350+
break;
351+
}
352+
emitEvent('password', updatePassword, reasonStr);
353+
},
354+
onProgress: function(status) {
355+
356+
var ratio = status.loaded / status.total;
357+
emitEvent('progress', Math.min(ratio, 1));
328358
}
329-
emitEvent('password', updatePassword, reasonStr);
330-
},
331-
onProgress: function(status) {
332-
333-
var ratio = status.loaded / status.total;
334-
emitEvent('progress', Math.min(ratio, 1));
335-
}
336-
});
337-
}
359+
});
360+
}
338361

339-
loadingTask
362+
return loadingTask.promise;
363+
})
340364
.then(function(pdf) {
341365

342366
pdfDoc = pdf;

0 commit comments

Comments
 (0)
Please sign in to comment.