Skip to content

Intermittent "Failed to load PDF" with AWS S3 presigned URLs on larger files (ranges not used reliably; proxy works but direct fails) #2062

@iamalbinnj

Description

@iamalbinnj

Before you start - checklist

  • I followed instructions in documentation written for my React-PDF version
  • I have checked if this bug is not already reported
  • I have checked if an issue is not listed in Known issues
  • If I have a problem with PDF rendering, I checked if my PDF renders properly in PDF.js demo

Description

I'm unable to reliably load PDFs directly from AWS S3 presigned URLs using react-pdf.

  • Small PDFs often load fine directly via the presigned URL.
  • Larger PDFs (or files with more complex content/structure) frequently fail with "Failed to load PDF" or get stuck on loading/spinner.
  • The same presigned URL works perfectly:
    • When pasted into a new browser tab (downloads/opens, even if slowly for big files).
    • When proxied through a Next.js API route (/api/file?url=... streaming the content) — multiple 206 Partial Content range requests succeed (until hitting Amplify Hosting's ~5.7 MB response limit → 413 error).
  • No consistent CORS errors in console (some files work), no 403/404 when testing URL directly.

This strongly suggests pdf.js is not reliably using range requests on direct presigned URLs for some files, falling back to full download → timeout/failure on larger ones. Proxy hides the issue (different fetch context / no long query string).

Steps to reproduce

const proxiedUrl = /api/file?url=${encodeURIComponent(fileUrl)};

console.log("file url: ",fileUrl);
console.log("proxiedurl: ",proxiedUrl);

// PDF
if (ext === "pdf") {
return ;
}


{Array.from({ length: numPages || 0 }, (_, i) => {
const pageNumber = i + 1;

      return (
        <div
          key={pageNumber}
          ref={el => {
            pageRefs.current[pageNumber] = el;
          }}
          className="flex justify-center mb-6"
        >
          <Page
            pageNumber={pageNumber}
            width={pageWidth}
            renderAnnotationLayer={false}
          />
        </div>
      );
    })}

  </Document>

Expected behavior

large pdf should load from s3

Actual behavior

When passing the direct presigned S3 URL to in react-pdf:
Small or simple PDFs usually load successfully (pages render, viewer works).
Larger PDFs (or ones with more complex internal structure/content) fail to load:
The component shows the default error message: "Failed to load PDF file."

In the Network tab:
Initial HEAD or GET request to the presigned URL may succeed (200 OK).
But few or no subsequent 206 Partial Content range requests are made (pdf.js does not reliably use byte-range fetching).
Instead, pdf.js often attempts (or falls back to) downloading the entire file in one go → this times out, causes memory issues, or fails silently on bigger files.
No explicit CORS errors (since some files work and you've set CORS).

Additional information

No response

Environment

  • Browser (if applicable):
  • React-PDF version: 9.1.1
  • React version: 19.1.0
  • Bundler name and version (if applicable): "next": "15.5.5",

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions