Last active
January 22, 2022 18:01
-
-
Save billyyarosh/213e5bd24f5b5b7fe57e2b644b50da3c to your computer and use it in GitHub Desktop.
Example using chrome-aws-lambda layer with serverless to print PDF page.
This file contains hidden or 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
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