[Firefox] Add CSS at-page size when printing from FirefoxPrintService (bug 1820651)

- Duplicates at-page size method from PDFPrintService
- Updates getPagesOverview to rotate pages to fit the initial orientation
This commit is contained in:
Fred Chasen 2023-03-31 15:20:38 -07:00
parent 2358757b66
commit 3c326974a0
3 changed files with 44 additions and 12 deletions

View File

@ -146,6 +146,27 @@ FirefoxPrintService.prototype = {
const body = document.querySelector("body"); const body = document.querySelector("body");
body.setAttribute("data-pdfjsprinting", true); body.setAttribute("data-pdfjsprinting", true);
const hasEqualPageSizes = this.pagesOverview.every(function (size) {
return (
size.width === this.pagesOverview[0].width &&
size.height === this.pagesOverview[0].height
);
}, this);
if (!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 scaled down to the initial page size in Firefox.
this.pageStyleSheet = document.createElement("style");
const pageSize = this.pagesOverview[0];
this.pageStyleSheet.textContent = `@page { size: ${pageSize.width}pt ${pageSize.height}pt;}`;
body.append(this.pageStyleSheet);
if (pdfDocument.isPureXfa) { if (pdfDocument.isPureXfa) {
getXfaHtmlForPrinting(printContainer, pdfDocument); getXfaHtmlForPrinting(printContainer, pdfDocument);
return; return;
@ -169,6 +190,11 @@ FirefoxPrintService.prototype = {
const body = document.querySelector("body"); const body = document.querySelector("body");
body.removeAttribute("data-pdfjsprinting"); body.removeAttribute("data-pdfjsprinting");
if (this.pageStyleSheet) {
this.pageStyleSheet.remove();
this.pageStyleSheet = null;
}
}, },
}; };

View File

@ -111,9 +111,9 @@ PDFPrintService.prototype = {
// TODO(robwu): Use named pages when size calculation bugs get resolved // TODO(robwu): Use named pages when size calculation bugs get resolved
// (e.g. https://crbug.com/355116) AND when support for named pages is // (e.g. https://crbug.com/355116) AND when support for named pages is
// added (http://www.w3.org/TR/css3-page/#using-named-pages). // added (http://www.w3.org/TR/css3-page/#using-named-pages).
// In browsers where @page + size is not supported (such as Firefox, // In browsers where @page + size is not supported, the next stylesheet
// https://bugzil.la/851441), the next stylesheet will be ignored and the // will be ignored and the user has to select the correct paper size in
// user has to select the correct paper size in the UI if wanted. // the UI if wanted.
this.pageStyleSheet = document.createElement("style"); this.pageStyleSheet = document.createElement("style");
const pageSize = this.pagesOverview[0]; const pageSize = this.pagesOverview[0];
this.pageStyleSheet.textContent = this.pageStyleSheet.textContent =

View File

@ -1673,22 +1673,28 @@ class PDFViewer {
* @returns {Array} Array of objects with width/height/rotation fields. * @returns {Array} Array of objects with width/height/rotation fields.
*/ */
getPagesOverview() { getPagesOverview() {
let initialOrientation;
return this._pages.map(pageView => { return this._pages.map(pageView => {
const viewport = pageView.pdfPage.getViewport({ scale: 1 }); const viewport = pageView.pdfPage.getViewport({ scale: 1 });
const orientation = isPortraitOrientation(viewport);
if (!this.enablePrintAutoRotate || isPortraitOrientation(viewport)) { if (initialOrientation === undefined) {
return { initialOrientation = orientation;
width: viewport.width, } else if (
height: viewport.height, this.enablePrintAutoRotate &&
rotation: viewport.rotation, orientation !== initialOrientation
}; ) {
} // Rotate to fit the initial orientation.
// Landscape orientation.
return { return {
width: viewport.height, width: viewport.height,
height: viewport.width, height: viewport.width,
rotation: (viewport.rotation - 90) % 360, rotation: (viewport.rotation - 90) % 360,
}; };
}
return {
width: viewport.width,
height: viewport.height,
rotation: viewport.rotation,
};
}); });
} }