File size: 4,623 Bytes
7b9792a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5a2b67f
7b9792a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
const express = require("express");
const cors = require("cors");
const puppeteer = require("puppeteer");

const app = express();
const PORT = process.env.PORT || 7860; // Hugging Face uses port 786

// Middleware
app.use(cors());
app.use(express.json({ limit: "5mb" }));

// Root endpoint - Health check
app.get("/", (_req, res) => {
  res.json({ 
    status: "ok", 
    message: "HTML to PDF API is running",
    version: "1.0.0",
    endpoints: {
      health: "GET / or GET /health",
      convert: "POST /api/html-to-pdf"
    },
    usage: {
      method: "POST",
      url: "/api/html-to-pdf",
      body: {
        html_content: "Your HTML content here",
        pdf_options: {
          format: "A4",
          printBackground: true,
          margin: {
            top: "10mm",
            right: "10mm",
            bottom: "10mm",
            left: "10mm"
          }
        }
      }
    }
  });
});

// Health check endpoint
app.get("/health", (_req, res) => {
  res.json({ 
    status: "healthy",
    timestamp: new Date().toISOString(),
    uptime: process.uptime()
  });
});

// Main PDF conversion endpoint
app.post("/api/html-to-pdf", async (req, res) => {
  let browser;
  const startTime = Date.now();
  
  try {
    const { html_code: htmlContent, pdf_options: pdfOptions } = req.body || {};

    // Validate input
    if (!htmlContent || !htmlContent.trim()) {
      return res.status(400).json({ 
        error: "html_content is required",
        success: false 
      });
    }

    console.log(`[${new Date().toISOString()}] Starting PDF generation...`);

    // Launch browser
    browser = await puppeteer.launch({
      executablePath: "/usr/bin/chromium",
      args: [
        "--no-sandbox",
        "--disable-setuid-sandbox",
        "--disable-dev-shm-usage",
        "--disable-gpu",
        "--disable-software-rasterizer",
        "--disable-extensions",
        "--disable-background-networking",
        "--disable-default-apps",
        "--disable-sync",
        "--metrics-recording-only",
        "--mute-audio",
        "--no-first-run"
      ],
      headless: true
    });

    const page = await browser.newPage();
    
    // Set content
    await page.setContent(
      `<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
</head>
<body>
${htmlContent}
</body>
</html>`,
      { 
        waitUntil: "networkidle0", 
        timeout: 30000 
      }
    );

    // Generate PDF with default options merged with user options
    const defaultPdfOptions = {
      format: "A4",
      printBackground: true,
      margin: { 
        top: "10mm", 
        right: "10mm", 
        bottom: "10mm", 
        left: "10mm" 
      }
    };

    const pdfBuffer = await page.pdf({
      ...defaultPdfOptions,
      ...(pdfOptions || {})
    });

    await browser.close();
    browser = null;

    const processingTime = Date.now() - startTime;
    console.log(`[${new Date().toISOString()}] PDF generated successfully in ${processingTime}ms`);

    res.json({ 
      pdf_base64: pdfBuffer.toString("base64"),
      success: true,
      processing_time_ms: processingTime,
      pdf_size_bytes: pdfBuffer.length
    });

  } catch (err) {
    console.error(`[${new Date().toISOString()}] PDF Generation Error:`, err);
    
    if (browser) {
      try {
        await browser.close();
      } catch (closeErr) {
        console.error("Browser close error:", closeErr);
      }
    }

    res.status(500).json({ 
      error: "Failed to generate PDF", 
      details: err.message,
      success: false 
    });
  }
});

// 404 handler
app.use((_req, res) => {
  res.status(404).json({ 
    error: "Endpoint not found",
    available_endpoints: [
      "GET /",
      "GET /health",
      "POST /api/html-to-pdf"
    ]
  });
});

// Start server
app.listen(PORT, "0.0.0.0", () => {
  console.log(`
╔════════════════════════════════════════╗
β•‘   HTML to PDF API Server               β•‘
β•‘   Status: Running βœ…                   β•‘
β•‘   Port: ${PORT}                           β•‘
β•‘   URL: http://0.0.0.0:${PORT}            β•‘
β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•
  `);
});

// Graceful shutdown
process.on('SIGTERM', () => {
  console.log('SIGTERM signal received: closing HTTP server');
  process.exit(0);
});

process.on('SIGINT', () => {
  console.log('SIGINT signal received: closing HTTP server');
  process.exit(0);
});