2020-05-25 22:02:52 +03:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2020-09-08 21:19:32 +03:00
|
|
|
const EXPORTED_SYMBOLS = ["print"];
|
|
|
|
|
2020-05-25 22:02:52 +03:00
|
|
|
const { XPCOMUtils } = ChromeUtils.import(
|
|
|
|
"resource://gre/modules/XPCOMUtils.jsm"
|
|
|
|
);
|
|
|
|
|
2020-09-08 21:22:43 +03:00
|
|
|
XPCOMUtils.defineLazyModuleGetters(this, {
|
|
|
|
clearInterval: "resource://gre/modules/Timer.jsm",
|
|
|
|
setInterval: "resource://gre/modules/Timer.jsm",
|
|
|
|
|
2021-07-08 11:12:53 +03:00
|
|
|
Log: "chrome://remote/content/shared/Log.jsm",
|
2020-09-08 21:22:43 +03:00
|
|
|
});
|
2020-05-25 22:02:52 +03:00
|
|
|
|
2021-07-08 11:12:53 +03:00
|
|
|
XPCOMUtils.defineLazyGetter(this, "logger", () =>
|
|
|
|
Log.get(Log.TYPES.MARIONETTE)
|
|
|
|
);
|
2020-05-25 22:02:52 +03:00
|
|
|
|
|
|
|
this.print = {
|
|
|
|
maxScaleValue: 2.0,
|
|
|
|
minScaleValue: 0.1,
|
|
|
|
letterPaperSizeCm: {
|
|
|
|
width: 21.59,
|
|
|
|
height: 27.94,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
print.addDefaultSettings = function(settings) {
|
|
|
|
const {
|
|
|
|
landscape = false,
|
|
|
|
margin = {
|
|
|
|
top: 1,
|
|
|
|
bottom: 1,
|
|
|
|
left: 1,
|
|
|
|
right: 1,
|
|
|
|
},
|
|
|
|
page = print.letterPaperSizeCm,
|
|
|
|
shrinkToFit = true,
|
|
|
|
printBackground = false,
|
|
|
|
scale = 1.0,
|
|
|
|
} = settings;
|
|
|
|
return { landscape, margin, page, shrinkToFit, printBackground, scale };
|
|
|
|
};
|
|
|
|
|
|
|
|
function getPrintSettings(settings, filePath) {
|
|
|
|
const psService = Cc["@mozilla.org/gfx/printsettings-service;1"].getService(
|
|
|
|
Ci.nsIPrintSettingsService
|
|
|
|
);
|
|
|
|
|
|
|
|
let cmToInches = cm => cm / 2.54;
|
|
|
|
const printSettings = psService.newPrintSettings;
|
|
|
|
printSettings.isInitializedFromPrinter = true;
|
|
|
|
printSettings.isInitializedFromPrefs = true;
|
|
|
|
printSettings.outputFormat = Ci.nsIPrintSettings.kOutputFormatPDF;
|
|
|
|
printSettings.printerName = "marionette";
|
|
|
|
printSettings.printSilent = true;
|
|
|
|
printSettings.printToFile = true;
|
|
|
|
printSettings.showPrintProgress = false;
|
|
|
|
printSettings.toFileName = filePath;
|
|
|
|
|
|
|
|
// Setting the paperSizeUnit to kPaperSizeMillimeters doesn't work on mac
|
|
|
|
printSettings.paperSizeUnit = Ci.nsIPrintSettings.kPaperSizeInches;
|
|
|
|
printSettings.paperWidth = cmToInches(settings.page.width);
|
|
|
|
printSettings.paperHeight = cmToInches(settings.page.height);
|
|
|
|
|
|
|
|
printSettings.marginBottom = cmToInches(settings.margin.bottom);
|
|
|
|
printSettings.marginLeft = cmToInches(settings.margin.left);
|
|
|
|
printSettings.marginRight = cmToInches(settings.margin.right);
|
|
|
|
printSettings.marginTop = cmToInches(settings.margin.top);
|
|
|
|
|
|
|
|
printSettings.printBGColors = settings.printBackground;
|
|
|
|
printSettings.printBGImages = settings.printBackground;
|
|
|
|
printSettings.scaling = settings.scale;
|
|
|
|
printSettings.shrinkToFit = settings.shrinkToFit;
|
|
|
|
|
|
|
|
printSettings.headerStrCenter = "";
|
|
|
|
printSettings.headerStrLeft = "";
|
|
|
|
printSettings.headerStrRight = "";
|
|
|
|
printSettings.footerStrCenter = "";
|
|
|
|
printSettings.footerStrLeft = "";
|
|
|
|
printSettings.footerStrRight = "";
|
|
|
|
|
|
|
|
// Override any os-specific unwriteable margins
|
|
|
|
printSettings.unwriteableMarginTop = 0;
|
|
|
|
printSettings.unwriteableMarginLeft = 0;
|
|
|
|
printSettings.unwriteableMarginBottom = 0;
|
|
|
|
printSettings.unwriteableMarginRight = 0;
|
|
|
|
|
|
|
|
if (settings.landscape) {
|
|
|
|
printSettings.orientation = Ci.nsIPrintSettings.kLandscapeOrientation;
|
|
|
|
}
|
|
|
|
return printSettings;
|
|
|
|
}
|
|
|
|
|
2021-04-17 21:41:11 +03:00
|
|
|
print.printToFile = async function(browser, settings) {
|
2020-05-25 22:02:52 +03:00
|
|
|
// Create a unique filename for the temporary PDF file
|
2021-08-02 12:23:44 +03:00
|
|
|
const tempDir = await PathUtils.getTempDir();
|
|
|
|
const basePath = PathUtils.join(tempDir, "marionette.pdf");
|
|
|
|
const filePath = await PathUtils.createUniquePath(basePath);
|
2020-05-25 22:02:52 +03:00
|
|
|
|
|
|
|
let printSettings = getPrintSettings(settings, filePath);
|
|
|
|
|
2021-04-17 21:41:11 +03:00
|
|
|
await browser.browsingContext.print(printSettings);
|
2020-07-23 02:17:45 +03:00
|
|
|
|
|
|
|
// Bug 1603739 - With e10s enabled the promise returned by print() resolves
|
|
|
|
// too early, which means the file hasn't been completely written.
|
2020-05-25 22:02:52 +03:00
|
|
|
await new Promise(resolve => {
|
2020-07-23 02:17:45 +03:00
|
|
|
const DELAY_CHECK_FILE_COMPLETELY_WRITTEN = 100;
|
|
|
|
|
|
|
|
let lastSize = 0;
|
|
|
|
const timerId = setInterval(async () => {
|
2021-08-02 12:23:44 +03:00
|
|
|
const fileInfo = await IOUtils.stat(filePath);
|
2020-07-23 02:17:45 +03:00
|
|
|
if (lastSize > 0 && fileInfo.size == lastSize) {
|
|
|
|
clearInterval(timerId);
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
lastSize = fileInfo.size;
|
|
|
|
}, DELAY_CHECK_FILE_COMPLETELY_WRITTEN);
|
2020-05-25 22:02:52 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
logger.debug(`PDF output written to ${filePath}`);
|
|
|
|
return filePath;
|
|
|
|
};
|