Skip to main content

What's New in v4.1.0

Version 4.1.0 is a critical bug fix release that resolves the issue where onLoadComplete prop was not firing on Android devices. This was a regression similar to react-native-pdf issue #899.

iOS Status

The onLoadComplete callback has always worked correctly on iOS. This fix is Android-specific. iOS uses a notification-based approach (PDFViewDocumentChangedNotification) which is inherently more reliable and doesn't require the same timing fixes.

Critical Fix: Android onLoadComplete Callback

Problem

The onLoadComplete callback was not being triggered on Android devices, preventing applications from:

  • Knowing when PDF loading completed
  • Accessing the number of pages
  • Getting file paths for further operations
  • Initializing UI state based on PDF metadata

Root Causes

The issue had multiple root causes:

  1. Error Handling: The loadComplete() method could fail silently if getPageSize() threw exceptions, preventing the event from being dispatched
  2. Timing Issues: Events were dispatched before the React component was ready to receive them, causing events to be lost
  3. Initial Load: drawPdf() was not being called on initial mount, preventing PDF from loading properly

Solution

The fix implements a comprehensive solution with multiple safeguards:

1. Comprehensive Error Handling

Added robust error handling in loadComplete() to ensure events are always dispatched even if errors occur:

try {
SizeF pageSize = getPageSize(0);
if (pageSize != null) {
width = pageSize.getWidth();
height = pageSize.getHeight();
}
} catch (Exception e) {
showLog("Error getting page size in loadComplete: " + e.getMessage());
// Continue with default values to ensure event is dispatched
}

2. Delayed Event Dispatch

Implemented delayed event dispatch using Handler.post() to ensure React component is mounted and ready:

new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
if (dispatcher != null) {
dispatcher.dispatchEvent(tce);
loadCompleteDispatched = true;
showLog("loadComplete: Event dispatched successfully (delayed)");
}
}
});

3. Restored PDF Loading

Restored drawPdf() call in onAfterUpdateTransaction() to ensure PDF loads on initial mount:

@Override
public void onAfterUpdateTransaction(PdfView pdfView) {
super.onAfterUpdateTransaction(pdfView);
pdfView.drawPdf();
}

4. Duplicate Prevention

Added tracking flags to prevent duplicate loadComplete events:

private boolean loadCompleteDispatched = false;
private int lastKnownPageCount = 0;

5. Fallback Mechanisms

Added fallback dispatch in drawPdf() when skipping reload if PDF is already loaded but event wasn't dispatched.

Technical Implementation

Files Modified:

  • android/src/main/java/org/wonday/pdf/PdfView.java - Added error handling, delayed dispatch, and tracking flags
  • android/src/main/java/org/wonday/pdf/PdfManager.java - Restored drawPdf() call in onAfterUpdateTransaction()
  • ios/RNPDFPdf/RNPDFPdfView.mm - Updated onDocumentChanged to include file path in loadComplete message for consistency with Android
  • index.js - Added debug logging for event tracking in development mode

Key Changes:

  • Added Handler and Looper imports for delayed dispatch
  • Added loadCompleteDispatched and lastKnownPageCount instance variables
  • Wrapped error-prone operations in try-catch blocks
  • Implemented delayed event dispatch mechanism
  • Added fallback dispatch logic in drawPdf()
  • Reset flags when PDF path changes

Testing & Verification

The fix has been tested and verified on Android devices. The onLoadComplete callback now fires reliably with all expected parameters:

<Pdf
source={{ uri: 'https://example.com/document.pdf' }}
onLoadComplete={(numberOfPages, filePath, size, tableContents) => {
console.log(`PDF loaded: ${numberOfPages} pages`);
console.log(`File path: ${filePath}`);
console.log(`Size: ${size.width} x ${size.height}`);
}}
/>

Migration

Good News: This is a bug fix release with NO breaking changes!

Simply upgrade to v4.1.0:

npm install react-native-pdf-jsi@4.1.0
# or
yarn add react-native-pdf-jsi@4.1.0

No code changes required. The onLoadComplete callback will now work correctly on Android.

This fix resolves the same issue reported in:


Summary

Version 4.1.0 ensures reliable onLoadComplete callback execution on Android through:

  • ✅ Comprehensive error handling
  • ✅ Delayed event dispatch for proper timing
  • ✅ Restored PDF loading on initial mount
  • ✅ Duplicate event prevention
  • ✅ Fallback dispatch mechanisms

Tested and verified on Android - onLoadComplete now fires reliably!


Enjoy react-native-pdf-jsi v4.1.0! 🚀