@@ -379,6 +379,17 @@ var PDFView = {
379
379
return currentPageNumber ;
380
380
},
381
381
382
+ get supportsPrinting () {
383
+ var canvas = document .createElement ('canvas' );
384
+ var value = 'mozPrintCallback' in canvas ;
385
+ // shadow
386
+ Object .defineProperty (this , 'supportsPrinting' , { value : value ,
387
+ enumerable : true ,
388
+ configurable : true ,
389
+ writable : false });
390
+ return value ;
391
+ },
392
+
382
393
open : function pdfViewOpen (url , scale , password ) {
383
394
var parameters = {password : password };
384
395
if (typeof url === 'string' ) { // URL
@@ -1041,6 +1052,26 @@ var PDFView = {
1041
1052
params [unescape (key )] = unescape (value );
1042
1053
}
1043
1054
return params ;
1055
+ },
1056
+
1057
+ beforePrint : function pdfViewSetupBeforePrint () {
1058
+ if (!this .supportsPrinting ) {
1059
+ var printMessage = mozL10n .get ('printing_not_supported' , null ,
1060
+ 'Warning: Printing is not fully supported by this browser.' );
1061
+ this .error (printMessage );
1062
+ return ;
1063
+ }
1064
+ var body = document .querySelector ('body' );
1065
+ body .setAttribute ('data-mozPrintCallback' , true );
1066
+ for (var i = 0 , ii = this .pages .length ; i < ii ; ++i ) {
1067
+ this .pages [i ].beforePrint ();
1068
+ }
1069
+ },
1070
+
1071
+ afterPrint : function pdfViewSetupAfterPrint () {
1072
+ var div = document .getElementById ('printContainer' );
1073
+ while (div .hasChildNodes ())
1074
+ div .removeChild (div .lastChild );
1044
1075
}
1045
1076
};
1046
1077
@@ -1360,6 +1391,44 @@ var PageView = function pageView(container, pdfPage, id, scale,
1360
1391
div .setAttribute ('data-loaded' , true );
1361
1392
};
1362
1393
1394
+ this .beforePrint = function pageViewBeforePrint () {
1395
+ var pdfPage = this .pdfPage ;
1396
+ var viewport = pdfPage .getViewport (1 );
1397
+
1398
+ var canvas = this .canvas = document .createElement ('canvas' );
1399
+ canvas .width = viewport .width ;
1400
+ canvas .height = viewport .height ;
1401
+ canvas .style .width = viewport .width + 'pt' ;
1402
+ canvas .style .height = viewport .height + 'pt' ;
1403
+
1404
+ var printContainer = document .getElementById ('printContainer' );
1405
+ printContainer .appendChild (canvas );
1406
+
1407
+ var self = this ;
1408
+ canvas .mozPrintCallback = function (obj ) {
1409
+ var ctx = obj .context ;
1410
+ var renderContext = {
1411
+ canvasContext : ctx ,
1412
+ viewport : viewport
1413
+ };
1414
+
1415
+ pdfPage .render (renderContext ).then (function () {
1416
+ // Tell the printEngine that rendering this canvas/page has finished.
1417
+ obj .done ();
1418
+ self .pdfPage .destroy ();
1419
+ }, function (error ) {
1420
+ console .error (error );
1421
+ // Tell the printEngine that rendering this canvas/page has failed.
1422
+ // This will make the print proces stop.
1423
+ if ('abort' in object )
1424
+ obj .abort ();
1425
+ else
1426
+ obj .done ();
1427
+ self .pdfPage .destroy ();
1428
+ });
1429
+ };
1430
+ };
1431
+
1363
1432
this .updateStats = function pageViewUpdateStats () {
1364
1433
if (PDFJS .pdfBug && Stats .enabled ) {
1365
1434
var stats = this .stats ;
@@ -1706,6 +1775,10 @@ window.addEventListener('load', function webViewerLoad(evt) {
1706
1775
document .querySelector ('#viewSearch' ).classList .remove ('hidden' );
1707
1776
}
1708
1777
1778
+ if (!PDFView .supportsPrinting ) {
1779
+ document .getElementById ('print' ).classList .add ('hidden' );
1780
+ }
1781
+
1709
1782
// Listen for warnings to trigger the fallback UI. Errors should be caught
1710
1783
// and call PDFView.error() so we don't need to listen for those.
1711
1784
PDFJS .LogManager .addLogger ({
@@ -1960,3 +2033,11 @@ window.addEventListener('keydown', function keydown(evt) {
1960
2033
evt .preventDefault ();
1961
2034
}
1962
2035
});
2036
+
2037
+ window .addEventListener ('beforeprint' , function beforePrint (evt ) {
2038
+ PDFView .beforePrint ();
2039
+ });
2040
+
2041
+ window .addEventListener ('afterprint' , function afterPrint (evt ) {
2042
+ PDFView .afterPrint ();
2043
+ });
0 commit comments