const puppeteer = require('puppeteer'); /** * Renders an HTML string to a PDF buffer using Puppeteer. * @param {string} html - A complete standalone HTML document string * @returns {Promise} */ const createPdfFromHtml = async (html) => { const browser = await puppeteer.launch({ headless: "new", args: ['--no-sandbox', '--disable-setuid-sandbox'], }); const page = await browser.newPage(); // Increase viewport height dramatically to ensure long CVs aren't cropped during rendering buildup await page.setViewport({ width: 1200, height: 2000 }); await page.setContent(html, { waitUntil: 'networkidle0' }); const pdfBuffer = await page.pdf({ format: 'A4', printBackground: true, // Ensure no fixed height forces truncation pageRanges: '', margin: { top: '5px', bottom: '5px', left: '5px', right: '5px' }, }); await browser.close(); return Buffer.from(pdfBuffer); }; module.exports = { createPdfFromHtml };