@@ -379,6 +379,11 @@ var PDFView = {
379
379
return currentPageNumber ;
380
380
},
381
381
382
+ get supportsPrinting () {
383
+ var canvas = document .createElement ('canvas' );
384
+ return 'mozPrintCallback' in canvas ;
385
+ },
386
+
382
387
open : function pdfViewOpen (url , scale , password ) {
383
388
var parameters = {password : password };
384
389
if (typeof url === 'string' ) { // URL
@@ -1041,6 +1046,22 @@ var PDFView = {
1041
1046
params [unescape (key )] = unescape (value );
1042
1047
}
1043
1048
return params ;
1049
+ },
1050
+
1051
+ beforePrint : function pdfViewSetupBeforePrint () {
1052
+ if (!this .supportsPrinting ) {
1053
+ alert ('Printing is not supported by this browser.' );
1054
+ return ;
1055
+ }
1056
+ for (var i = 0 , ii = this .pages .length ; i < ii ; ++i ) {
1057
+ this .pages [i ].beforePrint ();
1058
+ }
1059
+ },
1060
+
1061
+ afterPrint : function pdfViewSetupAfterPrint () {
1062
+ var div = document .getElementById ('printContainer' );
1063
+ while (div .hasChildNodes ())
1064
+ div .removeChild (div .lastChild );
1044
1065
}
1045
1066
};
1046
1067
@@ -1360,6 +1381,40 @@ var PageView = function pageView(container, pdfPage, id, scale,
1360
1381
div .setAttribute ('data-loaded' , true );
1361
1382
};
1362
1383
1384
+ this .beforePrint = function pageViewBeforePrint () {
1385
+ var pdfPage = this .pdfPage ;
1386
+ var viewport = pdfPage .getViewport (1 );
1387
+
1388
+ var canvas = this .canvas = document .createElement ('canvas' );
1389
+ canvas .width = viewport .width ;
1390
+ canvas .height = viewport .height ;
1391
+ canvas .style .width = viewport .width + 'pt' ;
1392
+ canvas .style .height = viewport .height + 'pt' ;
1393
+
1394
+ var printContainer = document .getElementById ('printContainer' );
1395
+ printContainer .appendChild (canvas );
1396
+
1397
+ var self = this ;
1398
+ canvas .mozPrintCallback = function (obj ) {
1399
+ var ctx = obj .context ;
1400
+ var renderContext = {
1401
+ canvasContext : ctx ,
1402
+ viewport : viewport
1403
+ };
1404
+
1405
+ pdfPage .render (renderContext ).then (function () {
1406
+ // Tell the printEngine that rendering this canvas/page has finished.
1407
+ obj .done ();
1408
+ self .pdfPage .destroy ();
1409
+ }, function (error ) {
1410
+ console .error (error );
1411
+ // Tell the printEngine that rendering this canvas/page has failed.
1412
+ // This will make the print proces stop.
1413
+ obj .abort ();
1414
+ });
1415
+ };
1416
+ };
1417
+
1363
1418
this .updateStats = function pageViewUpdateStats () {
1364
1419
if (PDFJS .pdfBug && Stats .enabled ) {
1365
1420
var stats = this .stats ;
@@ -1960,3 +2015,11 @@ window.addEventListener('keydown', function keydown(evt) {
1960
2015
evt .preventDefault ();
1961
2016
}
1962
2017
});
2018
+
2019
+ window .addEventListener ('beforeprint' , function beforePrint (evt ) {
2020
+ PDFView .beforePrint ();
2021
+ });
2022
+
2023
+ window .addEventListener ('afterprint' , function afterPrint (evt ) {
2024
+ PDFView .afterPrint ();
2025
+ });
0 commit comments