diff --git a/README.md b/README.md index ea3dfd92d89a40099c36f7812b981cc91e3ba841..945fed1a5f19f51f95700301e4477d8b0d0188a7 100644 --- a/README.md +++ b/README.md @@ -17,22 +17,23 @@ pagedjs-cli ./path/to/index.html -o result.pdf ## Options ``` --h, --help output usage information --V, --version output the version number --i, --inputs [inputs] Inputs --o, --output [output] Output --d, --debug Show Electron Window to Debug --l, --landscape Landscape printing --s, --page-size [size] Print to Page Size [size] --w, --width [size] Print to Page Width [width] --h --height [size] Print to Page Height [weight] --m, --page-margin [margin] Print with margin [margin] --n, --hyphenate [lang] Hyphenate with language [language], defaults to "en-us" --hi, --hypher_ignore [str] Ignore passed element selectors, such as ".class_to_ignore, h1" --ho, --hypher_only [str] Only hyphenate passed elements selector, such as ".hyphenate, aside" --e, --encoding [type] Set the encoding of the input html, defaults to "utf-8" --t, --timeout [ms] Set a max timeout of [ms] ---outline-tags [tags] Specifies that an outline should be generated for the resulting PDF document. [tags] specifies which HTML tags should be considered for that outline. "h1,h2" will trigger an outline with "h1" tags as root elements and "h2" elements as their childs. +-h, --help output usage information +-V, --version output the version number +-i, --inputs [inputs] Inputs +-o, --output [output] Output +-d, --debug Show Electron Window to Debug +-l, --landscape Landscape printing +-s, --page-size [size] Print to Page Size [size] +-w, --width [size] Print to Page Width [width] +-h --height [size] Print to Page Height [weight] +-m, --page-margin [margin] Print with margin [margin] +-n, --hyphenate [lang] Hyphenate with language [language], defaults to "en-us" +-hi, --hypher_ignore [str] Ignore passed element selectors, such as ".class_to_ignore, h1" +-ho, --hypher_only [str] Only hyphenate passed elements selector, such as ".hyphenate, aside" +-e, --encoding [type] Set the encoding of the input html, defaults to "utf-8" +-t, --timeout [ms] Set a max timeout of [ms] +--outline-tags [tags] Specifies that an outline should be generated for the resulting PDF document. [tags] specifies which HTML tags should be considered for that outline. "h1,h2" will trigger an outline with "h1" tags as root elements and "h2" elements as their childs. +--additional-script <script> Additional script tags which are added to the HTML document before rendering. This is useful for adding custom pagedjs handlers. The option can be repeated. (default: []) ``` ## Hyphenation diff --git a/bin/paged b/bin/paged index a176a5e5767e70accc1ecdbec49863735cb42414..e2fb8aab95e59ecf41517067f91fa4593421efa8 100755 --- a/bin/paged +++ b/bin/paged @@ -31,8 +31,16 @@ program "HTML tags should be considered for that outline. " + "\"h1,h2\" will trigger an outline with \"h1\" tags as root elements " + "and \"h2\" elements as their childs.") + .option("--additional-script <script>", "Additional script tags which are " + + "added to the HTML document before rendering. This is useful for " + + "adding custom pagedjs handlers. The option can be repeated.", + collect, []) .parse(process.argv); +function collect(value, previous) { + return previous.concat(value); +} + let input = program.inputs || program.args[0]; @@ -96,7 +104,7 @@ if (typeof input === "string") { } (async () => { - let printer = new Printer(headless, allowLocal); + let printer = new Printer(headless, allowLocal, program.additionalScript); printer.on("page", (page) => { if (page.position === 0) { diff --git a/src/printer.js b/src/printer.js index 0dfb89a3934343e585706852a2c217f0f4a8ae47..39be3a12eecea130be03a1b007bfc263d7b9cef5 100644 --- a/src/printer.js +++ b/src/printer.js @@ -14,11 +14,12 @@ let scriptPath = paths[0] + "node_modules" + paths[paths.length-1]; const PostProcesser = require("./postprocesser"); class Printer extends EventEmitter { - constructor(headless, allowLocal) { + constructor(headless, allowLocal, additionalScripts) { super(); this.headless = headless !== false; this.allowLocal = allowLocal; this.pages = []; + this.additionalScripts = additionalScripts; } async setup() { @@ -98,6 +99,12 @@ class Printer extends EventEmitter { path: scriptPath }); + for (const script of this.additionalScripts) { + await page.addScriptTag({ + path: script + }); + } + // await page.exposeFunction("PuppeteerLogger", (msg) => { // console.log(msg); // });