Merge pull request #5857 from Rob--W/print-page-size

Set page size via @page + size
This commit is contained in:
Tim van der Meij 2015-05-14 16:52:40 +02:00
commit d7aa95d6b1
3 changed files with 59 additions and 3 deletions

View File

@ -495,10 +495,15 @@ var PDFPageView = (function PDFPageViewClosure() {
// better output until bug 811002 is fixed in FF. // better output until bug 811002 is fixed in FF.
var PRINT_OUTPUT_SCALE = 2; var PRINT_OUTPUT_SCALE = 2;
var canvas = document.createElement('canvas'); var canvas = document.createElement('canvas');
// The logical size of the canvas.
canvas.width = Math.floor(viewport.width) * PRINT_OUTPUT_SCALE; canvas.width = Math.floor(viewport.width) * PRINT_OUTPUT_SCALE;
canvas.height = Math.floor(viewport.height) * PRINT_OUTPUT_SCALE; canvas.height = Math.floor(viewport.height) * PRINT_OUTPUT_SCALE;
canvas.style.width = (PRINT_OUTPUT_SCALE * viewport.width) + 'pt';
canvas.style.height = (PRINT_OUTPUT_SCALE * viewport.height) + 'pt'; // The rendered size of the canvas, relative to the size of canvasWrapper.
canvas.style.width = (PRINT_OUTPUT_SCALE * 100) + '%';
canvas.style.height = (PRINT_OUTPUT_SCALE * 100) + '%';
var cssScale = 'scale(' + (1 / PRINT_OUTPUT_SCALE) + ', ' + var cssScale = 'scale(' + (1 / PRINT_OUTPUT_SCALE) + ', ' +
(1 / PRINT_OUTPUT_SCALE) + ')'; (1 / PRINT_OUTPUT_SCALE) + ')';
CustomStyle.setProp('transform' , canvas, cssScale); CustomStyle.setProp('transform' , canvas, cssScale);

View File

@ -1715,10 +1715,14 @@ html[dir='rtl'] #documentPropertiesOverlay .row > * {
body[data-mozPrintCallback] #printContainer { body[data-mozPrintCallback] #printContainer {
display: block; display: block;
} }
#printContainer canvas { /* wrapper around (scaled) print canvas elements */
#printContainer > div {
position: relative; position: relative;
top: 0; top: 0;
left: 0; left: 0;
overflow: hidden;
}
#printContainer canvas {
display: block; display: block;
} }
} }

View File

@ -1314,6 +1314,35 @@ var PDFViewerApplication = {
var body = document.querySelector('body'); var body = document.querySelector('body');
body.setAttribute('data-mozPrintCallback', true); body.setAttribute('data-mozPrintCallback', true);
if (!this.hasEqualPageSizes) {
console.warn('Not all pages have the same size. The printed result ' +
'may be incorrect!');
}
// Insert a @page + size rule to make sure that the page size is correctly
// set. Note that we assume that all pages have the same size, because
// variable-size pages are not supported yet (at least in Chrome & Firefox).
// TODO(robwu): Use named pages when size calculation bugs get resolved
// (e.g. https://crbug.com/355116) AND when support for named pages is
// added (http://www.w3.org/TR/css3-page/#using-named-pages).
// In browsers where @page + size is not supported (such as Firefox,
// https://bugzil.la/851441), the next stylesheet will be ignored and the
// user has to select the correct paper size in the UI if wanted.
this.pageStyleSheet = document.createElement('style');
var pageSize = this.pdfViewer.getPageView(0).pdfPage.getViewport(1);
this.pageStyleSheet.textContent =
// "size:<width> <height>" is what we need. But also add "A4" because
// Firefox incorrectly reports support for the other value.
'@supports ((size:A4) and (size:1pt 1pt)) {' +
'@page { size: ' + pageSize.width + 'pt ' + pageSize.height + 'pt;}' +
// The canvas and each ancestor node must have a height of 100% to make
// sure that each canvas is printed on exactly one page.
'#printContainer {height:100%}' +
'#printContainer > div {width:100% !important;height:100% !important;}' +
'}';
body.appendChild(this.pageStyleSheet);
for (i = 0, ii = this.pagesCount; i < ii; ++i) { for (i = 0, ii = this.pagesCount; i < ii; ++i) {
this.pdfViewer.getPageView(i).beforePrint(); this.pdfViewer.getPageView(i).beforePrint();
} }
@ -1330,12 +1359,30 @@ var PDFViewerApplication = {
//#endif //#endif
}, },
// Whether all pages of the PDF have the same width and height.
get hasEqualPageSizes() {
var firstPage = this.pdfViewer.getPageView(0);
for (var i = 1, ii = this.pagesCount; i < ii; ++i) {
var pageView = this.pdfViewer.getPageView(i);
if (pageView.width !== firstPage.width ||
pageView.height !== firstPage.height) {
return false;
}
}
return true;
},
afterPrint: function pdfViewSetupAfterPrint() { afterPrint: function pdfViewSetupAfterPrint() {
var div = document.getElementById('printContainer'); var div = document.getElementById('printContainer');
while (div.hasChildNodes()) { while (div.hasChildNodes()) {
div.removeChild(div.lastChild); div.removeChild(div.lastChild);
} }
if (this.pageStyleSheet && this.pageStyleSheet.parentNode) {
this.pageStyleSheet.parentNode.removeChild(this.pageStyleSheet);
this.pageStyleSheet = null;
}
this.printing = false; this.printing = false;
this.forceRendering(); this.forceRendering();
}, },