Skip to content

Conversation

devin-ai-integration[bot]
Copy link
Contributor

Fix: Export PDFTextWritingTool and add comprehensive tests

Summary

This PR resolves issue #3331 where PDFTextWritingTool was documented but missing from the crewai_tools package exports. The tool existed in the codebase but couldn't be imported because it wasn't properly exported in the __init__.py files.

Key Changes:

  • Added PDFTextWritingTool exports to crewai_tools/__init__.py and crewai_tools/tools/__init__.py
  • Major Implementation Update: Completely rewrote the PDF text writing logic to use modern pypdf.annotations.FreeText instead of the deprecated ContentStream API
  • Added comprehensive test suite with 9 test cases covering schema validation, functionality, error handling, and various parameter combinations
  • Updated test_search_tools.py to include PDFTextWritingTool import verification

Breaking Changes:

  • Custom font file embedding is no longer supported (parameter kept for backward compatibility but documented as unsupported)
  • Text rendering approach changed from direct content stream manipulation to PDF annotations

Review & Testing Checklist for Human

⚠️ HIGH RISK - This PR includes a complete rewrite of the core functionality. Please verify thoroughly:

  • Test with real PDF files: Create a simple PDF and verify that text is actually added at the correct positions and renders properly in different PDF viewers
  • Verify color rendering: Test all supported color formats ("0 0 0 rg", "1 0 0 rg", "0 1 0 rg", "0 0 1 rg") to ensure colors appear correctly
  • Check position coordinates: Verify that the x,y position parameters work as expected - the coordinate system may behave differently with FreeText annotations
  • Test error handling: Try edge cases like invalid PDF files, out-of-range pages, and malformed input parameters
  • Font parameter decision: Decide whether to completely remove the font_file parameter or keep it documented as unsupported for backward compatibility

Recommended Test Plan:

  1. Import PDFTextWritingTool in a fresh Python environment to verify exports work
  2. Create a multi-page PDF and add text to different pages at various positions
  3. Test with different font sizes, colors, and positions
  4. Open the generated PDFs in multiple viewers (Adobe Reader, Chrome, Firefox) to verify compatibility

Diagram

%%{ init : { "theme" : "default" }}%%
graph TD
    subgraph "Package Structure"
        A["crewai_tools/__init__.py"]:::major-edit
        B["crewai_tools/tools/__init__.py"]:::major-edit
    end
    
    subgraph "Implementation"
        C["pdf_text_writing_tool.py"]:::major-edit
        D["pypdf.annotations.FreeText<br/>(new API)"]:::context
        E["pypdf.ContentStream<br/>(deprecated, removed)"]:::context
    end
    
    subgraph "Testing"
        F["test_pdf_text_writing_tool.py<br/>(new file)"]:::major-edit
        G["test_search_tools.py"]:::minor-edit
    end
    
    A -->|exports| C
    B -->|exports| C
    C -->|uses| D
    C -.->|replaced| E
    F -->|tests| C
    G -->|imports| C
    
    subgraph Legend
        L1[Major Edit]:::major-edit
        L2[Minor Edit]:::minor-edit
        L3[Context/No Edit]:::context
    end

classDef major-edit fill:#90EE90
classDef minor-edit fill:#87CEEB
classDef context fill:#FFFFFF
Loading

Notes

  • API Migration: This PR migrates from pypdf's deprecated ContentStream API to the modern FreeText annotation approach. While this fixes compatibility with current pypdf versions, it represents a significant implementation change.
  • Font Support: The original implementation supported custom TTF font embedding, but this is not easily achievable with FreeText annotations. The parameter is kept for backward compatibility but documented as unsupported.
  • Testing Coverage: Added comprehensive unit tests, but visual verification with real PDFs is still recommended since the tests don't validate the actual PDF output appearance.

Link to Devin run: https://app.devin.ai/sessions/813c5e6fd0d94e57976c978e1e803bd2
Requested by: João (joao@crewai.com)

- Add PDFTextWritingTool to crewai_tools/__init__.py and tools/__init__.py exports
- Fix PDFTextWritingTool implementation to use current pypdf API (FreeText annotations)
- Replace outdated ContentStream API with modern pypdf.annotations.FreeText
- Add comprehensive tests for PDFTextWritingTool functionality
- Update test_search_tools.py to include PDFTextWritingTool import
- Resolves issue #3331: PDFTextWritingTool documented but missing from package

Co-Authored-By: João <joao@crewai.com>
Copy link
Contributor Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

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.

0 participants