| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- #!/usr/bin/node
- const fs = require("fs");
- const path = require("path");
- const yaml = require("yaml");
- const puppeteer = require("puppeteer");
- const pug = require("pug");
- const cv = yaml.parse(fs.readFileSync("./cv.yaml", { encoding: "utf8" }));
- 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);
|