|
| 1 | +# Pull Request: Add Missing Direct API Tools |
| 2 | + |
| 3 | +## Summary |
| 4 | +This PR adds 8 new direct API methods that were missing from the Python client, bringing it to feature parity with the Nutrient DWS API capabilities. |
| 5 | + |
| 6 | +## New Tools Added |
| 7 | + |
| 8 | +### 1. Create Redactions (3 methods for different strategies) |
| 9 | +- `create_redactions_preset()` - Use built-in patterns for common sensitive data |
| 10 | + - Presets: social-security-number, credit-card-number, email, phone-number, date, currency |
| 11 | +- `create_redactions_regex()` - Custom regex patterns for flexible redaction |
| 12 | +- `create_redactions_text()` - Exact text matches with case sensitivity options |
| 13 | + |
| 14 | +### 2. PDF Optimization |
| 15 | +- `optimize_pdf()` - Reduce file size with multiple optimization options: |
| 16 | + - Grayscale conversion (text, graphics, images) |
| 17 | + - Image quality reduction (1-100) |
| 18 | + - Linearization for web viewing |
| 19 | + - Option to disable images entirely |
| 20 | + |
| 21 | +### 3. Security Features |
| 22 | +- `password_protect_pdf()` - Add password protection and permissions |
| 23 | + - User password (for opening) |
| 24 | + - Owner password (for permissions) |
| 25 | + - Granular permissions: print, modification, extract, annotations, fill, etc. |
| 26 | +- `set_pdf_metadata()` - Update document properties |
| 27 | + - Title, author, subject, keywords, creator, producer |
| 28 | + |
| 29 | +### 4. Annotation Import |
| 30 | +- `apply_instant_json()` - Import Nutrient Instant JSON annotations |
| 31 | + - Supports file, bytes, or URL input |
| 32 | +- `apply_xfdf()` - Import standard XFDF annotations |
| 33 | + - Supports file, bytes, or URL input |
| 34 | + |
| 35 | +## Implementation Details |
| 36 | + |
| 37 | +### Code Quality |
| 38 | +- ✅ All methods have comprehensive docstrings with examples |
| 39 | +- ✅ Type hints are complete and pass mypy checks |
| 40 | +- ✅ Code follows project conventions and passes ruff linting |
| 41 | +- ✅ All existing unit tests continue to pass (167 tests) |
| 42 | + |
| 43 | +### Architecture |
| 44 | +- Methods that require file uploads (apply_instant_json, apply_xfdf) handle them directly |
| 45 | +- Methods that use output options (password_protect_pdf, set_pdf_metadata) use the Builder API |
| 46 | +- All methods maintain consistency with existing Direct API patterns |
| 47 | + |
| 48 | +### Testing |
| 49 | +- Comprehensive integration tests added for all new methods (28 new tests) |
| 50 | +- Tests cover success cases, error cases, and edge cases |
| 51 | +- Tests are properly skipped when API key is not configured |
| 52 | + |
| 53 | +## Files Changed |
| 54 | +- `src/nutrient_dws/api/direct.py` - Added 8 new methods (565 lines) |
| 55 | +- `tests/integration/test_new_tools_integration.py` - New test file (481 lines) |
| 56 | + |
| 57 | +## Usage Examples |
| 58 | + |
| 59 | +### Redact Sensitive Data |
| 60 | +```python |
| 61 | +# Redact social security numbers |
| 62 | +client.create_redactions_preset( |
| 63 | + "document.pdf", |
| 64 | + preset="social-security-number", |
| 65 | + output_path="redacted.pdf" |
| 66 | +) |
| 67 | + |
| 68 | +# Custom regex redaction |
| 69 | +client.create_redactions_regex( |
| 70 | + "document.pdf", |
| 71 | + pattern=r"\b\d{3}-\d{2}-\d{4}\b", |
| 72 | + appearance_fill_color="#000000" |
| 73 | +) |
| 74 | + |
| 75 | +# Then apply the redactions |
| 76 | +client.apply_redactions("redacted.pdf", output_path="final.pdf") |
| 77 | +``` |
| 78 | + |
| 79 | +### Optimize PDF Size |
| 80 | +```python |
| 81 | +# Aggressive optimization |
| 82 | +client.optimize_pdf( |
| 83 | + "large_document.pdf", |
| 84 | + grayscale_images=True, |
| 85 | + reduce_image_quality=50, |
| 86 | + linearize=True, |
| 87 | + output_path="optimized.pdf" |
| 88 | +) |
| 89 | +``` |
| 90 | + |
| 91 | +### Secure PDFs |
| 92 | +```python |
| 93 | +# Password protect with restricted permissions |
| 94 | +client.password_protect_pdf( |
| 95 | + "sensitive.pdf", |
| 96 | + user_password="view123", |
| 97 | + owner_password="admin456", |
| 98 | + permissions={ |
| 99 | + "print": False, |
| 100 | + "modification": False, |
| 101 | + "extract": True |
| 102 | + } |
| 103 | +) |
| 104 | +``` |
| 105 | + |
| 106 | +## Breaking Changes |
| 107 | +None - all changes are additive. |
| 108 | + |
| 109 | +## Migration Guide |
| 110 | +No migration needed - existing code continues to work as before. |
| 111 | + |
| 112 | +## Checklist |
| 113 | +- [x] Code follows project style guidelines |
| 114 | +- [x] Self-review of code completed |
| 115 | +- [x] Comments added for complex code sections |
| 116 | +- [x] Documentation/docstrings updated |
| 117 | +- [x] No warnings generated |
| 118 | +- [x] Tests added for new functionality |
| 119 | +- [x] All tests pass locally |
| 120 | +- [ ] Integration tests pass with live API (requires API key) |
| 121 | + |
| 122 | +## Next Steps |
| 123 | +After merging: |
| 124 | +1. Update README with examples of new methods |
| 125 | +2. Consider adding more tools: HTML to PDF, digital signatures, etc. |
| 126 | +3. Create a cookbook/examples directory with common use cases |
0 commit comments