|
|
import express from "express";
|
|
|
import puppeteer from "puppeteer";
|
|
|
|
|
|
const app = express();
|
|
|
app.use(express.json({ limit: "10mb" }));
|
|
|
|
|
|
const PORT = process.env.PORT || 7860;
|
|
|
|
|
|
app.post("/pdf", async (req, res) => {
|
|
|
try {
|
|
|
const { html, options = {} } = req.body;
|
|
|
|
|
|
if (!html) {
|
|
|
return res.status(400).json({ error: "Missing HTML" });
|
|
|
}
|
|
|
|
|
|
const browser = await puppeteer.launch({
|
|
|
executablePath: process.env.PUPPETEER_EXECUTABLE_PATH,
|
|
|
args: [
|
|
|
"--no-sandbox",
|
|
|
"--disable-dev-shm-usage",
|
|
|
"--disable-setuid-sandbox",
|
|
|
"--disable-gpu",
|
|
|
"--no-zygote"
|
|
|
],
|
|
|
});
|
|
|
|
|
|
const page = await browser.newPage();
|
|
|
await page.setContent(html, { waitUntil: "networkidle0" });
|
|
|
|
|
|
const pdf = await page.pdf({
|
|
|
format: "A4",
|
|
|
printBackground: true,
|
|
|
...options
|
|
|
});
|
|
|
|
|
|
await browser.close();
|
|
|
|
|
|
res.setHeader("Content-Type", "application/pdf");
|
|
|
res.send(pdf);
|
|
|
} catch (err) {
|
|
|
console.error(err);
|
|
|
res.status(500).json({ error: err.message });
|
|
|
}
|
|
|
});
|
|
|
|
|
|
|
|
|
app.get("/", (_req, res) => {
|
|
|
res.send("Puppeteer PDF Service is running");
|
|
|
});
|
|
|
|
|
|
app.listen(PORT, () => {
|
|
|
console.log(`Puppeteer PDF service running on port ${PORT}`);
|
|
|
});
|
|
|
|