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

fixed printing and added default options

parent ce1ae939
...@@ -105,10 +105,10 @@ class HeadlessChromium ...@@ -105,10 +105,10 @@ class HeadlessChromium
return await this.runPageCommand(page, "Page.enable") return await this.runPageCommand(page, "Page.enable")
} }
async printToPdf(page) async printToPdf(page, options)
{ {
// wip: printing fails for some reason // wip: printing fails for some reason
var response = await this.runPageCommand(page, "Page.printToPDF") var response = await this.runPageCommand(page, "Page.printToPDF", options)
return response return response.data //atob(response.data)
} }
} }
...@@ -2,7 +2,7 @@ class RequestHandler ...@@ -2,7 +2,7 @@ class RequestHandler
{ {
constructor() constructor()
{ {
this.buffer = ["https://www.example.com"] this.buffer = ["https://www.example.com", "https://www.nexedi.com/"]
} }
hasNextRequest() hasNextRequest()
...@@ -15,8 +15,11 @@ class RequestHandler ...@@ -15,8 +15,11 @@ class RequestHandler
return this.buffer.pop() 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 @@ ...@@ -7,6 +7,33 @@
<script src="connection.js"></script> <script src="connection.js"></script>
<script src="chromium.js"></script> <script src="chromium.js"></script>
<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. Usage: Start google chromium with the following command line flags.
--headless --headless
...@@ -24,7 +51,7 @@ ...@@ -24,7 +51,7 @@
{ {
var page = await browser.openNewPage(url) var page = await browser.openNewPage(url)
await browser.enablePage(page) await browser.enablePage(page)
var pdf = browser.printToPdf(page) var pdf = await browser.printToPdf(page, optionsForPrintToPdf)
await browser.closePage(page) await browser.closePage(page)
return pdf return pdf
} }
...@@ -39,7 +66,8 @@ ...@@ -39,7 +66,8 @@
var url = handler.getNextRequest() var url = handler.getNextRequest()
var pdf = await html2pdf(url) var pdf = await html2pdf(url)
handler.publishResults(pdf) var name = (new URL(url)).hostname
handler.publishResults(name, pdf)
setTimeout(mainloop, 3 /* seconds */ * 1000) setTimeout(mainloop, 3 /* seconds */ * 1000)
} }
mainloop() 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