Last active
December 21, 2021 17:01
-
-
Save litzinger/b373c16afe0521f0886a3eb8391e58e6 to your computer and use it in GitHub Desktop.
Simple Puppeteer screenshot script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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