Skip to content
Snippets Groups Projects
Commit 8d051b1a authored by Erik Schilling's avatar Erik Schilling
Browse files

Added argument for adding additional scripts to a document

This allows to render documents with special handlers without modifying
the HTML before generation.
parent ad88253a
No related branches found
No related tags found
1 merge request!7Added argument for adding additional scripts to a document
...@@ -17,22 +17,23 @@ pagedjs-cli ./path/to/index.html -o result.pdf ...@@ -17,22 +17,23 @@ pagedjs-cli ./path/to/index.html -o result.pdf
## Options ## Options
``` ```
-h, --help output usage information -h, --help output usage information
-V, --version output the version number -V, --version output the version number
-i, --inputs [inputs] Inputs -i, --inputs [inputs] Inputs
-o, --output [output] Output -o, --output [output] Output
-d, --debug Show Electron Window to Debug -d, --debug Show Electron Window to Debug
-l, --landscape Landscape printing -l, --landscape Landscape printing
-s, --page-size [size] Print to Page Size [size] -s, --page-size [size] Print to Page Size [size]
-w, --width [size] Print to Page Width [width] -w, --width [size] Print to Page Width [width]
-h --height [size] Print to Page Height [weight] -h --height [size] Print to Page Height [weight]
-m, --page-margin [margin] Print with margin [margin] -m, --page-margin [margin] Print with margin [margin]
-n, --hyphenate [lang] Hyphenate with language [language], defaults to "en-us" -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" -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" -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" -e, --encoding [type] Set the encoding of the input html, defaults to "utf-8"
-t, --timeout [ms] Set a max timeout of [ms] -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. --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 ## Hyphenation
......
...@@ -31,8 +31,16 @@ program ...@@ -31,8 +31,16 @@ program
"HTML tags should be considered for that outline. " + "HTML tags should be considered for that outline. " +
"\"h1,h2\" will trigger an outline with \"h1\" tags as root elements " + "\"h1,h2\" will trigger an outline with \"h1\" tags as root elements " +
"and \"h2\" elements as their childs.") "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); .parse(process.argv);
function collect(value, previous) {
return previous.concat(value);
}
let input = program.inputs || program.args[0]; let input = program.inputs || program.args[0];
...@@ -96,7 +104,7 @@ if (typeof input === "string") { ...@@ -96,7 +104,7 @@ if (typeof input === "string") {
} }
(async () => { (async () => {
let printer = new Printer(headless, allowLocal); let printer = new Printer(headless, allowLocal, program.additionalScript);
printer.on("page", (page) => { printer.on("page", (page) => {
if (page.position === 0) { if (page.position === 0) {
......
...@@ -14,11 +14,12 @@ let scriptPath = paths[0] + "node_modules" + paths[paths.length-1]; ...@@ -14,11 +14,12 @@ let scriptPath = paths[0] + "node_modules" + paths[paths.length-1];
const PostProcesser = require("./postprocesser"); const PostProcesser = require("./postprocesser");
class Printer extends EventEmitter { class Printer extends EventEmitter {
constructor(headless, allowLocal) { constructor(headless, allowLocal, additionalScripts) {
super(); super();
this.headless = headless !== false; this.headless = headless !== false;
this.allowLocal = allowLocal; this.allowLocal = allowLocal;
this.pages = []; this.pages = [];
this.additionalScripts = additionalScripts;
} }
async setup() { async setup() {
...@@ -98,6 +99,12 @@ class Printer extends EventEmitter { ...@@ -98,6 +99,12 @@ class Printer extends EventEmitter {
path: scriptPath path: scriptPath
}); });
for (const script of this.additionalScripts) {
await page.addScriptTag({
path: script
});
}
// await page.exposeFunction("PuppeteerLogger", (msg) => { // await page.exposeFunction("PuppeteerLogger", (msg) => {
// console.log(msg); // console.log(msg);
// }); // });
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment