From e9b8cfb9f7568e979dc45fdc1c19f416eef7a220 Mon Sep 17 00:00:00 2001 From: mertunsall Date: Sun, 29 Jun 2025 21:51:56 +0200 Subject: [PATCH] remove the default save_pdf option --- browser_use/controller/service.py | 16 -------- examples/custom-functions/save_pdf.py | 55 +++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 16 deletions(-) create mode 100644 examples/custom-functions/save_pdf.py diff --git a/browser_use/controller/service.py b/browser_use/controller/service.py index f4c389dd2..aa68a2ad8 100644 --- a/browser_use/controller/service.py +++ b/browser_use/controller/service.py @@ -2,7 +2,6 @@ import asyncio import enum import json import logging -import re from collections.abc import Awaitable, Callable from typing import Any, Generic, TypeVar, cast @@ -245,21 +244,6 @@ class Controller(Generic[Context]): long_term_memory=f"Input '{params.text}' into element {params.index}.", ) - # Save PDF - @self.registry.action('Save the current page as a PDF file') - async def save_pdf(page: Page): - short_url = re.sub(r'^https?://(?:www\.)?|/$', '', page.url) - slug = re.sub(r'[^a-zA-Z0-9]+', '-', short_url).strip('-').lower() - sanitized_filename = f'{slug}.pdf' - - await page.emulate_media(media='screen') - await page.pdf(path=sanitized_filename, format='A4', print_background=False) - msg = f'Saving page with URL {page.url} as PDF to ./{sanitized_filename}' - logger.info(msg) - return ActionResult( - extracted_content=msg, include_in_memory=True, long_term_memory=f'Saved PDF to {sanitized_filename}' - ) - # Tab Management Actions @self.registry.action('Switch tab', param_model=SwitchTabAction) async def switch_tab(params: SwitchTabAction, browser_session: BrowserSession): diff --git a/examples/custom-functions/save_pdf.py b/examples/custom-functions/save_pdf.py new file mode 100644 index 000000000..4a28d69bc --- /dev/null +++ b/examples/custom-functions/save_pdf.py @@ -0,0 +1,55 @@ +import asyncio +import os +import re +import sys +from pathlib import Path + +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) + +from dotenv import load_dotenv + +load_dotenv() + +from browser_use import ActionResult, Agent, Controller +from browser_use.browser.types import Page +from browser_use.llm import ChatOpenAI + +# Initialize controller +controller = Controller() + +download_path = Path.cwd() / 'downloads' + + +# Save PDF - exact copy from original controller function +@controller.registry.action('Save the current page as a PDF file') +async def save_pdf(page: Page): + short_url = re.sub(r'^https?://(?:www\.)?|/$', '', page.url) + slug = re.sub(r'[^a-zA-Z0-9]+', '-', short_url).strip('-').lower() + sanitized_filename = f'{slug}.pdf' + + await page.emulate_media(media='screen') + await page.pdf(path=download_path / sanitized_filename, format='A4', print_background=False) + msg = f'Saving page with URL {page.url} as PDF to {download_path / sanitized_filename}' + return ActionResult(extracted_content=msg, include_in_memory=True, long_term_memory=f'Saved PDF to {sanitized_filename}') + + +async def main(): + """ + Example task: Navigate to browser-use.com and save the page as a PDF + """ + task = """ + Go to https://browser-use.com/ and save the page as a PDF file. + """ + + # Initialize the language model + model = ChatOpenAI(model='gpt-4.1-mini') + + # Create and run the agent + agent = Agent(task=task, llm=model, controller=controller) + + result = await agent.run() + print(f'🎯 Task completed: {result}') + + +if __name__ == '__main__': + asyncio.run(main())