Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

added function to stream pdf file

paperboardofficialef825a2b69bd
diff --git a/browser_use/controller/service.py b/browser_use/controller/service.py
index bc5a220ee..126c28f0f 100644
--- a/browser_use/controller/service.py
+++ b/browser_use/controller/service.py
@@ -17,12 +17,16 @@ from browser_use.controller.views import (
InputTextAction,
NoParamsAction,
OpenTabAction,
+ ReadPdfAction,
ScrollAction,
SearchGoogleAction,
SendKeysAction,
SwitchTabAction,
)
from browser_use.utils import time_execution_async, time_execution_sync
+from io import BytesIO
+from PyPDF2 import PdfReader
+import requests
logger = logging.getLogger(__name__)
from langchain_core.language_models.chat_models import BaseChatModel
@@ -432,6 +436,37 @@ class Controller:
msg = f'Selection failed: {str(e)}'
logger.error(msg)
return ActionResult(error=msg, include_in_memory=True)
+
+ @self.registry.action(
+ 'Read the contents of a PDF file from a given URL. This action streams the PDF file and extracts its text content. Use this when you need to analyze or extract information from PDF documents available online.',
+ param_model=ReadPdfAction
+ )
+ async def read_pdf_content(params: ReadPdfAction) -> ActionResult:
+ try:
+ # Stream the PDF file
+ response = requests.get(params.url, stream=True)
+ response.raise_for_status()
+
+ # Read the PDF content
+ pdf_content = BytesIO(response.content)
+ pdf_reader = PdfReader(pdf_content)
+
+ text_content = ""
+ for page in pdf_reader.pages:
+ text_content += page.extract_text() + "\n"
+
+ msg = f"📄 Successfully read PDF from {params.url}. Content length: {len(text_content)} characters."
+ logger.info(msg)
+
+ return ActionResult(
+ extracted_content=text_content,
+ include_in_memory=True
+ )
+ except Exception as e:
+ error_msg = f"Failed to read PDF from {params.url}: {str(e)}"
+ logger.error(error_msg)
+ return ActionResult(error=error_msg, include_in_memory=True)
+
def action(self, description: str, **kwargs):
"""Decorator for registering custom actions
diff --git a/browser_use/controller/views.py b/browser_use/controller/views.py
index d3cb11caf..a59b7d636 100644
--- a/browser_use/controller/views.py
+++ b/browser_use/controller/views.py
@@ -42,6 +42,8 @@ class ScrollAction(BaseModel):
class SendKeysAction(BaseModel):
keys: str
+class ReadPdfAction(BaseModel):
+ url: str
class NoParamsAction(BaseModel):
"""
diff --git a/examples/features/read_pdf_file.py b/examples/features/read_pdf_file.py
new file mode 100644
index 000000000..9b9bf425b
--- /dev/null
+++ b/examples/features/read_pdf_file.py
@@ -0,0 +1,39 @@
+import asyncio
+import os
+
+from dotenv import load_dotenv
+from langchain_google_genai import ChatGoogleGenerativeAI
+from pydantic import SecretStr
+
+from browser_use import Agent
+from browser_use.browser.browser import Browser, BrowserConfig
+
+load_dotenv()
+api_key = os.getenv('GEMINI_API_KEY')
+if not api_key:
+ raise ValueError('GEMINI_API_KEY is not set')
+
+llm = ChatGoogleGenerativeAI(model='gemini-2.0-flash-exp', api_key=SecretStr(api_key))
+
+browser = Browser(
+ config=BrowserConfig(
+ # NOTE: you need to close your chrome browser - so that this can open your browser in debug mode
+ chrome_instance_path='C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe',
+ )
+)
+
+async def run_search():
+ agent = Agent(
+ task=(
+ 'search for "lorem ipsum pdf file" on google and stream the first pdf file and return its first paragraph'
+ ),
+ llm=llm,
+ max_actions_per_step=4,
+ browser=browser,
+ )
+
+ await agent.run(max_steps=25)
+
+
+if __name__ == '__main__':
+ asyncio.run(run_search())