#!/usr/bin/node const fs = require('fs') const path = require('path') const puppeteer = require('puppeteer') const pug = require('pug') const cv = require('./cv.json') function changeExt(filePath, new_) { parsed = path.parse(filePath) return path.join(parsed.dir, `${path.basename(parsed.name)}.${new_}`) } function createHtml(pug_file) { const rendered = pug.renderFile(pug_file, cv) const html_path = changeExt(pug_file, "html") fs.writeFile(html_path, rendered, (err) => { if (err) throw err; }) console.log(`Wrote HTML file to ${html_path}`) return html_path } 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) { url = `file://${path.resolve(htmlFile)}` pdf_path = changeExt(htmlFile, "pdf") printPDF(url).then((pdf) => { fs.writeFile(pdf_path, pdf, (err) => { if (err) throw err; }) console.log(`Wrote PDF file to ${pdf_path}`) }) } local_pug_path = './resume.pug' local_pdf_path = changeExt(local_pug_path, "pdf") local_html_path = createHtml(local_pug_path) exportToPdf(local_html_path)