Skip to content

feat: added improved pdf viewer#2833

Open
JonasKenke wants to merge 5 commits intoChainlit:mainfrom
JonasKenke:feat/pdf-viewer
Open

feat: added improved pdf viewer#2833
JonasKenke wants to merge 5 commits intoChainlit:mainfrom
JonasKenke:feat/pdf-viewer

Conversation

@JonasKenke
Copy link

@JonasKenke JonasKenke commented Mar 8, 2026

Summary

This change replaces the previous iframe-based browser PDF viewer with a new, enhanced PDF-viewer component and adds the associated support in both frontend and e2e test suites.

Validation

All tests passed ✔ All specs passed! 06:27

What changed

  • Replaced the old iframe‑based browser PDF viewer with a custom React viewer.
  • Added new PDF component and wired it into message/side‑view code.
  • Updated tests: new Cypress spec, dummy PDF, and setup helpers.
  • Bumped frontend deps and adjusted .npmrc

Old:

Screenshot_20260308_204628

New:

Screenshot_20260308_210416 Screenshot_20260308_210435

Summary by cubic

Replaced the iframe PDF viewer with a React-based viewer that supports inline previews, a fullscreen modal, and a side-panel that auto-opens only for new or changed elements. Ships a local pdfjs-dist worker (no CDN) with e2e coverage.

  • New Features

    • Custom viewer built with react-pdf and a toolbar for zoom, pagination, download, and print.
    • Inline thumbnail opens a fullscreen modal; side-display PDFs render with the viewer and auto-open the panel only when new/changed.
    • Local pdfjs-dist worker is bundled and hoisted via .npmrc (no external CDN).
  • Bug Fixes

    • Serve .mjs as JavaScript to prevent MIME errors.
    • Improved scrolling and layout: switched to overflow-auto, removed fixed inline height, and increased default side panel width.
    • Test setup polyfills DOMMatrix for pdfjs in JSDOM; e2e asserts the local worker loads with zero CDN requests.

Written for commit cfd840f. Summary will update on new commits.

Copilot AI review requested due to automatic review settings March 8, 2026 20:29
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. e2e-tests Has E2E tests frontend Pertains to the frontend. labels Mar 8, 2026
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

6 issues found across 12 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="cypress/e2e/pdf_viewer/spec.cy.ts">

<violation number="1" location="cypress/e2e/pdf_viewer/spec.cy.ts:6">
P2: Test uses non-existent Cypress event `window:console`, so console error collection never executes and the no-console-error assertion is effectively meaningless.</violation>

<violation number="2" location="cypress/e2e/pdf_viewer/spec.cy.ts:37">
P2: The MIME/CDN error test is effectively a no-op because it clears and asserts `consoleErrors` immediately without waiting for PDF load-time console output.</violation>

<violation number="3" location="cypress/e2e/pdf_viewer/spec.cy.ts:53">
P2: Test title claims worker-origin validation, but assertions only verify PDF rendering and cannot detect CDN worker usage.</violation>
</file>

<file name="frontend/src/components/chat/MessagesContainer/index.tsx">

<violation number="1" location="frontend/src/components/chat/MessagesContainer/index.tsx:97">
P2: Auto-open effect updates `sideViewState` but never clears it, allowing stale/reopened side panel state across `elements` updates.</violation>
</file>

<file name="frontend/src/components/Elements/PDF.tsx">

<violation number="1" location="frontend/src/components/Elements/PDF.tsx:46">
P2: `startPage` is not clamped to a minimum of 1, so invalid negative page numbers can be sent to `<Page pageNumber>`.</violation>

<violation number="2" location="frontend/src/components/Elements/PDF.tsx:67">
P2: `window.open` is used with `_blank` without `noopener`, enabling opener access for opened URLs and creating reverse-tabnabbing risk.</violation>
</file>

Since this is your first cubic review, here's how it works:

  • cubic automatically reviews your code and comments on bugs and improvements
  • Teach cubic by replying to its comments. cubic learns from your replies and gets better over time
  • Add one-off context when rerunning by tagging @cubic-dev-ai with guidance or docs links (including llms.txt)
  • Ask questions if you need clarification on any suggestion

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR replaces the existing iframe-based PDF rendering with a custom React PDF viewer (react-pdf/pdfjs) and wires it into inline and side-panel element rendering, with supporting backend MIME-type handling and new Cypress coverage.

