Created
July 19, 2022 13:12
-
-
Save ajaykarthikr/03e6f55ab0f7fb32d2255e4a7f370872 to your computer and use it in GitHub Desktop.
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'); | |
const playwright = require('playwright-core'); | |
const serverless = require("serverless-http"); | |
const express = require("express"); | |
const app = express(); | |
app.get("/", async function (req, res) { | |
let browser = null; | |
var resp = {} | |
try { | |
const browser = await playwright.chromium.launch({ | |
args: chromium.args, | |
executablePath: await chromium.executablePath, | |
headless: chromium.headless, | |
}); | |
let page = await browser.newPage(); | |
await page.goto('https://google.com'); | |
// This make sures it renders pdf properly | |
await page.emulateMedia({ media: 'screen' }); | |
const binData = await page.pdf({ | |
printBackground: true, | |
}); | |
console.log('Pdf size', binData?.length); | |
// convert binary data to base64 encoded string | |
const data = Buffer.from(binData).toString('base64'); | |
resp = res.status(200).json({ | |
data: data | |
}) | |
} catch (error) { | |
resp = res.status(400).json({ | |
error: error | |
}) | |
} finally { | |
if (browser !== null) { | |
await browser.close(); | |
} | |
} | |
return resp; | |
}); | |
module.exports.handler = serverless(app); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment