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

const fs = require("fs");

const path = require("path");

const generateEWayPDF = async () => {
  try {
    const templatePath = path.join(
      __dirname,
      "../../templates/eWayTemplate.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,
    });

    await browser.close();

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

    throw err;
  }
};

module.exports = generateEWayPDF;