Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some characters are sticking together after PDF print #54

Open
sh4nnongoh opened this issue Jun 24, 2022 · 1 comment
Open

Some characters are sticking together after PDF print #54

sh4nnongoh opened this issue Jun 24, 2022 · 1 comment

Comments

@sh4nnongoh
Copy link

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
Copy link

knash94 commented Nov 3, 2022

@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 puppeteer/puppeteer#2410

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants