Skip to content

Upgrade pdfjs-dist to 6.0.227 for latest stable PDF.js support. - #2113

Open
xbonell-sage wants to merge 3 commits into
wojtekmaj:mainfrom
xbonell-sage:main
Open

Upgrade pdfjs-dist to 6.0.227 for latest stable PDF.js support.#2113
xbonell-sage wants to merge 3 commits into
wojtekmaj:mainfrom
xbonell-sage:main

Conversation

@xbonell-sage

Copy link
Copy Markdown

Adapt LinkService, AnnotationLayer, and tests for PDF.js 6 API changes, and configure the Vitest worker so browser tests run reliably.

Adapt LinkService, AnnotationLayer, and tests for PDF.js 6 API changes, and configure the Vitest worker so browser tests run reliably.

@wojtekmaj wojtekmaj left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, not bad; a couple of notes that I'd be happy to work on further. However the breaking change in PDF.js itself should be studied, and especially the lifted minimum browser version requirements may be particularly problematic and may require a major version on out side as well.

div: layer,
imageResourcesPath,
linkService,
linkService: linkService as unknown as PDFLinkService,

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a fan of type assertions, let's fix that in a way that does not involve lying to TypeScript


try {
new pdfjs.AnnotationLayer(annotationLayerParameters).render(renderParameters);
let cancelled = false;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makeCancellable should handle that by default without the need of manually checking if promise has been cancelled.

const cancellable = makeCancellable(
new pdfjs.AnnotationLayer({
...annotationLayerParameters,
linkService: linkService as unknown as PDFLinkService,

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, I would rather not lie to TypeScript here.

Comment thread packages/react-pdf/src/LinkService.ts Outdated
Comment on lines 194 to 196
executeSetOCGState(): Promise<void> {
return Promise.resolve();
}

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or:

Suggested change
executeSetOCGState(): Promise<void> {
return Promise.resolve();
}
async executeSetOCGState(): Promise<void> {
// Intentionally empty
}

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand these changes yet...

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PDF.js 6 can render more [role="presentation"] nodes than textContent.items.length when marked content is included. Added a getRenderedTextItemCount() helper with a comment explaining this, instead of comparing against desiredTextItems.length.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand these changes yet...

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switched from asserting PDF.js internal _page* fields to public callback fields (pageNumber), which is what onLoadSuccess actually exposes. Updated the PR with a comment in the spec explaining this.

Comment thread .gitignore Outdated

# Project-generated directories and files
__screenshots__
.vitest-attachments

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Relevant? Why was this needed now?

Extend SimpleLinkService for proper typing, simplify AnnotationLayer cancellation, clarify test assertions, and remove unnecessary gitignore entry.
@xbonell-sage

xbonell-sage commented Jun 29, 2026

Copy link
Copy Markdown
Author

Thanks for the review! I've updated the PR addressing the inline comments:

  • LinkService now extends PDF.js SimpleLinkService, so linkService is passed to AnnotationLayer without type assertions.
  • AnnotationLayer no longer uses a manual cancelled flag — makeCancellable already suppresses .then/.catch after cancel(), consistent with the rest of the codebase.
  • executeSetOCGState uses the suggested async empty stub.
  • Tests now assert public callback fields (pageNumber, numPages) instead of PDF.js internals; TextLayer uses a getRenderedTextItemCount() helper with a comment explaining marked-content DOM nodes in PDF.js 6.
  • .gitignore — removed .vitest-attachments; it wasn't needed.

About the browser requirements / major version: noted that PDF.js 6 raises minimums (Chrome 125+, Safari 18+). I agree that probably this should ship as a react-pdf major separately.

Context / motivation:

We needed to upgrade pdfjs-dist in react-pdf because we also use pdfjs-dist directly in our application. Keeping both on the same major version avoids subtle runtime and type mismatches (worker URL, API surface, and shared document APIs).
In our setup, react-pdf and our own PDF.js usage must stay aligned, so this PR is primarily about version sync for a real production constraint, not just chasing the latest release.

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

Successfully merging this pull request may close these issues.

2 participants