-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathpdf.js
45 lines (37 loc) · 1017 Bytes
/
pdf.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Import Node.js Dependencies
import path from "node:path";
// Import Third-party Dependencies
import puppeteer from "puppeteer";
// Import Internal Dependencies
import * as CONSTANTS from "../constants.js";
import * as utils from "../utils/index.js";
export async function PDF(
reportHTMLPath,
options
) {
const { title, saveOnDisk = true } = options;
const browser = await puppeteer.launch({
args: ["--no-sandbox", "--disable-setuid-sandbox"]
});
const page = await browser.newPage();
try {
await page.emulateMediaType("print");
await page.goto(`file:${reportHTMLPath}`, {
waitUntil: "networkidle0",
timeout: 20_000
});
const pdfBuffer = await page.pdf({
path: saveOnDisk ? path.join(
CONSTANTS.DIRS.REPORTS,
utils.cleanReportName(title, ".pdf")
) : undefined,
format: "A4",
printBackground: true
});
return saveOnDisk ? path : pdfBuffer;
}
finally {
await page.close();
await browser.close();
}
}