-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
76 lines (61 loc) · 1.88 KB
/
index.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import express, { json } from "express";
import { writeFileSync, unlinkSync } from "fs";
import cors from "cors";
import carbone from "carbone";
import { convert } from "libreoffice-convert";
import { promisify } from "util";
import { v4 as uuidv4 } from "uuid";
import path from "path";
const _dirname = path.resolve();
const convertAsync = promisify(convert);
console.log(process.platform, process.arch);
const app = express();
const corsOptions = {
origin: "*",
optionsSuccessStatus: 200,
};
app.use(cors(corsOptions));
app.use(json({ limit: "200mb" }));
app.get("/", (req, res) => {
res.send("Hello Carbone: Your IP: " + req.ip);
});
app.post("/generate", async (req, res) => {
let { template, data, convertTo, templateExtension } = req.body;
if (!convertTo) {
convertTo = "pdf";
}
// convert base64 to binary
const binary = Buffer.from(template, "base64");
// template name with timestamp
const input = path.join(
_dirname,
"output",
`template-${uuidv4()}.${templateExtension}`
);
// write to disk
writeFileSync(input, binary);
const processResult = async (err, result) => {
if (err) {
return res
.status(400)
.json({ error: err.message, message: "Could parse data" });
}
try {
// Convert it to pdf format with undefined filter (see Libre office docs about filter)
const pdfBuf = await convertAsync(result, `${convertTo}`, undefined);
// send to client
res.contentType(`application/${convertTo}`);
res.send(pdfBuf);
} catch (error) {
console.log(error);
return res
.status(500)
.json({ error: error.message, message: "Could not convert to pdf" });
}
// delete template
unlinkSync(input);
};
carbone.render(input, data, processResult);
});
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => console.log(`Server started on port ${PORT}`));