Skip to content

Instantly share code, notes, and snippets.

@ultim8k
Created March 20, 2018 11:56
Show Gist options
  • Save ultim8k/31fb0f069466357614a26afd1c0ad277 to your computer and use it in GitHub Desktop.
Save ultim8k/31fb0f069466357614a26afd1c0ad277 to your computer and use it in GitHub Desktop.
pdf & screenshot generator
/**
*
* Generate screenshot and pdf from webpage
*
* Read more here:
* https://medium.com/@dschnr/using-headless-chrome-as-an-automated-screenshot-tool-4b07dffba79a
* https://github.com/GoogleChrome/puppeteer
*
*/
const puppeteer = require('puppeteer');
const dedent = require('dedent-js');
const namesList = [
'Euclid',
'Democritus',
'Neuton',
'Einstein',
'Tesla'
];
const renderTemplate = (name) => dedent`
<!DOCTYPE html>
<html>
<head></head>
<body>
<h1>Hey there, ${name}!</h1>
</body>
</html>
`;
// const markups = namesList.map(renderTemplate);
(async () => {
console.log('Super generator 3000');
const browser = await puppeteer.launch();
const page = await browser.newPage();
// await page.goto('https://oranger.me', {waitUntil: 'networkidle2'});
// await page.setContent('<!DOCTYPE html><html><head></head><body><h1>Hey there!</h1></body></html>');
for (let name of namesList) {
console.log('Creating file for ', name);
const markup = renderTemplate(name);
await page.setContent(markup);
await page.setViewport({
width: 800,
height: 600,
deviceScaleFactor: 3
});
await page.pdf({path: `./build/greet-${name}.pdf`, format: 'A4'});
await page.screenshot({path: `./build/greet-${name}.jpg`, type: 'jpeg', quality: 100});
}
console.log('done');
await browser.close();
})();
{
"name": "awesomeness-generator",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {
"dedent-js": "^1.0.1",
"puppeteer": "^1.2.0"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "me",
"license": "ISC"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment