#!/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 parseResume(resumePath) { return yaml.parse(fs.readFileSync(resumePath, { encoding: "utf8" })); } function createHtml(pugFile, resumeData, outputPath) { const rendered = pug.renderFile(pugFile, resumeData); 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 exportResumeToPdf(resumeDataPath, localPugPath, assetsPath, outputPath) { const resumeData = parseResume(resumeDataPath); const htmlPath = path.join(outputPath, "resume.html"); const pdfPath = path.join(outputPath, "resume.pdf"); const newAssetsPath = path.join(outputPath, "assets"); fs.mkdirSync(outputPath, { recursive: true }); fs.mkdirSync(newAssetsPath, { recursive: true }); fs.cpSync(assetsPath, newAssetsPath, { recursive: true }); createHtml(localPugPath, resumeData, htmlPath); console.log(`Wrote HTML file to ${htmlPath}`); exportToPdf(htmlPath, pdfPath); console.log(`Wrote PDF file to ${pdfPath}`); } const [resumeDataPath, resumeTemplatePath, assetsPath, outputPath] = process.argv.slice(2); if (!resumeDataPath || !resumeTemplatePath || !assetsPath || !outputPath) { console.error( `The script should be called like :\n${process.argv[1]} resume_path template_path assets_dir output_dir` ); process.exit(1); } exportResumeToPdf(resumeDataPath, resumeTemplatePath, assetsPath, outputPath);