Skip to content

Commit 40f00f2

Browse files
committed
Add onError prop and implement on iOS
1 parent 78e3627 commit 40f00f2

9 files changed

Lines changed: 69 additions & 15 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ For more details, see the [Sample Project](https://github.com/douglasjunior/reac
9696
|maxPageResolution|`number`|`2048`|(Android only) Max page resolution (width/height) in pixels when zooming. Defined to prevent Android crash when zooming too much: https://github.com/douglasjunior/react-native-pdf-renderer/issues/26 . |
9797
|singlePage|`boolean`|`false`|Renders only the first page without scroll. (useful for display thumbnail)|
9898
|onPageChange|`(current: number, total: number) => void`||Invoked on pages scroll.|
99+
|onError|`() => void`||Invoked when an error occurs.|
99100
|style|`StyleProp<ViewStyle>`||Styles to be applied to the native [view](https://reactnative.dev/docs/view-style-props).|
100101

101102
## Limitations

Sample/App.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import PdfRendererView from 'react-native-pdf-renderer';
44
import * as FileSystem from 'expo-file-system';
55
// import ReactNativeBlobUtil from 'react-native-blob-util';
66

7-
const PDF_URL =
8-
'https://www.nasa.gov/wp-content/uploads/static/history/alsj/a11/a11final-fltpln.pdf'; // 618 pages
9-
// const PDF_URL = 'https://www.africau.edu/images/default/sample.pdf'; // 2 pages
7+
const PDF_URL = 'https://github.com/douglasjunior/react-native-pdf-renderer/raw/refs/heads/main/Sample/A17_FlightPlan.pdf'; // 618 pages
8+
// const PDF_URL = 'https://github.com/ArturT/Test-PDF-Files/raw/refs/heads/master/not_encrypted.pdf'; // 1 pages
9+
// const PDF_URL = 'https://github.com/ArturT/Test-PDF-Files/raw/refs/heads/master/corrupted.pdf'; // corrupted
1010

1111
function App() {
1212
const [totalPages, setTotalPages] = useState(0);
@@ -73,7 +73,7 @@ function App() {
7373
}
7474

7575
return (
76-
<>
76+
<View style={{flex: 1}}>
7777
<Button
7878
title="Single Page"
7979
onPress={() => setSinglePage(prev => !prev)}
@@ -109,7 +109,7 @@ function App() {
109109
{currentPage + 1}/{totalPages}
110110
</Text>
111111
</View>
112-
</>
112+
</View>
113113
);
114114
};
115115

Sample/ios/Podfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1740,7 +1740,7 @@ PODS:
17401740
- React-logger (= 0.79.4)
17411741
- React-perflogger (= 0.79.4)
17421742
- React-utils (= 0.79.4)
1743-
- ReactNativePdfRenderer (2.0.0):
1743+
- ReactNativePdfRenderer (2.0.1):
17441744
- DoubleConversion
17451745
- glog
17461746
- hermes-engine
@@ -2093,7 +2093,7 @@ SPEC CHECKSUMS:
20932093
ReactAppDependencyProvider: 04be2a00933bf0930cafcc4da1e0760143573d82
20942094
ReactCodegen: 1bcfcb6519fadfb3e0072e27b8ff868b7dfbac64
20952095
ReactCommon: 2bc79208d91eda74edeff759d82c93460b07612f
2096-
ReactNativePdfRenderer: 776f3e1e51b0e42fbbe739d1042ac39ef14dea69
2096+
ReactNativePdfRenderer: 234866730af5a5eae357d6f59199b9d28acee82a
20972097
SocketRocket: d4aabe649be1e368d1318fdf28a022d714d65748
20982098
Yoga: a6cb833e04fb8c59a012b49fb1d040fcb0cbb633
20992099

ios/ReactNativePdfRendererLibrary/RNPDFView.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@
3636

3737
#ifdef RCT_NEW_ARCH_ENABLED
3838
#else
39-
@property (nonatomic, copy) RCTBubblingEventBlock onPageChange;
39+
@property (nonatomic, copy) RCTBubblingEventBlock onRnPdfPageChange;
40+
@property (nonatomic, copy) RCTBubblingEventBlock onRnPdfError;
4041
#endif
4142

43+
FOUNDATION_EXPORT NSNotificationName const RNPDFViewErrorNotification;
4244
-(void) setDistanceBetweenPages:(NSNumber*) distance;
4345
-(void) setParams:(NSDictionary*) params;
4446

ios/ReactNativePdfRendererLibrary/RNPDFView.mm

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
@implementation RNPDFView
2929

30+
NSNotificationName const RNPDFViewErrorNotification = @"RNPDFViewErrorNotification";
31+
3032
-(void) setParams:(NSDictionary*) params {
3133
NSString *source = [params objectForKey:@"source"];
3234
NSString *maxZoomString = [params objectForKey:@"maxZoom"];
@@ -52,6 +54,10 @@ -(void) setParams:(NSDictionary*) params {
5254
self.displayMode = singlePage ? kPDFDisplaySinglePage : kPDFDisplaySinglePageContinuous;
5355

5456
dispatch_async(dispatch_get_main_queue(), ^{
57+
if (pdfDocument == nil || pdfDocument.pageCount == 0) {
58+
[NSNotificationCenter.defaultCenter postNotificationName:RNPDFViewErrorNotification object:self];
59+
return;
60+
}
5561
self.document = pdfDocument;
5662
});
5763

ios/ReactNativePdfRendererLibrary/RNPdfRendererComponentView.mm

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ -(instancetype)init
4848
if (!observerAdded) {
4949
observerAdded = YES;
5050
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(handlePageChange:) name:PDFViewPageChangedNotification object:nil];
51+
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(handleError:) name:RNPDFViewErrorNotification object:nil];
5152
}
5253
if(self = [super init]) {
5354
_pdfView = [RNPDFView new];
@@ -65,18 +66,26 @@ - (void)dealloc
6566
}
6667
}
6768

68-
- (void)handlePageChange:(NSNotification*) notification {
69-
if ([RNPDFView class] != [notification.object class]) {
69+
- (void) handleError:(NSNotification*) notification {
70+
if (notification.object != _pdfView) {
7071
return;
7172
}
7273

73-
RNPDFView *view = notification.object;
74+
if (_eventEmitter) {
75+
self.eventEmitter.onError({});
76+
}
77+
}
78+
79+
- (void)handlePageChange:(NSNotification*) notification {
80+
if (notification.object != _pdfView) {
81+
return;
82+
}
7483

75-
NSUInteger currentPageNumber = [view.document indexForPage:view.currentPage];
84+
NSUInteger currentPageNumber = [_pdfView.document indexForPage:_pdfView.currentPage];
7685

7786
RNPdfRendererViewEventEmitter::OnPageChange result = RNPdfRendererViewEventEmitter::OnPageChange{};
7887
result.position = currentPageNumber;
79-
result.total = view.document.pageCount;
88+
result.total = _pdfView.document.pageCount;
8089

8190
if (_eventEmitter) {
8291
self.eventEmitter.onPageChange(result);

ios/ReactNativePdfRendererLibrary/RNPdfRendererModule.m

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ - (UIView *)view
3939
if (!observerAdded) {
4040
observerAdded = YES;
4141
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(handlePageChange:) name:PDFViewPageChangedNotification object:nil];
42+
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(handleError:) name:RNPDFViewErrorNotification object:nil];
4243
}
4344

4445
RNPDFView *view = [[RNPDFView alloc] init];
@@ -54,6 +55,18 @@ - (void)dealloc
5455
}
5556
}
5657

58+
- (void) handleError:(NSNotification*) notification {
59+
if ([RNPDFView class] != [notification.object class]) {
60+
return;
61+
}
62+
63+
RNPDFView *view = notification.object;
64+
65+
dispatch_async(dispatch_get_main_queue(), ^{
66+
view.onRnPdfError(@{});
67+
});
68+
}
69+
5770
- (void)handlePageChange:(NSNotification*) notification {
5871
if ([RNPDFView class] != [notification.object class]) {
5972
return;
@@ -64,14 +77,15 @@ - (void)handlePageChange:(NSNotification*) notification {
6477
NSUInteger currentPageNumber = [view.document indexForPage:view.currentPage];
6578

6679
dispatch_async(dispatch_get_main_queue(), ^{
67-
view.onPageChange(@{
80+
view.onRnPdfPageChange(@{
6881
@"position": [NSNumber numberWithInteger:currentPageNumber],
6982
@"total": [NSNumber numberWithInteger:view.document.pageCount],
7083
});
7184
});
7285
}
7386

74-
RCT_EXPORT_VIEW_PROPERTY(onPageChange, RCTBubblingEventBlock)
87+
RCT_EXPORT_VIEW_PROPERTY(onRnPdfPageChange, RCTBubblingEventBlock)
88+
RCT_EXPORT_VIEW_PROPERTY(onRnPdfError, RCTBubblingEventBlock)
7589

7690
RCT_CUSTOM_VIEW_PROPERTY(params, NSDictionary, RNPDFView)
7791
{

src/PdfRendererView.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ export type PdfRendererViewPropsType = {
8080
* @param totalPages total pages number
8181
*/
8282
onPageChange?: (page: number, totalPages: number) => void;
83+
/**
84+
* Invoked when an error occurs.
85+
*/
86+
onError?: () => void;
8387
};
8488

8589
type OnPageChangeEventType = {
@@ -97,6 +101,7 @@ const styles = StyleSheet.create({
97101
const PdfRendererView = ({
98102
testID = undefined,
99103
onPageChange,
104+
onError = undefined,
100105
style,
101106
source,
102107
singlePage = false,
@@ -123,6 +128,17 @@ const PdfRendererView = ({
123128
[onPageChange],
124129
);
125130

131+
const handleError = useCallback((event: NativeSyntheticEvent<{}>) => {
132+
if (!onError && __DEV__) {
133+
console.warn(
134+
'react-native-pdf-renderer: An error occurred while rendering the PDF. ',
135+
event.nativeEvent
136+
);
137+
return;
138+
}
139+
onError?.();
140+
}, [onError]);
141+
126142
const params: NativeParams = useMemo(
127143
() => ({
128144
source,
@@ -140,6 +156,11 @@ const PdfRendererView = ({
140156
style={viewStyles}
141157
params={params}
142158
onPageChange={handlePageChange}
159+
onError={handleError}
160+
// old architecture
161+
// @ts-ignore
162+
onRnPdfPageChange={handlePageChange}
163+
onRnPdfError={handleError}
143164
/>
144165
);
145166
};

src/specs/RNPdfRendererViewNativeComponent.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export interface NativeProps extends ViewProps {
4444
distanceBetweenPages: Float;
4545
params: NativeParams;
4646
onPageChange: BubblingEventHandler<PageChangeEventPayload>;
47+
onError: BubblingEventHandler<{}>;
4748
}
4849

4950
export default codegenNativeComponent<NativeProps>(

0 commit comments

Comments
 (0)