JupiterOne / playwright-aws-lambda

Support for running Microsoft's Playwright on AWS Lambda and Google Cloud Functions
MIT License
397 stars 53 forks source link

Some characters are sticking together after PDF print #54

Open sh4nnongoh opened 2 years ago

sh4nnongoh commented 2 years ago

Example of the issue:

sticky

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:

  1. A URL is sent to the lambda.
  2. The lambda spins up a headless Chromium browser and navigates to the URL.
  3. The page is captured as a pdf and sent to S3.
  4. The lambda returns the S3 key, which allows the user to download the PDF from S3.

Help & Guidance please.

knash94 commented 1 year ago

@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