Skip to content

Commit c757eed

Browse files
committed
Merge pull request mozilla#1884 from brendandahl/mozPrintCallback
Moz print callback
2 parents 4d0b478 + e037853 commit c757eed

File tree

4 files changed

+106
-2
lines changed

4 files changed

+106
-2
lines changed

l10n/en-US/viewer.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,5 @@ loading_error=An error occurred while loading the PDF.
8787
# Some common types are e.g.: "Check", "Text", "Comment", "Note"
8888
text_annotation_type=[{{type}} Annotation]
8989
request_password=PDF is protected by a password:
90+
91+
printing_not_supported=Warning: Printing is not fully supported by this browser.

web/viewer.css

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,7 +1113,16 @@ canvas {
11131113
font-size: 10px;
11141114
}
11151115

1116+
@page {
1117+
margin: 0;
1118+
}
1119+
1120+
#printContainer {
1121+
display: none;
1122+
}
1123+
11161124
@media print {
1125+
/* Rules for browsers that don't support mozPrintCallback. */
11171126
#sidebarContainer, .toolbar, #loadingBox, #errorWrapper, .textLayer {
11181127
display: none;
11191128
}
@@ -1135,6 +1144,19 @@ canvas {
11351144
.page[data-loaded] {
11361145
display: block;
11371146
}
1147+
1148+
/* Rules for browsers that support mozPrintCallback */
1149+
body[data-mozPrintCallback] #outerContainer {
1150+
display: none;
1151+
}
1152+
body[data-mozPrintCallback] #printContainer {
1153+
display: block;
1154+
}
1155+
#printContainer canvas {
1156+
position: relative;
1157+
top: 0;
1158+
left: 0;
1159+
}
11381160
}
11391161

11401162
@media all and (max-width: 950px) {

web/viewer.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,9 @@
102102
<span data-l10n-id="open_file_label">Open</span>
103103
</button>
104104

105-
<!--
106105
<button id="print" class="toolbarButton print" title="Print" tabindex="11" data-l10n-id="print" onclick="window.print()">
107106
<span data-l10n-id="print_label">Print</span>
108107
</button>
109-
-->
110108

111109
<button id="download" class="toolbarButton download" title="Download" onclick="PDFView.download();" tabindex="12" data-l10n-id="download">
112110
<span data-l10n-id="download_label">Download</span>
@@ -176,5 +174,6 @@
176174
</div> <!-- mainContainer -->
177175

178176
</div> <!-- outerContainer -->
177+
<div id="printContainer"></div>
179178
</body>
180179
</html>

web/viewer.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,17 @@ var PDFView = {
379379
return currentPageNumber;
380380
},
381381

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+
382393
open: function pdfViewOpen(url, scale, password) {
383394
var parameters = {password: password};
384395
if (typeof url === 'string') { // URL
@@ -1041,6 +1052,26 @@ var PDFView = {
10411052
params[unescape(key)] = unescape(value);
10421053
}
10431054
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);
10441075
}
10451076
};
10461077

@@ -1360,6 +1391,44 @@ var PageView = function pageView(container, pdfPage, id, scale,
13601391
div.setAttribute('data-loaded', true);
13611392
};
13621393

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+
13631432
this.updateStats = function pageViewUpdateStats() {
13641433
if (PDFJS.pdfBug && Stats.enabled) {
13651434
var stats = this.stats;
@@ -1706,6 +1775,10 @@ window.addEventListener('load', function webViewerLoad(evt) {
17061775
document.querySelector('#viewSearch').classList.remove('hidden');
17071776
}
17081777

1778+
if (!PDFView.supportsPrinting) {
1779+
document.getElementById('print').classList.add('hidden');
1780+
}
1781+
17091782
// Listen for warnings to trigger the fallback UI. Errors should be caught
17101783
// and call PDFView.error() so we don't need to listen for those.
17111784
PDFJS.LogManager.addLogger({
@@ -1960,3 +2033,11 @@ window.addEventListener('keydown', function keydown(evt) {
19602033
evt.preventDefault();
19612034
}
19622035
});
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

Comments
 (0)