Changes:

  • Introduces a new PDFElement/PDFViewer React component for inline previews, modal fullscreen viewing, and side/page display.
  • Updates chat + side-view behavior to better accommodate PDF rendering (auto-open side panel on side elements; scrolling/layout tweaks).
  • Adds an e2e test app/spec (plus a dummy PDF) and updates frontend dependencies/test setup for pdfjs compatibility.

Reviewed changes

Copilot reviewed 10 out of 12 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
frontend/tests/setup-tests.ts Adds a JSDOM DOMMatrix polyfill required by pdfjs-dist during test imports.
frontend/src/components/chat/MessagesContainer/index.tsx Adds logic to auto-open side panel when display='side' elements are present.
frontend/src/components/chat/Messages/Message/Content/InlinedElements/InlinedPDFList.tsx Adjusts inline PDF wrapper layout for the new component.
frontend/src/components/Elements/PDF.tsx Replaces iframe rendering with a new react-pdf-based viewer (toolbar, zoom/paging, modal).
frontend/src/components/ElementSideView.tsx Updates overflow behavior and default sizing to better support the new viewer.
frontend/package.json Adds react-pdf dependency.
frontend/pnpm-lock.yaml Locks react-pdf / pdfjs-dist and transitive deps.
cypress/e2e/pdf_viewer/spec.cy.ts Adds Cypress spec validating inline preview, modal open/close, and side rendering.
cypress/e2e/pdf_viewer/main.py Adds a Chainlit app fixture that emits inline + side PDF elements.
cypress/e2e/pdf_viewer/dummy.pdf Adds a dummy PDF used by the Cypress app fixture.
backend/chainlit/server.py Ensures .mjs files are served with application/javascript (PDF worker).
.npmrc Adjusts hoisting to ensure pdfjs-dist is available in the workspace layout.
Files not reviewed (1)
  • frontend/pnpm-lock.yaml: Language not supported

Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

2 issues found across 3 files (changes from recent commits).

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="cypress/e2e/pdf_viewer/spec.cy.ts">

<violation number="1" location="cypress/e2e/pdf_viewer/spec.cy.ts:37">
P2: The MIME/CDN regression test is vacuous and no longer verifies MIME/CDN error conditions; it can pass with zero validated PDF resources.</violation>
</file>

<file name="frontend/src/components/chat/MessagesContainer/index.tsx">

<violation number="1" location="frontend/src/components/chat/MessagesContainer/index.tsx:109">
P2: `sideView` can become stale because it is only updated when a new side-element ID appears, not when existing side elements change.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 10 out of 12 changed files in this pull request and generated 1 comment.

Files not reviewed (1)
  • frontend/pnpm-lock.yaml: Language not supported

@Allaoua9
Copy link
Contributor

Allaoua9 commented Mar 9, 2026

Thanks for putting this together — I can see the effort that went into improving the PDF experience here.

I do have a few questions to better understand the trade-offs.

What are the main limitations of the native browser PDF viewer that this change is trying to address? In my experience, Chrome’s built-in PDF viewer is already quite good, and Firefox’s native experience is also solid. I dont know about other browsers though.

Could you clarify which concrete user-facing features this PR adds over the current iframe/native approach? That would help us better understand the value of introducing and maintaining a custom viewer.

One thing I liked about the current iframe approach is that it could point to PDFs behind authenticated URLs (Sharepoint for example would request authentication or authorization). Am I right in thinking that this new approach would make that harder or not support it ?

@JonasKenke
Copy link
Author

Hey,
Thanks for checking out the pr. So my main goal was to provide a unified look of the PDF viewer on all browser. Also the old inline PDF viewer was often hard to read and now you get a small preview and can get a pop-up like for the images. The main goal was to have a unified experience so it is similar to the image inline. For me also cosmetic wise as I like a custom PDF viewer that matches the app it self more. In addition in my tests the I frame based PDF viewer had some issues with responsive ness how it's currently implemented.

For the authenticated urls I'm not sure but will check and I'm open for improvements

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

e2e-tests Has E2E tests frontend Pertains to the frontend. size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants