-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
72 lines (57 loc) · 1.53 KB
/
server.js
File metadata and controls
72 lines (57 loc) · 1.53 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import express from "express";
import puppeteer from "puppeteer-core";
import chromium from "@sparticuz/chromium";
const app = express();
app.use(express.json({ limit: "20mb" }));
// ✅ Ruta base (ya la tenías)
app.get("/", (req, res) => {
res.send("HTML → PDF service is running");
});
// ✅ NUEVO: endpoint liviano para despertar Render
app.get("/ping", (_req, res) => {
res.status(200).send("ok");
});
// 🔥 Endpoint pesado (PDF)
app.post("/render-pdf", async (req, res) => {
try {
const { html } = req.body;
if (!html) {
return res.status(400).json({ error: "HTML requerido" });
}
const browser = await puppeteer.launch({
args: chromium.args,
executablePath: await chromium.executablePath(),
headless: chromium.headless,
timeout: 60000,
});
const page = await browser.newPage();
await page.setContent(html, {
waitUntil: "networkidle0",
timeout: 60000,
});
const pdfBuffer = await page.pdf({
format: "A4",
printBackground: true,
margin: {
top: "8mm",
right: "10mm",
bottom: "15mm",
left: "10mm",
},
});
await browser.close();
res.json({
pdf_base64: pdfBuffer.toString("base64"),
});
} catch (error) {
console.error("ERROR PDF:", error);
res.status(500).json({
error: "Error generando PDF",
detail: error.message,
});
}
});
const PORT = process.env.PORT || 10000;
app.listen(PORT, () => {
console.log("HTML → PDF renderer activo en puerto", PORT);
});