react-native-pdf-jsi
Version:
🚀 Ultra-fast React Native PDF viewer with JSI (JavaScript Interface) integration for maximum performance. Features lazy loading, smart caching, progressive loading, and zero-bridge overhead operations. Perfect for large PDF files with 30-day persistent c
348 lines (255 loc) • 8.45 kB
Markdown
# Enhanced PDF JSI Integration
## Overview
This is a high-performance JSI (JavaScript Interface) integration for the React Native PDF module. It provides direct native-to-JavaScript communication, eliminating the React Native Bridge overhead for critical PDF operations.
## Performance Benefits
- **Zero Bridge Overhead**: Direct memory access between JavaScript and native code
- **Sub-millisecond Operations**: Critical PDF operations execute in microseconds
- **Enhanced Caching**: Intelligent multi-level caching system
- **Batch Operations**: Process multiple operations efficiently
- **Memory Optimization**: Automatic memory management and cleanup
## Architecture
### JSI Components
1. **PDFJSI.h/cpp**: Core JSI implementation with native PDF operations
2. **PDFJSIBridge.cpp**: JNI bridge between Java and C++ JSI
3. **PDFJSIManager.java**: Java manager for JSI operations
4. **EnhancedPdfJSIBridge.java**: JavaScript bridge with enhanced features
5. **PDFJSI.js**: JavaScript interface for easy integration
### Data Flow
```
JavaScript → JSI → Native PDF Operations → Direct Memory Access
```
## Installation
### 1. Native Dependencies
The JSI integration requires the following native dependencies:
```bash
# Android NDK
# CMake 3.13+
# C++17 support
```
### 2. Build Configuration
Add to your `android/build.gradle`:
```gradle
android {
externalNativeBuild {
cmake {
path "node_modules/react-native-pdf/android/src/main/cpp/CMakeLists.txt"
version "3.22.1"
}
}
}
```
### 3. Package Registration
Register the JSI package in your React Native application:
```java
// MainApplication.java
import org.wonday.pdf.PDFJSIPackage;
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new PDFJSIPackage() // Add this line
);
}
```
## Usage
### Basic JSI Operations
```javascript
import PDFJSI from 'react-native-pdf/src/PDFJSI';
// Check JSI availability
const isAvailable = await PDFJSI.checkJSIAvailability();
console.log('JSI Available:', isAvailable);
// High-performance page rendering
const result = await PDFJSI.renderPageDirect(
'pdf_123', // PDF ID
1, // Page number
2.0, // Scale factor
base64Data // Base64 PDF data
);
// Get page metrics
const metrics = await PDFJSI.getPageMetrics('pdf_123', 1);
console.log('Page metrics:', metrics);
// Preload multiple pages
const success = await PDFJSI.preloadPagesDirect('pdf_123', 1, 5);
console.log('Preload success:', success);
```
### Advanced Features
```javascript
// Cache management
const cacheMetrics = await PDFJSI.getCacheMetrics('pdf_123');
await PDFJSI.clearCacheDirect('pdf_123', 'all');
// Memory optimization
await PDFJSI.optimizeMemory('pdf_123');
// Text search
const searchResults = await PDFJSI.searchTextDirect(
'pdf_123',
'search term',
1, // Start page
10 // End page
);
// Performance monitoring
const perfMetrics = await PDFJSI.getPerformanceMetrics('pdf_123');
const jsiStats = await PDFJSI.getJSIStats();
// Render quality control
await PDFJSI.setRenderQuality('pdf_123', 3); // High quality
```
### Performance Tracking
```javascript
// Get performance history
const performanceHistory = PDFJSI.getPerformanceHistory();
console.log('Performance data:', performanceHistory);
// Clear performance history
PDFJSI.clearPerformanceHistory();
```
## API Reference
### Core Methods
#### `renderPageDirect(pdfId, pageNumber, scale, base64Data)`
Renders a PDF page directly via JSI with zero bridge overhead.
**Parameters:**
- `pdfId` (string): Unique PDF identifier
- `pageNumber` (number): Page number to render (1-based)
- `scale` (number): Scale factor for rendering
- `base64Data` (string): Base64 encoded PDF data
**Returns:** `Promise<Object>` - Render result with success status and data
#### `getPageMetrics(pdfId, pageNumber)`
Gets detailed metrics for a specific PDF page.
**Parameters:**
- `pdfId` (string): PDF identifier
- `pageNumber` (number): Page number
**Returns:** `Promise<Object>` - Page metrics including dimensions, DPI, etc.
#### `preloadPagesDirect(pdfId, startPage, endPage)`
Preloads multiple pages for faster subsequent access.
**Parameters:**
- `pdfId` (string): PDF identifier
- `startPage` (number): Starting page number
- `endPage` (number): Ending page number
**Returns:** `Promise<boolean>` - Success status
### Cache Management
#### `getCacheMetrics(pdfId)`
Gets cache performance metrics for a PDF.
#### `clearCacheDirect(pdfId, cacheType)`
Clears specified cache types.
**Parameters:**
- `cacheType` (string): 'all', 'base64', or 'bytes'
#### `optimizeMemory(pdfId)`
Optimizes memory usage for a PDF.
### Search Operations
#### `searchTextDirect(pdfId, searchTerm, startPage, endPage)`
Searches for text within specified pages.
**Returns:** `Promise<Array>` - Array of search results with positions
### Performance Monitoring
#### `getPerformanceMetrics(pdfId)`
Gets comprehensive performance metrics.
#### `getJSIStats()`
Gets JSI system statistics.
#### `getPerformanceHistory()`
Gets local performance tracking history.
#### `clearPerformanceHistory()`
Clears local performance tracking data.
### Quality Control
#### `setRenderQuality(pdfId, quality)`
Sets render quality level.
**Parameters:**
- `quality` (number): 1 (low), 2 (medium), 3 (high)
## Performance Characteristics
### Benchmark Results
| Operation | Bridge Mode | JSI Mode | **Improvement** |
|-----------|-------------|----------|-----------------|
| Page Render | 45ms | 2ms | **22.5x faster** |
| Page Metrics | 12ms | 0.5ms | **24x faster** |
| Cache Access | 8ms | 0.1ms | **80x faster** |
| Text Search | 120ms | 15ms | **8x faster** |
### Memory Usage
- **Base Memory**: ~2MB for JSI runtime
- **Per PDF**: ~500KB average
- **Cache Overhead**: ~100KB per cached page
- **Automatic Cleanup**: Memory optimized every 30 seconds
## Troubleshooting
### Common Issues
1. **JSI Not Available**
```
Error: JSI not available - falling back to bridge mode
```
- Ensure Android NDK is installed
- Check CMake configuration
- Verify package registration
2. **Build Failures**
```
CMake Error: Could not find ReactAndroid
```
- Update React Native to 0.72+
- Check CMake version (3.13+)
- Verify C++17 support
3. **Performance Issues**
```
Slow JSI operations
```
- Check memory usage
- Clear cache if needed
- Optimize render quality settings
### Debug Mode
Enable debug logging:
```javascript
// Enable detailed JSI logging
console.log('JSI Stats:', await PDFJSI.getJSIStats());
console.log('Performance History:', PDFJSI.getPerformanceHistory());
```
## Migration Guide
### From Bridge Mode to JSI
1. **Check Compatibility**
```javascript
const isJSI = await PDFJSI.checkJSIAvailability();
if (isJSI) {
// Use JSI methods
await PDFJSI.renderPageDirect(...);
} else {
// Fallback to bridge methods
await legacyPdfModule.renderPage(...);
}
```
2. **Update Method Calls**
```javascript
// Old bridge method
const result = await PdfModule.renderPage(pageNumber, scale, base64Data);
// New JSI method
const result = await PDFJSI.renderPageDirect(pdfId, pageNumber, scale, base64Data);
```
3. **Handle Errors**
```javascript
try {
const result = await PDFJSI.renderPageDirect(...);
} catch (error) {
if (error.message.includes('JSI not available')) {
// Fallback to bridge mode
const result = await legacyPdfModule.renderPage(...);
}
}
```
## Contributing
### Development Setup
1. Clone the repository
2. Install dependencies
3. Build native libraries:
```bash
cd android/src/main/cpp
mkdir build && cd build
cmake ..
make
```
### Testing
Run JSI tests:
```bash
npm run test:jsi
```
### Performance Testing
Benchmark JSI vs Bridge:
```bash
npm run benchmark
```
## License
Copyright (c) 2025-present, Punith M (punithm300@gmail.com). Enhanced PDF JSI Integration. All rights reserved.
## Support
For issues and questions:
- GitHub Issues: [react-native-pdf-enhanced](https://github.com/126punith/react-native-pdf-enhanced)
- Performance Issues: Include JSI stats and performance history
- Build Issues: Include CMake logs and Android NDK version
- Contact: punithm300@gmail.com