'use strict';

const puppeteer = require('puppeteer');

const createPdf = async() => {
  let browser;
  try {
    browser = await puppeteer.launch({args: ['--no-sandbox', '--disable-setuid-sandbox']});
    const page = await browser.newPage();
    //await page.setViewport({width: 794, height: 1122, deviceScaleFactor: 1});
    await page.goto(process.argv[2], {timeout: 3000, waitUntil: 'networkidle2'});
    await page.waitFor(300);
    await page.pdf({
      path: process.argv[3],
      format: 'A4',
      landscape: false,
      margin: { top: "4.5cm", right:  "1.2cm", bottom:  "3.5cm", left:  "1.22cm" },
      printBackground: true,
      footerTemplate: '<div style="width:100%;font-family:arial;font-size: 8px; text-align:right;color:#3c3c3b; margin-right: 20px;margin-bottom:8px;">Page <span class="pageNumber"></span> sur <span class="totalPages"></span>  </div>',
      displayHeaderFooter: true,
    });
  } catch (err) {
      console.log(err.message);
  } finally {
    if (browser) {
      browser.close();
    }
    process.exit();
  }
};
createPdf();