Skip to content

feat(whiteboard): add PNG and PDF export functionality (#1915) - #1939

Open
Shriraj888 wants to merge 2 commits into
durdana3105:mainfrom
Shriraj888:feat/export-whiteboard-canvas-as-PNG/PDF
Open

feat(whiteboard): add PNG and PDF export functionality (#1915)#1939
Shriraj888 wants to merge 2 commits into
durdana3105:mainfrom
Shriraj888:feat/export-whiteboard-canvas-as-PNG/PDF

Conversation

@Shriraj888

@Shriraj888 Shriraj888 commented Aug 6, 2026

Copy link
Copy Markdown

📌 Description

During study sessions, users create detailed notes and diagrams on the Whiteboard. Previously, there was no way to save or export the canvas state after a session ended.

This PR implements Export to PNG and Export to PDF buttons directly on the Whiteboard toolbar, allowing users to save their whiteboard diagrams locally in their preferred format.

✨ Key Changes

  • Export Utilities (exportUtils.ts):
    • getExportCanvasDataURL(): Synthesizes canvas drawing onto an offscreen canvas pre-filled with the dark slate background (#020617), preserving dark slate background styling and eraser stroke visibility.
    • exportCanvasToPNG(): Encodes canvas to PNG and triggers browser file download.
    • exportCanvasToPDF(): Uses jsPDF to generate a custom-dimensioned PDF matching the canvas aspect ratio and triggers browser file download.
  • Toolbar Component (Canvas.tsx):
    • Added Export PNG and Export PDF buttons with icons (Download, FileText) to the top toolbar.
    • Added user feedback toasts using sonner (toast.success & toast.error).
  • Dependencies (package.json):
    • Added jspdf dependency for client-side PDF document generation.
  • Unit Tests (exportUtils.test.ts):
    • Added unit test suite for background fill synthesis, link trigger downloads, PDF creation, and invalid canvas dimension handling.

🧪 How to Test

  1. Join/open a study room with a Whiteboard (/room/:id).
  2. Draw strokes/diagrams using different colors and eraser tools.
  3. Click Export PNG and verify a valid .png file downloads matching the canvas drawing with the dark background.
  4. Click Export PDF and verify a valid .pdf document downloads containing the canvas image.

📸 Automated Test Status

  • npx vitest run src/components/Whiteboard/ (10/10 tests passed)
  • npm run typecheck (Passed with zero errors)

Closes #1915

Summary by CodeRabbit

  • New Features

    • Added options to export whiteboard drawings as PNG images or PDF files.
    • Exported files use automatic filenames and PDF orientation based on canvas dimensions.
    • Added responsive toolbar behavior and success/error notifications.
    • Added background handling to produce clear, consistent exported images.
  • Bug Fixes

    • Added validation to prevent exports from empty or unavailable canvases.
    • Improved error handling when an export cannot be completed.

Copilot AI lite review requested due to automatic review settings August 6, 2026 05:49
@vercel

vercel Bot commented Aug 6, 2026

Copy link
Copy Markdown

@Shriraj888 is attempting to deploy a commit to the durdana3105's projects Team on Vercel.

A member of the Team first needs to authorize it.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@coderabbitai

coderabbitai Bot commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 143e4fd0-05f3-4489-8c00-41c1e7f6aaff

📥 Commits

Reviewing files that changed from the base of the PR and between 3ff9d29 and 9bf56f1.

📒 Files selected for processing (3)
  • src/components/Whiteboard/exportUtils.test.ts
  • src/components/Whiteboard/exportUtils.ts
  • src/hooks/useSessions.ts
🚧 Files skipped from review as they are similar to previous changes (3)
  • src/components/Whiteboard/exportUtils.test.ts
  • src/components/Whiteboard/exportUtils.ts
  • src/hooks/useSessions.ts

📝 Walkthrough

Walkthrough

Changes

Whiteboard export

Layer / File(s) Summary
Canvas export pipeline
package.json, src/components/Whiteboard/exportUtils.ts, src/components/Whiteboard/exportUtils.test.ts
Added PNG rendering and download utilities. Added PDF generation with automatic orientation and zero-size validation. Added Vitest coverage and the jspdf dependency.
Whiteboard export toolbar
src/components/Whiteboard/Canvas.tsx
Added PNG and PDF toolbar actions with canvas checks, generated filenames, notifications, error handling, icons, and responsive layout styling.

Session callback typing

Layer / File(s) Summary
Realtime callback annotations
src/hooks/useSessions.ts
Added explicit types for broadcast payloads and subscription status callbacks without changing runtime behavior.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant WhiteboardToolbar
  participant CanvasExportHandlers
  participant exportUtils
  participant Browser
  participant jsPDF
  WhiteboardToolbar->>CanvasExportHandlers: Select PNG or PDF export
  CanvasExportHandlers->>exportUtils: Export canvas with generated filename
  exportUtils->>Browser: Trigger PNG download
  exportUtils->>jsPDF: Add canvas image and save PDF
Loading
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Out of Scope Changes check ⚠️ Warning The PR also removes the Settings lazy import and changes session broadcast typing, which are unrelated to issue [#1915]. Remove the unrelated Settings import change and session typing changes, or link them to separate issues.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly identifies the main change: PNG and PDF export functionality for the Whiteboard.
Linked Issues check ✅ Passed The PR adds Whiteboard PNG and PDF toolbar actions, browser downloads, PDF generation, and tests required by issue [#1915].
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (2)
src/hooks/useSessions.ts (1)

183-192: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Use concrete realtime payload types.

payload: any disables type checking for both broadcast messages. Define payload types for the typing and activity events. Let .subscribe infer status or use the SDK status type instead of widening it to string. TypeScript documents that any allows unchecked property access, and Supabase provides typed callback contracts for these APIs. (typescriptlang.org)

-        .on("broadcast", { event: "typing" }, ({ payload }: { payload: any }) => {
+        .on("broadcast", { event: "typing" }, ({ payload }: { payload: TypingPayload }) => {
...
-        .on("broadcast", { event: "activity" }, ({ payload }: { payload: any }) => {
+        .on("broadcast", { event: "activity" }, ({ payload }: { payload: ActivityPayload }) => {
...
-        .subscribe(async (status: string) => {
+        .subscribe(async (status) => {

Verify these callback types against the pinned @supabase/supabase-js 2.102.1 declarations before adding SDK-specific imports.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/hooks/useSessions.ts` around lines 183 - 192, Replace the any payload
annotations in the typing and activity handlers within useSessions with concrete
payload types matching each event’s fields, preserving their existing behavior.
Remove the explicit string annotation from the subscribe callback so the
Supabase SDK infers its status type, or use the compatible SDK status type after
verifying the pinned `@supabase/supabase-js` declarations.
src/components/Whiteboard/exportUtils.test.ts (1)

31-48: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Mock and assert the offscreen canvas.

getExportCanvasDataURL creates a separate canvas with document.createElement("canvas"). This setup only configures dummyCanvas, so the test does not prove that the offscreen path fills the background or draws the source canvas. Return a mocked offscreen canvas and assert fillRect, drawImage, and its toDataURL call.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/components/Whiteboard/exportUtils.test.ts` around lines 31 - 48, Update
the getExportCanvasDataURL test setup to mock the canvas returned by
document.createElement("canvas"), including its 2D context, fillRect, drawImage,
and toDataURL methods. In the test, assert the offscreen context fills the
background and draws dummyCanvas, and verify the offscreen canvas toDataURL call
returns the expected data URL.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/components/Whiteboard/exportUtils.ts`:
- Around line 7-13: Update getExportCanvasDataURL to validate that the source
canvas has non-zero dimensions before creating the offscreen canvas, rejecting
invalid dimensions consistently for PNG and PDF export callers. Add a test
covering zero-width or zero-height canvases and the resulting PNG export
failure.

---

Nitpick comments:
In `@src/components/Whiteboard/exportUtils.test.ts`:
- Around line 31-48: Update the getExportCanvasDataURL test setup to mock the
canvas returned by document.createElement("canvas"), including its 2D context,
fillRect, drawImage, and toDataURL methods. In the test, assert the offscreen
context fills the background and draws dummyCanvas, and verify the offscreen
canvas toDataURL call returns the expected data URL.

In `@src/hooks/useSessions.ts`:
- Around line 183-192: Replace the any payload annotations in the typing and
activity handlers within useSessions with concrete payload types matching each
event’s fields, preserving their existing behavior. Remove the explicit string
annotation from the subscribe callback so the Supabase SDK infers its status
type, or use the compatible SDK status type after verifying the pinned
`@supabase/supabase-js` declarations.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 6e0d67a6-a800-46ba-8e43-1458989f65f4

📥 Commits

Reviewing files that changed from the base of the PR and between 8c4d292 and 3ff9d29.

⛔ Files ignored due to path filters (1)
  • package-lock.json is excluded by !**/package-lock.json
📒 Files selected for processing (6)
  • package.json
  • src/App.tsx
  • src/components/Whiteboard/Canvas.tsx
  • src/components/Whiteboard/exportUtils.test.ts
  • src/components/Whiteboard/exportUtils.ts
  • src/hooks/useSessions.ts
💤 Files with no reviewable changes (1)
  • src/App.tsx

Comment thread src/components/Whiteboard/exportUtils.ts
@Shriraj888

Shriraj888 commented Aug 6, 2026

Copy link
Copy Markdown
Author

@durdana3105 Could you please review my PR when you get a chance? I'd really appreciate your feedback.

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.

[Feature] Export Whiteboard canvas as PNG/PDF

2 participants