Skip to content

Some characters are sticking together after PDF print #54

Open
@sh4nnongoh

Description

@sh4nnongoh

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions