to_pdf.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/usr/bin/env node
  2. const fs = require("fs");
  3. const path = require("path");
  4. const yaml = require("yaml");
  5. const { exec } = require("child_process");
  6. const puppeteer = require("puppeteer");
  7. const pug = require("pug");
  8. function parseData(dataFilePath) {
  9. return yaml.parse(fs.readFileSync(dataFilePath, { encoding: "utf8" }));
  10. }
  11. function createHtml(pugFile, data, outputPath) {
  12. const rendered = pug.renderFile(pugFile, data);
  13. fs.writeFile(outputPath, rendered, (err) => {
  14. if (err) throw err;
  15. });
  16. }
  17. async function printPDF(url) {
  18. const browser = await puppeteer.launch({ headless: "new" });
  19. const page = await browser.newPage();
  20. await page.goto(url, { waitUntil: "networkidle0" });
  21. const pdf = await page.pdf({ format: "A4" });
  22. await browser.close();
  23. return pdf;
  24. }
  25. function exportToPdf(htmlFile, pdfPath) {
  26. url = `file://${path.resolve(htmlFile)}`;
  27. printPDF(url).then((pdf) => {
  28. fs.writeFile(pdfPath, pdf, (err) => {
  29. if (err) throw err;
  30. });
  31. });
  32. }
  33. function exportRenderToPdf(dataFilePath, templatePath, assetsPath, outputPath, outputBasename) {
  34. const data = parseData(dataFilePath);
  35. const revPath = path.join(outputPath, `${outputBasename}.revision.txt`);
  36. const htmlPath = path.join(outputPath, `${outputBasename}.html`);
  37. const pdfPath = path.join(outputPath, `${outputBasename}.pdf`);
  38. const newAssetsPath = path.join(outputPath, "assets");
  39. fs.mkdirSync(outputPath, { recursive: true });
  40. fs.mkdirSync(newAssetsPath, { recursive: true });
  41. fs.cpSync(assetsPath, newAssetsPath, { recursive: true });
  42. createHtml(templatePath, data, htmlPath);
  43. console.log(`Wrote HTML file to ${htmlPath}`);
  44. exportToPdf(htmlPath, pdfPath);
  45. console.log(`Wrote PDF file to ${pdfPath}`);
  46. writeGitRevisionFile(dataFilePath, templatePath, assetsPath, revPath);
  47. }
  48. function writeGitRevisionFile(dataFilePath, templatePath, assetsPath, revPath) {
  49. const cmd = `git log --format="%H" ${dataFilePath} ${templatePath} ${assetsPath} | head -1`;
  50. exec(cmd, (err, stdout, stderr) => {
  51. if (err) {
  52. return;
  53. }
  54. fs.appendFile(revPath, stdout, (err) => {
  55. if (err) throw err;
  56. });
  57. console.log(`Wrote file revision data to ${revPath}`);
  58. });
  59. }
  60. const [inputPath, variantName, outputPath] = process.argv.slice(2);
  61. if (!inputPath || !variantName || !outputPath) {
  62. console.error(`The script should be called like :\n${process.argv[1]} input_path variant_name output_dir`);
  63. process.exit(1);
  64. }
  65. const inputDirName = inputPath.split(path.sep).at(-1);
  66. const dataFilePath = path.join(inputPath, `${variantName}.yaml`);
  67. const templatePath = path.join(inputPath, `template.pug`);
  68. const assetsPath = path.join(inputPath, "assets");
  69. exportRenderToPdf(dataFilePath, templatePath, assetsPath, outputPath, `${inputDirName}_${variantName}`);