-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
50 lines (38 loc) · 1.21 KB
/
index.js
File metadata and controls
50 lines (38 loc) · 1.21 KB
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
45
46
47
48
49
50
const express = require("express");
const fileUpload = require("express-fileupload");
const puppeteer = require("puppeteer");
const serverPort = process.env["SERVER_PORT"] || 3000;
const puppeteerOptions = {
args: [
"--no-sandbox",
"--disable-setuid-sandbox",
"--disable-dev-shm-usage",
],
};
(async () => {
const browser = await puppeteer.launch(puppeteerOptions);
const app = express();
app.use(fileUpload());
app.post("/convert", async function (req, res, next) {
try {
if (!req.files) {
res.status(400);
next();
return;
}
const htmlContent = req.files.html.data.toString();
const page = await browser.newPage();
await page.setContent(htmlContent);
await page.emulateMedia("screen");
const pdfBuffer = await page.pdf();
res.status(201).type("application/pdf").send(pdfBuffer);
next();
await page.close();
} catch (error) {
// handle errors
}
});
app.listen(serverPort, () => {
console.log(`html2pdf server is listening on port ${serverPort}`);
});
})();