const puppeteer = require('puppeteer')
const ejs = require('ejs')

const printPDF = async (filename, data) => {

    // Create a browser instance
    const browser = await puppeteer.launch({ args: ["--no-sandbox"] });

    // Create a new page
    const [page] = await browser.pages();

    // ejs 
    const html = await ejs.renderFile(filename, data);

    // set data with html
    await page.setContent(html, {
        waitUntil: 'domcontentloaded'
    });

    // Downlaod the PDF
    const pdf = await page.pdf({
        format: 'A4',
        printBackground: true
    });
    await browser.close();
    return pdf
}

module.exports = printPDF