-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add browse pdf command to save pages as PDF
#1910
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
derekmeegan
wants to merge
4
commits into
main
Choose a base branch
from
derek/add_pdf_command
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1b444a5
Add `browse pdf` command to save pages as PDF
derekmeegan 716a487
fix: validate --scale and --format for pdf command
derekmeegan 57ab187
fix: add page null guard in pdf command
derekmeegan e8457cb
Downgrade changeset to patch per review feedback
derekmeegan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@browserbasehq/browse-cli": patch | ||
| --- | ||
|
|
||
| Add `browse pdf [path]` command to save the current page as a PDF. Supports `--landscape`, `--format` (letter, legal, a4, a3), `--scale`, and `--no-background` options. Returns base64-encoded PDF when no path is given. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1582,6 +1582,52 @@ async function executeCommand( | |
| } | ||
| } | ||
|
|
||
| case "pdf": { | ||
| if (!page) { | ||
| return { error: "No page open. Navigate to a URL first." }; | ||
| } | ||
| const [opts] = args as [ | ||
| { | ||
| path?: string; | ||
| landscape?: boolean; | ||
| printBackground?: boolean; | ||
| scale?: number; | ||
| format?: string; | ||
| }?, | ||
| ]; | ||
| const cdpSession = page.mainFrame().session; | ||
| const paperSizes: Record<string, { width: number; height: number }> = { | ||
| letter: { width: 8.5, height: 11 }, | ||
| legal: { width: 8.5, height: 14 }, | ||
| a4: { width: 8.27, height: 11.69 }, | ||
| a3: { width: 11.69, height: 16.54 }, | ||
| }; | ||
| const paper = | ||
| paperSizes[(opts?.format ?? "letter").toLowerCase()] ?? | ||
| paperSizes.letter; | ||
| const result = await cdpSession.send<{ data: string }>( | ||
| "Page.printToPDF", | ||
| { | ||
| landscape: opts?.landscape ?? false, | ||
| printBackground: opts?.printBackground ?? true, | ||
| scale: opts?.scale ?? 1, | ||
| paperWidth: paper.width, | ||
| paperHeight: paper.height, | ||
| marginTop: 0.4, | ||
| marginBottom: 0.4, | ||
| marginLeft: 0.4, | ||
| marginRight: 0.4, | ||
| preferCSSPageSize: true, | ||
| }, | ||
| ); | ||
| if (opts?.path) { | ||
| await fs.writeFile(opts.path, Buffer.from(result.data, "base64")); | ||
| return { saved: opts.path }; | ||
| } | ||
| return { base64: result.data }; | ||
| } | ||
|
|
||
| // Daemon control | ||
| case "stop": { | ||
| process.nextTick(() => { | ||
|
|
@@ -2756,6 +2802,46 @@ networkCmd | |
| } | ||
| }); | ||
|
|
||
| // ==================== PDF ==================== | ||
|
|
||
| program | ||
| .command("pdf [path]") | ||
| .description("Save page as PDF") | ||
| .option("--landscape", "Landscape orientation") | ||
| .option("--no-background", "Omit background graphics") | ||
| .option("--scale <n>", "Scale factor (0.1-2)", "1") | ||
| .option("--format <format>", "Paper format: letter, legal, a4, a3", "letter") | ||
| .action(async (filePath: string | undefined, cmdOpts) => { | ||
| const opts = program.opts<GlobalOpts>(); | ||
| try { | ||
| const scale = parseFloat(cmdOpts.scale); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: Scale validation is too permissive because Prompt for AI agents |
||
| if (isNaN(scale) || scale < 0.1 || scale > 2) { | ||
| console.error("Error: --scale must be a number between 0.1 and 2"); | ||
| process.exit(1); | ||
| } | ||
| const validFormats = ["letter", "legal", "a4", "a3"]; | ||
| if (!validFormats.includes(cmdOpts.format.toLowerCase())) { | ||
| console.error( | ||
| `Error: --format must be one of: ${validFormats.join(", ")}`, | ||
| ); | ||
| process.exit(1); | ||
| } | ||
| const result = await runCommand("pdf", [ | ||
| { | ||
| path: filePath, | ||
| landscape: cmdOpts.landscape, | ||
| printBackground: cmdOpts.background !== false, | ||
| scale, | ||
| format: cmdOpts.format, | ||
| }, | ||
| ]); | ||
| output(result, opts.json ?? false); | ||
| } catch (e) { | ||
| console.error("Error:", e instanceof Error ? e.message : e); | ||
| process.exit(1); | ||
| } | ||
| }); | ||
|
|
||
| // ==================== RUN ==================== | ||
|
|
||
| program.parse(); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.