CVBora / services /pdfService.js
ismizo's picture
Upload 17 files
2b1f3f2 verified
Raw History Blame Contribute Delete
1.02 kB
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<Buffer>}
*/
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 };