Create index.js
Browse files
index.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const express = require("express");
|
| 2 |
+
const app = express();
|
| 3 |
+
const puppeteer = require("puppeteer-extra");
|
| 4 |
+
const StealthPlugin = require("puppeteer-extra-plugin-stealth");
|
| 5 |
+
const bodyParser = require("body-parser");
|
| 6 |
+
const multer = require("multer");
|
| 7 |
+
const markdownIt = require("markdown-it");
|
| 8 |
+
|
| 9 |
+
app.use(bodyParser.json());
|
| 10 |
+
app.use(bodyParser.urlencoded({ extended: false }));
|
| 11 |
+
app.use(multer().array(""));
|
| 12 |
+
|
| 13 |
+
puppeteer.use(StealthPlugin());
|
| 14 |
+
|
| 15 |
+
const initBrowser = puppeteer.launch({
|
| 16 |
+
executablePath: process.env.CHROME_BIN || null,
|
| 17 |
+
defaultViewport: null,
|
| 18 |
+
headless: true,
|
| 19 |
+
});
|
| 20 |
+
|
| 21 |
+
// Fungsi untuk mengonversi Markdown ke HTML
|
| 22 |
+
const convertMarkdownToHTML = (markdown) => {
|
| 23 |
+
const md = new markdownIt();
|
| 24 |
+
return md.render(markdown);
|
| 25 |
+
};
|
| 26 |
+
|
| 27 |
+
// Fungsi untuk membuat PDF dari Markdown
|
| 28 |
+
async function MdtoPdf(md, cssCustom, type) {
|
| 29 |
+
const browser = await initBrowser;
|
| 30 |
+
const page = await browser.newPage();
|
| 31 |
+
const htmlContent = convertMarkdownToHTML(md);
|
| 32 |
+
const fullHtmlContent = `
|
| 33 |
+
<html>
|
| 34 |
+
<head>
|
| 35 |
+
<link href="https://fonts.googleapis.com/css2?family=Noto+Sans+Arabic&display=swap" rel="stylesheet">
|
| 36 |
+
<style>
|
| 37 |
+
body {
|
| 38 |
+
font-family: 'Noto Sans Arabic', sans-serif;
|
| 39 |
+
margin: 0;
|
| 40 |
+
padding: 0;
|
| 41 |
+
}
|
| 42 |
+
${cssCustom}
|
| 43 |
+
</style>
|
| 44 |
+
</head>
|
| 45 |
+
<body>
|
| 46 |
+
${htmlContent}
|
| 47 |
+
</body>
|
| 48 |
+
</html>
|
| 49 |
+
`;
|
| 50 |
+
await page.setContent(fullHtmlContent, { waitUntil: "networkidle0" });
|
| 51 |
+
const pdfBuffer = await page.pdf({
|
| 52 |
+
format: type || "A4",
|
| 53 |
+
printBackground: true,
|
| 54 |
+
margin: {
|
| 55 |
+
top: "10mm", // Jarak atas
|
| 56 |
+
bottom: "10mm", // Jarak bawah
|
| 57 |
+
left: "10mm", // Jarak kiri
|
| 58 |
+
right: "10mm", // Jarak kanan
|
| 59 |
+
},
|
| 60 |
+
});
|
| 61 |
+
await page.close();
|
| 62 |
+
return pdfBuffer;
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
app.get("/", (req, res) => {
|
| 66 |
+
res.send("Hello World");
|
| 67 |
+
});
|
| 68 |
+
|
| 69 |
+
app.post("/mdtopdf", async (req, res) => {
|
| 70 |
+
const data = req.body;
|
| 71 |
+
if (!("md" in data)) {
|
| 72 |
+
return res.status(400).json({
|
| 73 |
+
error: "No markdown parameter in request",
|
| 74 |
+
});
|
| 75 |
+
}
|
| 76 |
+
const md = data["md"];
|
| 77 |
+
const css = data["css"];
|
| 78 |
+
const type = data["type"] || "A4";
|
| 79 |
+
try {
|
| 80 |
+
const pdfBuffer = await MdtoPdf(md, css, type);
|
| 81 |
+
res.set("Content-Type", "application/pdf");
|
| 82 |
+
res.send(pdfBuffer);
|
| 83 |
+
} catch (e) {
|
| 84 |
+
res.status(500).json({
|
| 85 |
+
error: "Failed to generate PDF",
|
| 86 |
+
});
|
| 87 |
+
}
|
| 88 |
+
});
|
| 89 |
+
|
| 90 |
+
app.listen(8080, () => console.log("Server running at port 8080"));
|