Skip to content

Instantly share code, notes, and snippets.

@litzinger
Last active December 21, 2021 17:01
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save litzinger/b373c16afe0521f0886a3eb8391e58e6 to your computer and use it in GitHub Desktop.
Save litzinger/b373c16afe0521f0886a3eb8391e58e6 to your computer and use it in GitHub Desktop.
Simple Puppeteer screenshot script
/*
If saving AWS creds locally they need to be in ~/.aws/credentials
[default]
aws_access_key_id = foo
aws_secret_access_key = bar
*/
const puppeteer = require('puppeteer');
const AWS = require('aws-sdk');
const s3 = new AWS.S3();
const bucket = 'BucketName';
const fileName = 'file-name-'+ unique() +'.pdf';
function unique() {
return 'xxxxxxxxxxxx'.replace(/[x]/g, function(c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('http://localhost:8080/some-page', {
waitUntil: 'networkidle2' // make sure all JS is loaded and all Ajax requests are done.
});
await page.emulateMedia('print'); // or screen
const pdf = await page.pdf({
fullPage: true,
// landscape: true,
format: 'Tabloid',
printBackground: true,
path: fileName
});
const params = {
Bucket: bucket,
Key: fileName,
Body: pdf
};
// Put it into an S3 bucket
await s3.putObject(params).promise();
await browser.close();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment