| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- #!/usr/bin/env node
- const fs = require("fs");
- const path = require("path");
- const yaml = require("yaml");
- const puppeteer = require("puppeteer");
- const pug = require("pug");
- function parseData(dataFilePath) {
- return yaml.parse(fs.readFileSync(dataFilePath, { encoding: "utf8" }));
- }
- function createHtml(pugFile, data, outputPath) {
- const rendered = pug.renderFile(pugFile, data);
- fs.writeFile(outputPath, rendered, (err) => {
- if (err) throw err;
- });
- }
- async function printPDF(url) {
- const browser = await puppeteer.launch({ headless: "new" });
- const page = await browser.newPage();
- await page.goto(url, { waitUntil: "networkidle0" });
- const pdf = await page.pdf({ format: "A4" });
- await browser.close();
- return pdf;
- }
- function exportToPdf(htmlFile, pdfPath) {
- url = `file://${path.resolve(htmlFile)}`;
- printPDF(url).then((pdf) => {
- fs.writeFile(pdfPath, pdf, (err) => {
- if (err) throw err;
- });
- });
- }
- function exportRenderToPdf(dataFilePath, templatePath, assetsPath, outputPath, outputBasename) {
- const data = parseData(dataFilePath);
- const htmlPath = path.join(outputPath, `${outputBasename}.html`);
- const pdfPath = path.join(outputPath, `${outputBasename}.pdf`);
- const newAssetsPath = path.join(outputPath, "assets");
- fs.mkdirSync(outputPath, { recursive: true });
- fs.mkdirSync(newAssetsPath, { recursive: true });
- fs.cpSync(assetsPath, newAssetsPath, { recursive: true });
- createHtml(templatePath, data, htmlPath);
- console.log(`Wrote HTML file to ${htmlPath}`);
- exportToPdf(htmlPath, pdfPath);
- console.log(`Wrote PDF file to ${pdfPath}`);
- }
- const [inputPath, templateName, variantName, outputPath] = process.argv.slice(2);
- if (!inputPath || !templateName || !variantName || !outputPath) {
- console.error(
- `The script should be called like :\n${process.argv[1]} input_path template_name variant_name output_dir`
- );
- process.exit(1);
- }
- const dataFilePath = path.join(inputPath, `${variantName}.yaml`);
- const templatePath = path.join(inputPath, `${templateName}.pug`);
- const assetsPath = path.join(inputPath, "assets");
- exportRenderToPdf(dataFilePath, templatePath, assetsPath, outputPath, "resume");
|