const puppeteer = require("puppeteer-core");

const fs = require("fs");

const path = require("path");

const generateEInvoicePDF = async () => {
  try {
    const templatePath = path.join(
      __dirname,
      "../../templates/eInvoiceTemplate.html"
    );

    const html = fs.readFileSync(templatePath, "utf8");

    const browser = await puppeteer.launch({
      headless: true,

      executablePath:
        "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe",

      args: [
        "--no-sandbox",
        "--disable-setuid-sandbox",
        "--disable-dev-shm-usage",
        "--disable-gpu",
      ],
    });

    const page = await browser.newPage();

    await page.setContent(html, {
      waitUntil: "load",
    });

    const pdf = await page.pdf({
      format: "A4",
      printBackground: true,
      margin: {
        top: "10px",
        bottom: "10px",
        left: "10px",
        right: "10px",
      },
    });

    await browser.close();

    return pdf;
  } catch (err) {
    console.log("E-INVOICE PDF ERROR", err);

    throw err;
  }
};

module.exports = generateEInvoicePDF;