Skip to content

Instantly share code, notes, and snippets.

@hashrock
Created December 21, 2017 08:53
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hashrock/ee78f93c3f9a8a9a72c7212e5a58653f to your computer and use it in GitHub Desktop.
Save hashrock/ee78f93c3f9a8a9a72c7212e5a58653f to your computer and use it in GitHub Desktop.
puppeteer - markdown-it - pdf(WIP)
const fs = require('fs');
const puppeteer = require('puppeteer');
var md = require('markdown-it')();
(async() => {
var result = md.render('# markdown-it rulezz!');
var html = `
<html><head><title>Hey</title><style></style></head>
<body>
${result}
</body></html>
`
console.log(html)
const browser = await puppeteer.launch({headless: true});
const page = await browser.newPage();
await page.setViewport({width: 1600, height: 1200, deviceScaleFactor: 2});
//await page.goto(url);
await page.setContent(html, { waitUntil: 'networkidle0' })
await page.pdf({
path: "./test.pdf",
format: 'A4',
printBackground: true,
displayHeaderFooter: true,
headerTemplate: "",
footerTemplate: "<h1 style='font-size: 17px; text-align: center; width: 100%;'><span class='pageNumber'></span> / <span class='totalPages'></span>ぺ〜じ</div>"
,margin: { left: '2cm', top: '3cm', right: '1cm', bottom: '2.5cm' }
});
browser.close();
})();
{
"dependencies": {
"file-url": "^2.0.2",
"markdown-it": "^8.4.0",
"puppeteer": "GoogleChrome/puppeteer#8a40cd5eef86fe8c3a4894775de8e1d269836857"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment