React-PDF rendering in AWS Lambda slows down significantly on subsequent renders

36 views Asked by At

I'm encountering performance issues with rendering PDF files using react-pdf within an AWS Lambda function. Initially, the rendering process is slow but manageable. However, when attempting to render a PDF for the second time, the performance degradation becomes severe, making the application unusable.

Here's a brief overview of my setup:

I'm using react-pdf library to render PDF files. The rendering process is carried out within an AWS Lambda function. The AWS Lambda function is deployed using AWS CDK (Cloud Development Kit). Despite trying various optimization techniques, I'm still unable to resolve this issue. Has anyone encountered similar problems with react-pdf rendering in AWS Lambda? If so, what strategies or optimizations can be employed to improve rendering performance, especially for subsequent renders? How could i debug this to find out more info?

  console.log("Generating first page");
  const pdfPages = [];
  const mainPage = await getMainPage(reportModel);
  pdfPages.push(mainPage);

  console.log("Generating second page");
  const secondPage = await getSecondPage(reportModel);
  pdfPages.push(secondPage);

  console.log("Rendering pages");  <==== Last log in Cloudwatch before hanging.
  const pdfStream = await ReactPDF.renderToStream(
    <Document>{...pdfPages}</Document>
  );
  console.log("Saving to S3");
  const s3Key = `reports/pdf/${reportModel.permit.permit_id}.pdf`;
  await putPdfToS3(s3Key, pdfStream);

Any insights or suggestions would be greatly appreciated. Thank you!

0

There are 0 answers