Repository navigation
Fix CDP Page.printToPDF support - #7044
Conversation
|
Ready to review this PR? Stage has broken it down into 3 individual chapters for you:
Chapters generated by Stage for commit 06a9a44 on Jul 1, 2026 8:25pm UTC. |
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
📝 WalkthroughWalkthroughThis PR adds native handling of CDP Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant CdpWsProxy
participant webContents
Client->>CdpWsProxy: Page.printToPDF (transferMode)
CdpWsProxy->>webContents: printToPDF(buildPrintToPdfOptions)
webContents-->>CdpWsProxy: PDF Buffer
alt transferMode ReturnAsStream
CdpWsProxy->>CdpWsProxy: store buffer in pdfStreams
CdpWsProxy-->>Client: stream handle (data:'', stream)
Client->>CdpWsProxy: IO.read(handle, offset, size)
CdpWsProxy-->>Client: base64 chunk, eof
Client->>CdpWsProxy: IO.close(handle)
CdpWsProxy->>CdpWsProxy: delete stream from pdfStreams
else direct response
CdpWsProxy-->>Client: base64 data
end
Related issues: None provided. Related PRs: None provided. Suggested labels: enhancement, testing, cdp-proxy Suggested reviewers: None provided. 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 4
🧹 Nitpick comments (1)
src/main/browser/cdp-ws-proxy.test.ts (1)
386-400: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winAssert that
IO.closeactually invalidates the stream.A no-op close would still pass this test. Add a read-after-close expectation for an error response to lock in cleanup behavior.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 3e2ca505-6435-46f8-adbc-b057715c2308
📒 Files selected for processing (2)
src/main/browser/cdp-ws-proxy.test.tssrc/main/browser/cdp-ws-proxy.ts
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/main/browser/cdp-ws-proxy.test.ts (1)
416-443: 🩺 Stability & Availability | 🔵 Trivial | 💤 Low valueFixed sleep for disconnect-cleanup timing.
The test relies on a hardcoded
setTimeout(resolve, 10)afterclient.close()to let disconnect cleanup run before asserting the stream is invalidated. On a loaded CI runner this could theoretically race if cleanup takes longer than 10ms, causing an intermittent false failure/pass.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 6457539c-6d84-4f7c-ba42-90b2c1295574
📒 Files selected for processing (2)
src/main/browser/cdp-ws-proxy.test.tssrc/main/browser/cdp-ws-proxy.ts
🚧 Files skipped from review as they are similar to previous changes (1)
- src/main/browser/cdp-ws-proxy.ts
…dp-print-to-pdf Move the CDP Page.printToPDF option translation and the ReturnAsStream handle store out of cdp-ws-proxy into a focused cdp-print-to-pdf module, mirroring the existing cdp-screenshot sibling. Keeps the over-budget proxy from growing further and gives the PDF logic direct unit coverage. No behavior change: all existing proxy tests pass unmodified. Co-authored-by: Orca <[email protected]>
A ReturnAsStream print that resolved after the requesting client disconnected (or was replaced) registered its buffer in pdfStreams after the disconnect's clear() already ran, orphaning a potentially large PDF until the 5-minute TTL. Re-check isActiveClient after the await, mirroring navigateWithLifecycle/reloadWithLifecycle. Co-authored-by: Orca <[email protected]>
Maintainer takeover + verificationAdopted and finished this PR (thanks @AvichalDwivedi2205). Summary of what was validated and changed: Security screen (CLEAN). This touches the CDP proxy layer, so I checked it against the PR #5035 auth hardening. No CDP auth is weakened, no new listening surface (server still binds Validity (CONFIRMED). Verified end-to-end. Drove the real Polish on top of the original: extracted the mapping + stream store into a focused Full before/after evidence and trade-offs are in the updated PR description. Not merging — leaving that to a human reviewer. |
|
Merged — this passed our review bar. @AvichalDwivedi2205 thanks for the contribution! We verified it end-to-end (real websocket client, byte-valid PDFs in both direct and ReturnAsStream modes), confirmed it doesn't weaken the CDP proxy auth, extracted the PDF mapping into a focused module, and fixed a post-disconnect buffer-retention race before merging. |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/main/browser/cdp-print-to-pdf.ts (1)
97-177: 🚀 Performance & Scalability | 🔵 TrivialConsider a cap on concurrent buffered PDF streams.
create()has no upper bound on the number of in-flight buffers; a client that issues manyReturnAsStreamprints without reading/closing them can accumulate large PDFs in memory for up to the 5-minute TTL. Given this proxy serves a single trusted local client, this is a narrow risk, but worth a note for future hardening (e.g., a max total buffered bytes/streams check increate()).
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 2625ff40-f40c-4abd-8812-44501fd43636
📒 Files selected for processing (4)
src/main/browser/cdp-print-to-pdf.test.tssrc/main/browser/cdp-print-to-pdf.tssrc/main/browser/cdp-ws-proxy.test.tssrc/main/browser/cdp-ws-proxy.ts
|
Thank you, @AvichalDwivedi2205. Solid work wiring up Page.printToPDF for external CDP clients so Playwright's page.pdf() just works against Orca, and I like that you modeled it on the existing screenshot handler. Really appreciate the contribution — merged! |
|
Thank you, Avichal! 🙏 printToPDF over CDP is genuinely useful for anyone driving the built-in browser with Playwright, and you mirrored the existing screenshot handler so cleanly it slotted right in. Great contribution — thanks for making it. 🐋 |
* Fix CDP printToPDF proxy * Harden CDP PDF stream handling * Fix PDF margin units * refactor(cdp): extract printToPDF param mapping + stream store into cdp-print-to-pdf Move the CDP Page.printToPDF option translation and the ReturnAsStream handle store out of cdp-ws-proxy into a focused cdp-print-to-pdf module, mirroring the existing cdp-screenshot sibling. Keeps the over-budget proxy from growing further and gives the PDF logic direct unit coverage. No behavior change: all existing proxy tests pass unmodified. Co-authored-by: Orca <[email protected]> * fix(cdp): drop print result if client disconnected during printToPDF A ReturnAsStream print that resolved after the requesting client disconnected (or was replaced) registered its buffer in pdfStreams after the disconnect's clear() already ran, orphaning a potentially large PDF until the 5-minute TTL. Re-check isActiveClient after the await, mirroring navigateWithLifecycle/reloadWithLifecycle. Co-authored-by: Orca <[email protected]> --------- Co-authored-by: Neil <[email protected]> Co-authored-by: Orca <[email protected]>
Description
Fixes #7032.
Adds
Page.printToPDFsupport to Orca's per-tab CDP websocket proxy (CdpWsProxy). Before this change, a CDP client (Playwright/Puppeteer) callingpage.pdf()against an Orca browser tab gotProtocol error (Page.printToPDF): 'Page.printToPDF' wasn't found, because the proxy forwarded the command towebContents.debugger.sendCommand, and Electron webview guests do not implementPage.printToPDFover the debugger.The proxy now handles
Page.printToPDFnatively via Electron'swebContents.printToPDF, exactly mirroring how the existingPage.captureScreenshothandler already bypasses the debugger for the same class of webview-guest limitation. It supports both response shapes:{ data: <base64> }) whentransferModeis omitted.{ data: "", stream: <handle> }+IO.read/IO.close) whentransferMode: "ReturnAsStream"— the path Playwright'spage.pdf()uses.CDP params are translated faithfully to Electron options:
landscape,printBackground,scale,paperWidth/paperHeight→pageSize,marginTop/Bottom/Left/Right→margins(omitted sides default to CDP's 1cm),pageRanges, header/footer templates, and the tagged-PDF / outline flags. Only well-formed values are forwarded, so a malformed param can never smuggleNaN/Infinityinto Electron.This does not duplicate the existing
orca pdfCLI command — that path (agent-browser-bridge.pdf()) already bypasses the proxy with a nativeprintToPDFcall and serves Orca's own tooling. This PR fills the distinct gap for external CDP clients driving the tab over the websocket endpoint.Takeover note
This PR was adopted and finished by a maintainer (original author: @AvichalDwivedi2205). Changes on top of the original submission:
cdp-print-to-pdf.tsmodule (buildPrintToPdfOptions+CdpPdfStreamStore), mirroring the siblingcdp-screenshot.ts. This keeps the already-over-budget proxy from growing and gives the PDF logic direct unit coverage.ReturnAsStreamprint that resolved after the requesting client disconnected would register its (potentially large) buffer in the stream store after the disconnect's cleanup already ran, orphaning it until the 5-minute TTL.handlePrintToPdfnow re-checksisActiveClientafter theawait, matchingnavigateWithLifecycle/reloadWithLifecycle.Evidence
Before (origin/main) — the gap is real. A unit repro sending
Page.printToPDFagainst the proxy shows the command is forwarded to the debugger (nativeprintToPDFnever called) and the client receives the CDP error:The identical repro inverts on this branch —
Page.printToPDFis no longer forwarded to the debugger:After — real PDF bytes through the real CDP endpoint. An Electron harness drives
CdpWsProxyagainst a real offscreenwebContentsover a real websocket:Both the direct and streamed paths produce byte-identical, valid
PDF document, version 1.4, 1 pagesoutput (file(1)confirmed).IO.closefrees the handle; a read after close is rejected withInvalid stream handle.Tests / checks:
pnpm vitest runon both suites → 46 passed (16 new module tests + 30 proxy tests).pnpm run typecheckclean.oxlintclean on all touched files. The disconnect-mid-print regression test was confirmed to fail without theisActiveClientguard (expected "create" to not be called at all, but actually been called 1 times) and pass with it.ELI5
Orca has a built-in browser. Robots (Playwright/Puppeteer) can drive that browser through a special socket. When a robot asked "print this page to a PDF," Orca said "I don't know that command." Now Orca hears that request, uses Electron's real printer to make the PDF, and hands the bytes back — either all at once or in small pieces, whichever the robot asked for. It also cleans up the PDF it made if the robot walks away, so memory doesn't pile up.
Trade-offs
printToPDFreturns the whole document as one buffer, so aReturnAsStreamhandle holds the entire PDF (potentially tens of MB for a long page) until the client sendsIO.closeor the 5-minute TTL evicts it. This is inherent to the Electron API — there is no incremental stream to forward — and is bounded by the TTL plus eager cleanup on client disconnect. Auto-freeing at EOF was deliberately avoided because it would violate the CDP IO-stream contract (streams persist untilIO.close; a client may legitimately re-read viaoffset).IO.read/IO.closestreams (e.g. Tracing), continue to forward through the existing debugger path unchanged.