diff --git a/bin/paged b/bin/paged index 153add217773d7ca2a8f53bbc16062ee61a36aef..cd81407f4037de78480e3e28ac6e447271b460a7 100755 --- a/bin/paged +++ b/bin/paged @@ -18,6 +18,7 @@ program .option("-s, --page-size [size]", "Print to Page Size [size]") .option("-w, --width [size]", "Print to Page Width [width] in MM") .option("-h --height [size]", "Print to Page Height [weight] in MM") + .option("--forceTransparentBackground", "Print with transparent background") // .option("-m, --page-margin [margin]", "Print with margin [margin]") // .option("-n, --hyphenate [lang]", "Hyphenate with language [language], defaults to "en-us"") // .option("-hi, --hypher_ignore [str]", "Ignore passed element selectors, such as ".class_to_ignore, h1"") @@ -107,14 +108,20 @@ if (typeof input === "string") { } (async () => { - let printer = new Printer({ + const printerOptions = { headless: headless, allowLocal: allowLocal, allowRemote: !program.blockRemote, allowedPaths: program.allowedPaths, allowedDomains: program.allowedDomains, additionalScripts: program.additionalScript, - }); + }; + + if (program.forceTransparentBackground) { + printerOptions.overrideDefaultBackgroundColor = { r: 0, g: 0, b: 0, a: 0 }; // Workaround to get a transparent background in the resulting PDF. See https://bugs.chromium.org/p/chromium/issues/detail?id=498892 for more information. + } + + let printer = new Printer(printerOptions); printer.on("page", (page) => { if (page.position === 0) { diff --git a/src/printer.js b/src/printer.js index c23adbafc3e0a265fb9c3de55c22d3f40d67535f..0036ff1d4e7b44d1c24f1d437aeb82fdb3ca77e8 100644 --- a/src/printer.js +++ b/src/printer.js @@ -25,6 +25,7 @@ class Printer extends EventEmitter { this.allowedDomains = options.allowedDomains || []; this.ignoreHTTPSErrors = options.ignoreHTTPSErrors; this.browserWSEndpoint = options.browserEndpoint; + this.overrideDefaultBackgroundColor = options.overrideDefaultBackgroundColor; this.pages = []; } @@ -34,7 +35,7 @@ class Printer extends EventEmitter { headless: this.headless, args: ["--disable-dev-shm-usage"], ignoreHTTPSErrors: this.ignoreHTTPSErrors - } + }; if (this.allowLocal) { puppeteerOptions.args.push("--allow-file-access-from-files"); @@ -63,6 +64,10 @@ class Printer extends EventEmitter { const page = await this.browser.newPage(); + if (this.overrideDefaultBackgroundColor) { + page._client.send('Emulation.setDefaultBackgroundColorOverride', { color: this.overrideDefaultBackgroundColor }); + } + let uri, url, relativePath, html; if (typeof input === "string") { try {