#!/usr/bin/env node /** * Fixed PDF Generation with Proper Page Breaks and A4 Format * * Fixes: * 1. No unexpected blank pages * 2. Correct page size (A4) * 3. Proper handling of page breaks using CSS * 4. Page numbers in footer should match actual HTML pages */ const express = require('express'); const puppeteer = require('puppeteer'); const bodyParser = require('body-parser'); const cors = require('cors'); const fs = require('fs'); const path = require('path'); const crypto = require('crypto'); const app = express(); const PORT = process.env.PORT || 8001; // Different port to avoid conflicts // Configure temp folder const TEMP_FOLDER = 'temp'; if (!fs.existsSync(TEMP_FOLDER)) { fs.mkdirSync(TEMP_FOLDER, { recursive: true }); } // Middleware app.use(cors()); app.use(bodyParser.json({ limit: '100mb' })); app.use(bodyParser.urlencoded({ extended: true, limit: '100mb' })); // Serve static files from temp folder app.use('/download', express.static(TEMP_FOLDER)); /** * Generate A4 PDF with proper page breaks and no extra pages */ async function generateFixedA4PDF(htmlContent, outputPDF) { let browser = null; try { console.log("🚀 Starting FIXED PDF generation..."); // Launch browser browser = await puppeteer.launch({ headless: true, args: [ '--no-sandbox', '--disable-setuid-sandbox', '--disable-dev-shm-usage', '--disable-accelerated-2d-canvas', '--no-first-run', '--no-zygote', '--disable-gpu' ] }); const page = await browser.newPage(); // Set viewport to A4 dimensions (210mm x 297mm = 794px x 1123px at 96 DPI) await page.setViewport({ width: 794, height: 1123 }); page.setDefaultTimeout(0); // Infinite timeout // Create improved HTML with proper page break CSS const fixedHTML = `