Skip to content

Commit 131d989

Browse files
committed
Refactor
1 parent 29cd3ae commit 131d989

File tree

1 file changed

+76
-75
lines changed

1 file changed

+76
-75
lines changed

index.js

Lines changed: 76 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,23 @@ const hb = require("handlebars");
44
const fs = require("fs");
55
const inlineCss = require("inline-css");
66
module.exports;
7+
78
async function generatePdf(file, options, callback, font) {
8-
// we are using headless mode
9-
let args = ["--no-sandbox", "--disable-setuid-sandbox"];
10-
if (options.args) {
11-
args = options.args;
12-
delete options.args;
13-
}
9+
// we are using headless mode
10+
let args = ["--no-sandbox", "--disable-setuid-sandbox"];
11+
if (options.args) {
12+
args = options.args;
13+
delete options.args;
14+
}
1415

15-
const browser = await puppeteer.launch({
16-
args: args,
17-
});
18-
const page = await browser.newPage();
19-
let embeddedFontStyle = "";
20-
if (file.content) {
21-
if (font) {
22-
embeddedFontStyle = `
16+
const browser = await puppeteer.launch({
17+
args: args,
18+
});
19+
const page = await browser.newPage();
20+
let embeddedFontStyle = "";
21+
if (file.content) {
22+
if (font) {
23+
embeddedFontStyle = `
2324
<style>
2425
@font-face {
2526
font-family: ${font.name};
@@ -29,76 +30,76 @@ async function generatePdf(file, options, callback, font) {
2930
}
3031
</style>
3132
`;
32-
}
33-
data = await inlineCss(file.content, { url: "/" });
34-
data = embeddedFontStyle + data;
33+
}
34+
data = await inlineCss(file.content, {url: "/"});
35+
data = embeddedFontStyle + data;
3536

36-
console.log("Compiling the template with handlebars");
37-
// we have compile our code with handlebars
38-
const template = hb.compile(data, { strict: true });
39-
const result = template(data);
40-
const html = result;
37+
console.log("Compiling the template with handlebars");
38+
// we have compile our code with handlebars
39+
const template = hb.compile(data, {strict: true});
40+
const result = template(data);
41+
const html = result;
4142

42-
// We set the page content as the generated html by handlebars
43-
await page.setContent(html, {
44-
waitUntil: "networkidle0", // wait for page to load completely
45-
});
46-
} else {
47-
await page.goto(file.url, {
48-
waitUntil: ["load", "networkidle0"], // wait for page to load completely
49-
});
50-
}
43+
// We set the page content as the generated html by handlebars
44+
await page.setContent(html, {
45+
waitUntil: "networkidle0", // wait for page to load completely
46+
});
47+
} else {
48+
await page.goto(file.url, {
49+
waitUntil: ["load", "networkidle0"], // wait for page to load completely
50+
});
51+
}
5152

52-
return Promise.props(page.pdf(options))
53-
.then(async function (data) {
54-
await browser.close();
53+
return Promise.props(page.pdf(options))
54+
.then(async function (data) {
55+
await browser.close();
5556

56-
return Buffer.from(Object.values(data));
57-
})
58-
.asCallback(callback);
57+
return Buffer.from(Object.values(data));
58+
})
59+
.asCallback(callback);
5960
}
6061

6162
async function generatePdfs(files, options, callback) {
62-
// we are using headless mode
63-
let args = ["--no-sandbox", "--disable-setuid-sandbox"];
64-
if (options.args) {
65-
args = options.args;
66-
delete options.args;
67-
}
68-
const browser = await puppeteer.launch({
69-
args: args,
70-
});
71-
let pdfs = [];
72-
const page = await browser.newPage();
73-
for (let file of files) {
74-
if (file.content) {
75-
data = await inlineCss(file.content, { url: "/" });
76-
console.log("Compiling the template with handlebars");
77-
// we have compile our code with handlebars
78-
const template = hb.compile(data, { strict: true });
79-
const result = template(data);
80-
const html = result;
81-
// We set the page content as the generated html by handlebars
82-
await page.setContent(html, {
83-
waitUntil: "networkidle0", // wait for page to load completely
84-
});
85-
} else {
86-
await page.goto(file.url, {
87-
waitUntil: "networkidle0", // wait for page to load completely
88-
});
63+
// we are using headless mode
64+
let args = ["--no-sandbox", "--disable-setuid-sandbox"];
65+
if (options.args) {
66+
args = options.args;
67+
delete options.args;
68+
}
69+
const browser = await puppeteer.launch({
70+
args: args,
71+
});
72+
let pdfs = [];
73+
const page = await browser.newPage();
74+
for (let file of files) {
75+
if (file.content) {
76+
data = await inlineCss(file.content, {url: "/"});
77+
console.log("Compiling the template with handlebars");
78+
// we have compile our code with handlebars
79+
const template = hb.compile(data, {strict: true});
80+
const result = template(data);
81+
const html = result;
82+
// We set the page content as the generated html by handlebars
83+
await page.setContent(html, {
84+
waitUntil: "networkidle0", // wait for page to load completely
85+
});
86+
} else {
87+
await page.goto(file.url, {
88+
waitUntil: "networkidle0", // wait for page to load completely
89+
});
90+
}
91+
let pdfObj = JSON.parse(JSON.stringify(file));
92+
delete pdfObj["content"];
93+
pdfObj["buffer"] = Buffer.from(Object.values(await page.pdf(options)));
94+
pdfs.push(pdfObj);
8995
}
90-
let pdfObj = JSON.parse(JSON.stringify(file));
91-
delete pdfObj["content"];
92-
pdfObj["buffer"] = Buffer.from(Object.values(await page.pdf(options)));
93-
pdfs.push(pdfObj);
94-
}
9596

96-
return Promise.resolve(pdfs)
97-
.then(async function (data) {
98-
await browser.close();
99-
return data;
100-
})
101-
.asCallback(callback);
97+
return Promise.resolve(pdfs)
98+
.then(async function (data) {
99+
await browser.close();
100+
return data;
101+
})
102+
.asCallback(callback);
102103
}
103104

104105
module.exports.generatePdf = generatePdf;

0 commit comments

Comments
 (0)