const puppeteer = require('puppeteer');
const fs = require('fs');
const path = require('path');

const dir = 'D:\\projects\\charging-cabinet\\docs';

async function convert(htmlName, pdfName) {
  const html = fs.readFileSync(path.join(dir, htmlName), 'utf8');
  const browser = await puppeteer.launch({ headless: 'new', executablePath: 'C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe', args: ['--no-sandbox'] });
  const page = await browser.newPage();
  await page.setContent(html, { waitUntil: 'networkidle0' });
  await page.pdf({
    path: path.join(dir, pdfName),
    format: 'A4',
    margin: { top: '20mm', bottom: '20mm', left: '20mm', right: '20mm' },
    printBackground: true,
  });
  await browser.close();
  console.log(`OK: ${pdfName}`);
}

(async () => {
  await convert('开发合同.html', '开发合同.pdf');
  await convert('附件-开发需求规格说明书.html', '附件-开发需求规格说明书.pdf');
  console.log('Done');
})();
