Skip to content

Add browse pdf command to save pages as PDF#1910

Open
derekmeegan wants to merge 3 commits intomainfrom
derek/add_pdf_command
Open

Add browse pdf command to save pages as PDF#1910
derekmeegan wants to merge 3 commits intomainfrom
derek/add_pdf_command

Conversation

@derekmeegan
Copy link
Copy Markdown
Contributor

Summary

  • Adds browse pdf [path] command using CDP Page.printToPDF
  • Supports --landscape, --format (letter/legal/a4/a3), --scale, --no-background
  • Saves to file when path given, returns base64-encoded PDF otherwise
  • Includes CSS page size preference support

Usage

browse pdf ./page.pdf                    # save as PDF
browse pdf ./page.pdf --landscape        # landscape
browse pdf ./page.pdf --format a4        # A4 paper
browse pdf                               # base64 output

Test results

Test Local Browserbase
pdf /tmp/test.pdf 321KB PDF, 2 pages 291KB PDF, 2 pages
pdf --landscape --format a4 Saved successfully N/A (same CDP path)
pdf (base64 output) Returns base64 string N/A (same CDP path)

🤖 Generated with Claude Code

Uses CDP Page.printToPDF to generate PDFs with configurable paper
format (letter, legal, a4, a3), orientation, scale, and background
printing. Saves to file when path given, returns base64 otherwise.

Usage:
  browse pdf ./page.pdf                  # save as PDF
  browse pdf ./page.pdf --landscape      # landscape orientation
  browse pdf ./page.pdf --format a4      # A4 paper size
  browse pdf                             # returns base64

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Mar 29, 2026

🦋 Changeset detected

Latest commit: 57ab187

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 0 packages

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Copy Markdown
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.

3 issues found across 2 files

Confidence score: 4/5

  • This PR is likely safe to merge, with mostly input-validation and release-process issues rather than clear runtime breakage (top severity is 5/10).
  • In packages/cli/src/index.ts, --scale is not validated, so invalid values silently falling back in daemon mode can produce unexpected output and make misconfiguration harder to detect.
  • In packages/cli/src/index.ts, unknown --format values are coerced to letter instead of being rejected, and .changeset/add-pdf-command.md targets an ignored package so the release note/version bump will be a no-op.
  • Pay close attention to packages/cli/src/index.ts, .changeset/add-pdf-command.md - tighten CLI argument validation and confirm release metadata targets a publishable package.
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=".changeset/add-pdf-command.md">

<violation number="1" location=".changeset/add-pdf-command.md:2">
P2: This changeset references a package that is configured as ignored, so the release note/version bump will be a no-op.</violation>
</file>

<file name="packages/cli/src/index.ts">

<violation number="1" location="packages/cli/src/index.ts:1603">
P2: Unknown `--format` values are silently converted to letter instead of being rejected.</violation>

<violation number="2" location="packages/cli/src/index.ts:2819">
P2: `--scale` is not validated, so invalid values are silently coerced to the default scale in daemon mode.</violation>
</file>
Architecture diagram
sequenceDiagram
    participant User
    participant CLI as CLI (Commander)
    participant Exec as Command Executor
    participant Browser as Browser (CDP Session)
    participant FS as Local Filesystem

    User->>CLI: browse pdf [path] --format a4 --landscape
    
    CLI->>Exec: NEW: runCommand("pdf", opts)
    
    Note over Exec: Maps format (a4/letter) to<br/>paperWidth & paperHeight
    
    Exec->>Browser: NEW: Page.printToPDF (CDP)
    Note right of Browser: landscape, scale, printBackground,<br/>preferCSSPageSize: true
    
    Browser-->>Exec: { data: base64_string }
    
    alt path provided
        Exec->>FS: NEW: writeFile(path, Buffer from base64)
        Exec-->>CLI: { saved: path }
    else no path
        Exec-->>CLI: { base64: string }
    end
    
    CLI-->>User: Output JSON or status message
Loading

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

@@ -0,0 +1,5 @@
---
"@browserbasehq/browse-cli": minor
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot Mar 29, 2026

Choose a reason for hiding this comment

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

P2: This changeset references a package that is configured as ignored, so the release note/version bump will be a no-op.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At .changeset/add-pdf-command.md, line 2:

<comment>This changeset references a package that is configured as ignored, so the release note/version bump will be a no-op.</comment>

<file context>
@@ -0,0 +1,5 @@
+---
+"@browserbasehq/browse-cli": minor
+---
+
</file context>
Fix with Cubic

Reject invalid scale values (must be 0.1-2) and unknown format values
(must be letter, legal, a4, or a3) with clear error messages instead
of silently falling back.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown
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.

1 issue found across 1 file (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="packages/cli/src/index.ts">

<violation number="1" location="packages/cli/src/index.ts:2814">
P2: Scale validation is too permissive because `parseFloat` accepts invalid trailing characters (e.g. `1abc`). Use strict numeric parsing so malformed `--scale` values are rejected.</violation>
</file>

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

.action(async (filePath: string | undefined, cmdOpts) => {
const opts = program.opts<GlobalOpts>();
try {
const scale = parseFloat(cmdOpts.scale);
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot Mar 29, 2026

Choose a reason for hiding this comment

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

P2: Scale validation is too permissive because parseFloat accepts invalid trailing characters (e.g. 1abc). Use strict numeric parsing so malformed --scale values are rejected.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At packages/cli/src/index.ts, line 2814:

<comment>Scale validation is too permissive because `parseFloat` accepts invalid trailing characters (e.g. `1abc`). Use strict numeric parsing so malformed `--scale` values are rejected.</comment>

<file context>
@@ -2811,12 +2811,24 @@ program
   .action(async (filePath: string | undefined, cmdOpts) => {
     const opts = program.opts<GlobalOpts>();
     try {
+      const scale = parseFloat(cmdOpts.scale);
+      if (isNaN(scale) || scale < 0.1 || scale > 2) {
+        console.error("Error: --scale must be a number between 0.1 and 2");
</file context>
Fix with Cubic

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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.

1 participant