Skip to content

Instantly share code, notes, and snippets.

@billyyarosh
Last active January 22, 2022 18:01
Show Gist options
  • Save billyyarosh/213e5bd24f5b5b7fe57e2b644b50da3c to your computer and use it in GitHub Desktop.
Save billyyarosh/213e5bd24f5b5b7fe57e2b644b50da3c to your computer and use it in GitHub Desktop.
Example using chrome-aws-lambda layer with serverless to print PDF page.
const chromium = require('chrome-aws-lambda');
module.exports.handler = async (event) => {
let browser = null;
try {
browser = await chromium.puppeteer.launch({
args: chromium.args,
defaultViewport: chromium.defaultViewport,
executablePath: await chromium.executablePath,
headless: chromium.headless
});
const page = await browser.newPage();
await page.goto('https://google.com');
const pdf = await page.pdf({format: 'A4'});
return {
statusCode: 200,
headers: {
"Content-type": "application/pdf",
"accept-ranges": "bytes",
'Access-Control-Allow-Origin': '*',
},
body: pdf.toString('base64'),
isBase64Encoded : true
}
} finally {
if (browser !== null) {
await browser.close();
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment