Commit 689977b5 authored by lukas.niegsch's avatar lukas.niegsch

fixed printing and added default options

parent ce1ae939
......@@ -105,10 +105,10 @@ class HeadlessChromium
return await this.runPageCommand(page, "Page.enable")
}
async printToPdf(page)
async printToPdf(page, options)
{
// wip: printing fails for some reason
var response = await this.runPageCommand(page, "Page.printToPDF")
return response
var response = await this.runPageCommand(page, "Page.printToPDF", options)
return response.data //atob(response.data)
}
}
......@@ -2,7 +2,7 @@ class RequestHandler
{
constructor()
{
this.buffer = ["https://www.example.com"]
this.buffer = ["https://www.example.com", "https://www.nexedi.com/"]
}
hasNextRequest()
......@@ -15,8 +15,11 @@ class RequestHandler
return this.buffer.pop()
}
publishResults(data)
publishResults(name, data)
{
console.log("result: \n" + data)
var a = document.createElement("a");
a.href = "data:application/pdf;base64," + data;
a.download = `${name}.pdf`;
a.click();
}
}
\ No newline at end of file
......@@ -7,6 +7,33 @@
<script src="connection.js"></script>
<script src="chromium.js"></script>
<script>
function mm2inches (mm)
{
return mm / 25.4
}
/*
Options for printing:
https://chromedevtools.github.io/devtools-protocol/1-3/Page/#method-printToPDF
*/
var optionsForPrintToPdf =
{
landscape: true,
displayHeaderFooter: false,
printBackground: true,
scale: 1,
paperWidth: mm2inches(297 /* millimetre */),
paperHeight: mm2inches(210 /* millimetre */),
marginTop: mm2inches(10 /* millimetre */),
marginBottom: mm2inches(10 /* millimetre */),
marginLeft: mm2inches(10 /* millimetre */),
marginRight: mm2inches(10 /* millimetre */),
pageRanges: "",
headerTemplate: "",
footerTemplate: "",
preferCSSPageSize: false
}
/*
Usage: Start google chromium with the following command line flags.
--headless
......@@ -24,7 +51,7 @@
{
var page = await browser.openNewPage(url)
await browser.enablePage(page)
var pdf = browser.printToPdf(page)
var pdf = await browser.printToPdf(page, optionsForPrintToPdf)
await browser.closePage(page)
return pdf
}
......@@ -39,7 +66,8 @@
var url = handler.getNextRequest()
var pdf = await html2pdf(url)
handler.publishResults(pdf)
var name = (new URL(url)).hostname
handler.publishResults(name, pdf)
setTimeout(mainloop, 3 /* seconds */ * 1000)
}
mainloop()
......
Markdown is supported
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