[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:
parent
2358757b66
commit
3c326974a0
@ -146,6 +146,27 @@ FirefoxPrintService.prototype = {
|
||||
const body = document.querySelector("body");
|
||||
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) {
|
||||
getXfaHtmlForPrinting(printContainer, pdfDocument);
|
||||
return;
|
||||
@ -169,6 +190,11 @@ FirefoxPrintService.prototype = {
|
||||
|
||||
const body = document.querySelector("body");
|
||||
body.removeAttribute("data-pdfjsprinting");
|
||||
|
||||
if (this.pageStyleSheet) {
|
||||
this.pageStyleSheet.remove();
|
||||
this.pageStyleSheet = null;
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
|
@ -111,9 +111,9 @@ PDFPrintService.prototype = {
|
||||
// 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.
|
||||
// In browsers where @page + size is not supported, 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");
|
||||
const pageSize = this.pagesOverview[0];
|
||||
this.pageStyleSheet.textContent =
|
||||
|
@ -1673,21 +1673,27 @@ class PDFViewer {
|
||||
* @returns {Array} Array of objects with width/height/rotation fields.
|
||||
*/
|
||||
getPagesOverview() {
|
||||
let initialOrientation;
|
||||
return this._pages.map(pageView => {
|
||||
const viewport = pageView.pdfPage.getViewport({ scale: 1 });
|
||||
|
||||
if (!this.enablePrintAutoRotate || isPortraitOrientation(viewport)) {
|
||||
const orientation = isPortraitOrientation(viewport);
|
||||
if (initialOrientation === undefined) {
|
||||
initialOrientation = orientation;
|
||||
} else if (
|
||||
this.enablePrintAutoRotate &&
|
||||
orientation !== initialOrientation
|
||||
) {
|
||||
// Rotate to fit the initial orientation.
|
||||
return {
|
||||
width: viewport.width,
|
||||
height: viewport.height,
|
||||
rotation: viewport.rotation,
|
||||
width: viewport.height,
|
||||
height: viewport.width,
|
||||
rotation: (viewport.rotation - 90) % 360,
|
||||
};
|
||||
}
|
||||
// Landscape orientation.
|
||||
return {
|
||||
width: viewport.height,
|
||||
height: viewport.width,
|
||||
rotation: (viewport.rotation - 90) % 360,
|
||||
width: viewport.width,
|
||||
height: viewport.height,
|
||||
rotation: viewport.rotation,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user