Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Using puppeteer-core and chrome-aws-lamba to screenshot a web element
// Code from https://github.com/leighhalliday/generate-og-image/blob/master/src/chromium.ts
// and https://github.com/wesbos/wesbos/blob/master/functions/ogimage/ogimage.js
// and thanks wes.bos for the video on this at https://youtu.be/A0Ww-SU7K5E
const chrome = require("chrome-aws-lambda");
const puppeteer = require("puppeteer-core");
const exePath = "/usr/bin/google-chrome";
async function getOptions(isDev) {
let options;
if (isDev) {
options = {
args: ["--incognito"],
executablePath: exePath,
headless: true,
};
} else {
options = {
args: chrome.args,
executablePath: await chrome.executablePath,
headless: chrome.headless,
};
}
return options;
}
async function getScreenshot(url, isDev) {
const options = await getOptions(isDev);
console.log("📸");
const browser = await puppeteer.launch(options);
const page = await browser.newPage();
await page.setViewport({ width: 720, height: 1920, deviceScaleFactor: 1.5 });
await page.goto(url);
return await page.screenshot({ type: "jpeg", quality: 100 });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment