DEV Community

Hossain MD Athar
Hossain MD Athar

Posted on

Answer: React-PDF Slow Performance with large PDF, reneders unneccessarily.

If, in 2025, anyone is facing the same issue with react-pdf causing excessive bundle size or initial page load lag, you can try the following approach. Instead of using React.lazy() (which is for JSX components), I'm using standard dynamic imports for the required functions and components. This ensures they are imported and processed only when the PDF button is clicked, not on every page render. This solves the performance and bundle size issues.

const handleGeneratePdf = async () => {
    if (!ticket || !company) return;

    try {
      const { pdf } = await import("@react-pdf/renderer");
      const { default: CertiPdf } = await import("../CertPdf");

      // Generate PDF blob
      const blob = await pdf(<CertPdf company={company} ticket={ticket} />).toBlob();

      // Create URL for viewing/printing
      const url = URL.createObjectURL(blob);
      setPdfUrl(url);
    } catch (error) {
      console.error("Error generating PDF:", error);
    } 
  };

Enter fullscreen mode Exit fullscreen mode

Top comments (0)