Skip to content

Instantly share code, notes, and snippets.

@fujimura
Created March 12, 2018 13:40
Show Gist options
  • Save fujimura/09d4935a357d2da2932b20d4fd7aa47d to your computer and use it in GitHub Desktop.
Save fujimura/09d4935a357d2da2932b20d4fd7aa47d to your computer and use it in GitHub Desktop.
markdown_to_html_slide_to_pdf.js
const fs = require("fs");
const { execSync } = require("child_process");
const path = require("path");
const puppeteer = require("puppeteer");
const htmlToPdf = async src => {
const browser = await puppeteer.launch();
try {
const dest = `${path.basename(src)}.pdf`;
const page = await browser.newPage();
await page.goto(`file://${src}`);
await page.pdf({
path: dest,
width: 1024,
height: 768
});
return dest;
} finally {
browser.close()
}
};
fs.watch("./", async (eventType, filename) => {
if (filename && path.extname(filename) == ".md") {
const f = `${path.basename(filename)}.html`;
console.log(eventType, filename, f);
execSync(`pandoc -t slidy -s ${filename} -o ${f}`);
const pdfPath = await htmlToPdf(path.resolve(f));
execSync(`open ${pdfPath}`);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment