playwright-aws-lambda
playwright-aws-lambda copied to clipboard
Some characters are sticking together after PDF print
Example of the issue:
Code snippet:
try {
const browser = await launchChromium({ headless: true })
const page = await browser.newPage({
viewport: {
width: 868,
height: 500,
},
})
const nextURL = new URL(url)
await page.goto(nextURL.toString())
const { getHeaderTemplate, getFooterTemplate, margin } = templates[type]
const [headerTemplate, footerTemplate] = await Promise.all([
getHeaderTemplate(templateProps),
getFooterTemplate(templateProps),
])
const buffer = await page.pdf({
displayHeaderFooter: true,
headerTemplate,
footerTemplate,
margin,
printBackground: true,
})
const compressed = zlib.gzipSync(buffer)
const contentEncoding = 'gzip'
const filename = 'filename.pdf'
const s3Key = 'mykey/'.concat(filename)
const contentType = 'application/pdf'
const putObjectParams = {
Bucket: s3Bucket,
Key: `public/${s3Key}`,
ContentType: contentType,
ContentEncoding: contentEncoding,
Body: compressed,
}
await Promise.all([s3.putObject(putObjectParams).promise(), browser.close()])
return {
statusCode: 200,
headers: defaultHeaders,
body: {
s3Key,
filename,
contentType,
contentEncoding,
},
}
} catch (error) {
console.error(error)
return {
statusCode: 500,
headers: defaultHeaders,
body: { error: error.message },
}
}
Further explanation:
- A URL is sent to the lambda.
- The lambda spins up a headless Chromium browser and navigates to the URL.
- The page is captured as a pdf and sent to S3.
- The lambda returns the S3 key, which allows the user to download the PDF from S3.
Help & Guidance please.
@sh4nnongoh I've been able to work around the issue with this, hope it helps!
browser = await playwright.launchChromium({
args: [
...playwright.getChromiumArgs(true),
'--font-render-hinting=none'
]
});
Seems to be a common chromium issue, found the solution on puppeteer https://github.com/puppeteer/puppeteer/issues/2410