react-native-pdf-renderer 1.1.1 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -12,6 +12,14 @@ It uses [PdfRenderer](https://developer.android.com/reference/android/graphics/p
12
12
  |-|-|
13
13
  |<img src="https://github.com/douglasjunior/react-native-pdf-renderer/raw/main/screenshots/android.gif" width="240"/>|<img src="https://github.com/douglasjunior/react-native-pdf-renderer/raw/main/screenshots/ios.gif" width="240"/>
14
14
 
15
+ ## Why another PDF renderer?
16
+
17
+ The main reason why I create this library is to avoid using third party native dependencies, like `com.github.TalbotGooday:AndroidPdfViewer`, `com.github.mhiew:android-pdf-viewer`, `react-native-blob-util` or even `react-native-webview`.
18
+
19
+ But why?
20
+
21
+ Every React Native developer knows (or will discover soon) the pain of updating the React Native ecosystem when a new version of Android or iOS comes out, so here we want to avoid this pain as much as possible.
22
+
15
23
  ## Requirements
16
24
 
17
25
  - React Native >= 0.60.0
@@ -65,8 +73,6 @@ For more details, see the [Sample Project](https://github.com/douglasjunior/reac
65
73
 
66
74
  ## PdfRendererView props
67
75
 
68
- Inherits [View Props](https://reactnative.dev/docs/view#props).
69
-
70
76
  |Name|Value|Default|Description|
71
77
  |-|-|-|-|
72
78
  |source|`string`||Path to a file stored on device.|
@@ -74,6 +80,7 @@ Inherits [View Props](https://reactnative.dev/docs/view#props).
74
80
  |maxZoom|`number`|`5`|Max zoom scale.|
75
81
  |singlePage|`boolean`|`false`|(Experimental) Renders only the first page without scroll. (useful for display thumbnail)|
76
82
  |onPageChange|`(current: number, total: number) => void`||Invoked on pages scroll.|
83
+ |style|`StyleProp<ViewStyle>`||Styles to be applied to the View |
77
84
 
78
85
  ## Limitations
79
86
 
@@ -83,7 +90,7 @@ The `PdfRendererView` is `flex: 1` by default, so you need to make sure that you
83
90
 
84
91
  ### Border radius
85
92
 
86
- The `borderRadius` style is ignored by React Native custom view in Android and crash on iOS. (read more #1)
93
+ The `borderRadius` style is ignored by React Native custom view in Android and crash on iOS. (read more [#1](https://github.com/douglasjunior/react-native-pdf-renderer/issues/1#issuecomment-1483395465))
87
94
 
88
95
  If you need `borderRadius`, the best option is to wrap the `PdfRendererView` in another `View`.
89
96
 
@@ -30,7 +30,6 @@ import com.facebook.react.bridge.ReactApplicationContext;
30
30
  import com.facebook.react.uimanager.ViewManager;
31
31
  import com.github.douglasjunior.reactNativePdfRenderer.modules.PdfRendererViewManager;
32
32
 
33
- import java.util.Arrays;
34
33
  import java.util.Collections;
35
34
  import java.util.List;
36
35
 
@@ -44,7 +43,7 @@ public class PdfRendererPackage implements ReactPackage {
44
43
  @NonNull
45
44
  @Override
46
45
  public List<ViewManager> createViewManagers(@NonNull ReactApplicationContext reactApplicationContext) {
47
- return Arrays.asList(
46
+ return Collections.singletonList(
48
47
  new PdfRendererViewManager(reactApplicationContext)
49
48
  );
50
49
  }
@@ -0,0 +1,63 @@
1
+ // MIT License
2
+
3
+ // Copyright (c) 2023 Douglas Nassif Roma Junior
4
+
5
+ // Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ // of this software and associated documentation files (the "Software"), to deal
7
+ // in the Software without restriction, including without limitation the rights
8
+ // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ // copies of the Software, and to permit persons to whom the Software is
10
+ // furnished to do so, subject to the following conditions:
11
+
12
+ // The above copyright notice and this permission notice shall be included in all
13
+ // copies or substantial portions of the Software.
14
+
15
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ // SOFTWARE.
22
+
23
+ package com.github.douglasjunior.reactNativePdfRenderer.modules;
24
+
25
+ import java.util.HashSet;
26
+ import java.util.Set;
27
+
28
+ public class ObservableZoom {
29
+ private final Set<ZoomChangeListener> listeners = new HashSet<>();
30
+ private float zoom;
31
+
32
+ public ObservableZoom(int initialZoom) {
33
+ this.zoom = initialZoom;
34
+ }
35
+
36
+ public void addListener(ZoomChangeListener listener) {
37
+ this.listeners.add(listener);
38
+ }
39
+
40
+ public void removeListener(ZoomChangeListener zoomListener) {
41
+ this.listeners.remove(zoomListener);
42
+ }
43
+
44
+ private void notifyListeners() {
45
+ for (ZoomChangeListener listener : listeners) {
46
+ listener.onZoomChange(this.zoom);
47
+ }
48
+ }
49
+
50
+ public float getZoom() {
51
+ return zoom;
52
+ }
53
+
54
+ public void setZoom(float newZoom) {
55
+ if (newZoom == this.zoom) return;
56
+ this.zoom = newZoom;
57
+ notifyListeners();
58
+ }
59
+
60
+ public interface ZoomChangeListener {
61
+ void onZoomChange(float newZoom);
62
+ }
63
+ }
@@ -31,6 +31,8 @@ import android.graphics.Color;
31
31
  import android.graphics.Matrix;
32
32
  import android.graphics.pdf.PdfRenderer;
33
33
  import android.os.ParcelFileDescriptor;
34
+ import android.text.TextUtils;
35
+ import android.util.Log;
34
36
  import android.util.TypedValue;
35
37
  import android.view.GestureDetector;
36
38
  import android.view.MotionEvent;
@@ -45,38 +47,47 @@ import androidx.recyclerview.widget.RecyclerView;
45
47
 
46
48
  import com.facebook.react.bridge.Arguments;
47
49
  import com.facebook.react.bridge.ReactApplicationContext;
48
- import com.facebook.react.bridge.WritableMap;
49
50
  import com.facebook.react.uimanager.events.RCTEventEmitter;
50
51
 
51
52
  import java.io.File;
52
53
  import java.io.IOException;
53
54
 
55
+ @SuppressLint({"ViewConstructor", "NotifyDataSetChanged"})
54
56
  public class PdfRendererRecyclerView extends RecyclerView {
55
57
  private final ViewGroup mParent;
56
58
  private final GestureDetector mGestureDetector;
59
+ private final int mMinZoom = 1;
60
+ private final ReactApplicationContext mReactApplicationContext;
61
+ private final ScaleGestureDetector mScaleDetector;
62
+ private final Matrix mMatrix;
63
+ private final ObservableZoom mZoomObserver;
64
+ private final LayoutManager mLayoutManager;
57
65
  private boolean mRequestedLayout = false;
58
- private float mMinZoom = 1;
59
66
  private float mMaxZoom = 5;
60
67
  private float mDistanceBetweenPages = 0;
61
68
  private int mWidth;
62
69
  private int mHeight;
63
70
  private int mCurrentItemPosition = -1;
64
- private ReactApplicationContext mReactApplicationContext;
65
- private ScaleGestureDetector mScaleDetector;
66
- private Matrix mMatrix;
67
71
  private boolean mSinglePage;
68
72
 
69
73
  public PdfRendererRecyclerView(@NonNull ReactApplicationContext context, ViewGroup parent) {
70
74
  super(context);
71
75
 
72
- LayoutManager lm = new LayoutManager(context);
73
- lm.setOrientation(LinearLayoutManager.VERTICAL);
76
+ mZoomObserver = new ObservableZoom(mMinZoom);
74
77
 
75
- PdfRendererAdapter adapter = new PdfRendererAdapter();
78
+ mLayoutManager = new LayoutManager(context);
79
+ mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
76
80
 
77
- this.setLayoutManager(lm);
81
+ var adapter = new PdfRendererAdapter();
82
+
83
+ this.setLayoutManager(mLayoutManager);
78
84
  this.setAdapter(adapter);
79
- this.addOnScrollListener(new ScrollListener());
85
+ this.addOnScrollListener(new OnScrollListener() {
86
+ @Override
87
+ public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
88
+ dispatchPageChangeEvent();
89
+ }
90
+ });
80
91
 
81
92
  mReactApplicationContext = context;
82
93
  mParent = parent;
@@ -99,37 +110,37 @@ public class PdfRendererRecyclerView extends RecyclerView {
99
110
 
100
111
  public void closeAdapter() {
101
112
  try {
102
- PdfRendererAdapter adapter = (PdfRendererAdapter) getAdapter();
113
+ var adapter = (PdfRendererAdapter) getAdapter();
114
+ assert adapter != null;
103
115
  adapter.close();
104
116
  adapter.notifyDataSetChanged();
105
117
  } catch (Exception ex) {
106
- ex.printStackTrace();
107
- // do nothing
118
+ Log.e("PdfRendererRecyclerView", "Error closing adapter", ex);
108
119
  }
109
120
  }
110
121
 
111
122
  public void updateSource(String source) throws IOException {
112
123
  mCurrentItemPosition = -1;
113
- PdfRendererAdapter adapter = (PdfRendererAdapter) getAdapter();
124
+ var adapter = (PdfRendererAdapter) getAdapter();
125
+ if (adapter == null) return;
126
+ adapter.close();
114
127
  adapter.updateSource(source);
115
128
  adapter.notifyDataSetChanged();
116
- dispatchPageChangeEvent();
129
+ post(this::dispatchPageChangeEvent);
117
130
  }
118
131
 
119
132
  private void dispatchPageChangeEvent() {
120
- int position = ((LinearLayoutManager) getLayoutManager()).findLastCompletelyVisibleItemPosition();
133
+ var newPosition = mLayoutManager.findLastCompletelyVisibleItemPosition();
134
+ if (newPosition < 0) newPosition = mLayoutManager.findLastVisibleItemPosition();
121
135
 
122
- if (position < 0) {
123
- position = ((LinearLayoutManager) getLayoutManager()).findLastVisibleItemPosition();
124
- }
136
+ if (newPosition != mCurrentItemPosition) {
137
+ mCurrentItemPosition = newPosition;
125
138
 
126
- if (position != mCurrentItemPosition) {
127
- mCurrentItemPosition = position;
139
+ var adapter = (PdfRendererAdapter) getAdapter();
140
+ if (adapter == null) return;
128
141
 
129
- PdfRendererAdapter adapter = (PdfRendererAdapter) getAdapter();
130
-
131
- WritableMap event = Arguments.createMap();
132
- event.putInt("position", position);
142
+ var event = Arguments.createMap();
143
+ event.putInt("position", newPosition);
133
144
  event.putInt("total", adapter.getPageCount());
134
145
 
135
146
  mReactApplicationContext
@@ -156,19 +167,21 @@ public class PdfRendererRecyclerView extends RecyclerView {
156
167
  @Override
157
168
  protected void onSizeChanged(int w, int h, int oldw, int oldh) {
158
169
  super.onSizeChanged(w, h, oldw, oldh);
159
- float scale = Math.max(w / mWidth, h / mHeight);
170
+ var scale = mWidth > 0 && mHeight > 0
171
+ ? Math.max(w / mWidth, h / mHeight)
172
+ : 1;
160
173
  mMatrix.setScale(scale, scale);
161
174
  mMatrix.postTranslate((w - scale * mWidth) / 2f, (h - scale * mHeight) / 2f);
162
175
  }
163
176
 
164
177
  private boolean isScrollingInsideZoomedArea() {
165
- float[] values = new float[9];
178
+ var values = new float[9];
166
179
  mMatrix.getValues(values);
167
180
 
168
- float scaleY = Math.min(Math.max(values[Matrix.MSCALE_Y], mMinZoom), mMaxZoom);
169
- float posY = values[Matrix.MTRANS_Y];
181
+ var scaleY = Math.min(Math.max(values[Matrix.MSCALE_Y], mMinZoom), mMaxZoom);
182
+ var posY = values[Matrix.MTRANS_Y];
170
183
 
171
- float maxPosY = mHeight - mHeight * scaleY;
184
+ var maxPosY = mHeight - mHeight * scaleY;
172
185
 
173
186
  return posY < 0 && posY > maxPosY;
174
187
  }
@@ -195,7 +208,7 @@ public class PdfRendererRecyclerView extends RecyclerView {
195
208
  try {
196
209
  return super.onInterceptTouchEvent(ev);
197
210
  } catch (IllegalArgumentException ex) {
198
- ex.printStackTrace();
211
+ Log.e("PdfRendererRecyclerView", "Error intercepting touch event", ex);
199
212
  }
200
213
  return false;
201
214
  }
@@ -206,17 +219,17 @@ public class PdfRendererRecyclerView extends RecyclerView {
206
219
  }
207
220
 
208
221
  private void validateMatrixLimits() {
209
- float[] values = new float[9];
222
+ var values = new float[9];
210
223
  mMatrix.getValues(values);
211
224
 
212
- float scaleX = Math.min(Math.max(values[Matrix.MSCALE_X], mMinZoom), mMaxZoom);
213
- float posX = values[Matrix.MTRANS_X];
225
+ var scaleX = Math.min(Math.max(values[Matrix.MSCALE_X], mMinZoom), mMaxZoom);
226
+ var posX = values[Matrix.MTRANS_X];
214
227
 
215
- float scaleY = Math.min(Math.max(values[Matrix.MSCALE_Y], mMinZoom), mMaxZoom);
216
- float posY = values[Matrix.MTRANS_Y];
228
+ var scaleY = Math.min(Math.max(values[Matrix.MSCALE_Y], mMinZoom), mMaxZoom);
229
+ var posY = values[Matrix.MTRANS_Y];
217
230
 
218
- float maxPosX = mWidth - mWidth * scaleX;
219
- float maxPosY = mHeight - mHeight * scaleY;
231
+ var maxPosX = mWidth - mWidth * scaleX;
232
+ var maxPosY = mHeight - mHeight * scaleY;
220
233
 
221
234
  if (posX > 0.0f)
222
235
  posX = 0.0f;
@@ -259,11 +272,11 @@ public class PdfRendererRecyclerView extends RecyclerView {
259
272
  private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
260
273
  @Override
261
274
  public boolean onScale(ScaleGestureDetector detector) {
262
- float[] values = new float[9];
275
+ var values = new float[9];
263
276
  mMatrix.getValues(values);
264
277
 
265
- float zoom = values[Matrix.MSCALE_X];
266
- float factor = detector.getScaleFactor();
278
+ var zoom = values[Matrix.MSCALE_X];
279
+ var factor = detector.getScaleFactor();
267
280
 
268
281
  if (zoom >= mMaxZoom && factor > 1) {
269
282
  return false;
@@ -272,8 +285,20 @@ public class PdfRendererRecyclerView extends RecyclerView {
272
285
  mMatrix.postScale(factor, factor, getWidth() / 2f, getHeight() / 2f);
273
286
  validateMatrixLimits();
274
287
  ViewCompat.postInvalidateOnAnimation(PdfRendererRecyclerView.this);
288
+
275
289
  return true;
276
290
  }
291
+
292
+ @Override
293
+ public void onScaleEnd(@NonNull ScaleGestureDetector detector) {
294
+ super.onScaleEnd(detector);
295
+
296
+ var values = new float[9];
297
+ mMatrix.getValues(values);
298
+ var zoom = values[Matrix.MSCALE_X];
299
+
300
+ mZoomObserver.setZoom(zoom);
301
+ }
277
302
  }
278
303
 
279
304
  private class GestureListener extends GestureDetector.SimpleOnGestureListener {
@@ -291,13 +316,13 @@ public class PdfRendererRecyclerView extends RecyclerView {
291
316
  return false;
292
317
  }
293
318
 
294
- float[] values = new float[9];
319
+ var values = new float[9];
295
320
  mMatrix.getValues(values);
296
321
 
297
- float currentZoom = values[Matrix.MSCALE_X];
298
- float newZoom = currentZoom > mMinZoom ? mMinZoom : mMaxZoom;
299
- float centerX = getWidth() / 2f;
300
- float centerY = getHeight() / 2f;
322
+ var currentZoom = values[Matrix.MSCALE_X];
323
+ var newZoom = currentZoom > mMinZoom ? mMinZoom : mMaxZoom;
324
+ var centerX = getWidth() / 2f;
325
+ var centerY = getHeight() / 2f;
301
326
 
302
327
  mMatrix.setScale(newZoom, newZoom, centerX, centerY);
303
328
  mMatrix.postTranslate(centerX - e.getX(), centerY - e.getY());
@@ -305,6 +330,8 @@ public class PdfRendererRecyclerView extends RecyclerView {
305
330
  validateMatrixLimits();
306
331
 
307
332
  ViewCompat.postInvalidateOnAnimation(PdfRendererRecyclerView.this);
333
+
334
+ mZoomObserver.setZoom(newZoom);
308
335
  return true;
309
336
  }
310
337
  }
@@ -315,15 +342,15 @@ public class PdfRendererRecyclerView extends RecyclerView {
315
342
  private PdfRenderer mPdfRenderer;
316
343
 
317
344
  public void updateSource(String source) throws IOException {
318
- if (source == null || source.isEmpty()) return;
319
- File file = new File(source.replace("file://", ""));
345
+ if (TextUtils.isEmpty(source)) return;
346
+ var file = new File(source.replace("file://", ""));
320
347
  mFileDescriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
321
348
  mPdfRenderer = new PdfRenderer(mFileDescriptor);
322
349
  }
323
350
 
324
351
  public void close() throws IOException {
325
- mPdfRenderer.close();
326
- mFileDescriptor.close();
352
+ if (mPdfRenderer != null) mPdfRenderer.close();
353
+ if (mFileDescriptor != null) mFileDescriptor.close();
327
354
  mPdfRenderer = null;
328
355
  mFileDescriptor = null;
329
356
  }
@@ -331,40 +358,30 @@ public class PdfRendererRecyclerView extends RecyclerView {
331
358
  @NonNull
332
359
  @Override
333
360
  public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
334
- ImageView imageView = new ImageView(parent.getContext());
361
+ var imageView = new ImageView(parent.getContext());
335
362
  imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
336
363
  imageView.setBackgroundColor(Color.WHITE);
337
- LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, 0);
364
+ var params = new LayoutParams(LayoutParams.MATCH_PARENT, 0);
338
365
  imageView.setLayoutParams(params);
339
- ViewHolder viewHolder = new ViewHolder(parent, imageView);
340
- return viewHolder;
366
+ return new ViewHolder(imageView);
341
367
  }
342
368
 
343
369
  @Override
344
- public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
345
- if (mPdfRenderer == null) return;
346
-
347
- PdfRenderer.Page page = mPdfRenderer.openPage(position);
348
- Bitmap bitmap = Bitmap.createBitmap(page.getWidth(), page.getHeight(), Bitmap.Config.ARGB_4444);
349
- page.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
350
-
351
- ImageView imageView = holder.getImageView();
352
- imageView.setImageBitmap(bitmap);
353
-
354
- LayoutParams lp = (LayoutParams) imageView.getLayoutParams();
355
- lp.width = LayoutParams.MATCH_PARENT;
356
-
357
- if (mSinglePage) {
358
- lp.height = LayoutParams.MATCH_PARENT;
359
- lp.setMargins(0, 0, 0, 0);
360
- } else {
361
- lp.height = Math.round(((float) mWidth / (float) page.getWidth()) * (float) page.getHeight());
362
- lp.setMargins(0, 0, 0, (int) mDistanceBetweenPages);
363
- }
370
+ public void onViewDetachedFromWindow(@NonNull ViewHolder holder) {
371
+ super.onViewDetachedFromWindow(holder);
372
+ holder.detachFromWindow();
373
+ }
364
374
 
365
- imageView.setLayoutParams(lp);
375
+ @Override
376
+ public void onViewAttachedToWindow(@NonNull ViewHolder holder) {
377
+ super.onViewAttachedToWindow(holder);
378
+ holder.attachFromWindow();
379
+ }
366
380
 
367
- page.close();
381
+ @Override
382
+ public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
383
+ if (mPdfRenderer == null) return;
384
+ holder.update(position, mZoomObserver.getZoom());
368
385
  }
369
386
 
370
387
  @Override
@@ -382,29 +399,61 @@ public class PdfRendererRecyclerView extends RecyclerView {
382
399
  }
383
400
 
384
401
  public class ViewHolder extends RecyclerView.ViewHolder {
402
+ private ObservableZoom.ZoomChangeListener zoomListener;
385
403
 
386
- private final ViewGroup mParentView;
387
-
388
- public ViewHolder(ViewGroup parentView, ImageView imageView) {
404
+ public ViewHolder(ImageView imageView) {
389
405
  super(imageView);
390
- mParentView = parentView;
391
- }
392
-
393
- public ViewGroup getParentView() {
394
- return mParentView;
395
406
  }
396
407
 
397
408
  public ImageView getImageView() {
398
409
  return (ImageView) this.itemView;
399
410
  }
400
- }
401
- }
402
411
 
403
- class ScrollListener extends OnScrollListener {
412
+ public void update(int position, float newZoom) {
413
+ var page = mPdfRenderer.openPage(position);
414
+ var bitmap = Bitmap.createBitmap(
415
+ Math.round(page.getWidth() * newZoom),
416
+ Math.round(page.getHeight() * newZoom),
417
+ Bitmap.Config.ARGB_4444
418
+ );
419
+ page.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
420
+
421
+ var imageView = getImageView();
422
+ imageView.setImageBitmap(bitmap);
423
+
424
+ var lp = (LayoutParams) imageView.getLayoutParams();
425
+ lp.width = LayoutParams.MATCH_PARENT;
426
+
427
+ if (mSinglePage) {
428
+ lp.height = LayoutParams.MATCH_PARENT;
429
+ lp.setMargins(0, 0, 0, 0);
430
+ } else {
431
+ lp.height = Math.round(((float) mWidth / (float) page.getWidth()) * (float) page.getHeight());
432
+ lp.setMargins(0, 0, 0, (int) mDistanceBetweenPages);
433
+ }
434
+ imageView.setLayoutParams(lp);
435
+
436
+ page.close();
437
+ }
404
438
 
405
- @Override
406
- public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
407
- dispatchPageChangeEvent();
439
+ public void detachFromWindow() {
440
+ if (zoomListener != null) {
441
+ mZoomObserver.removeListener(zoomListener);
442
+ zoomListener = null;
443
+ }
444
+ }
445
+
446
+ public void attachFromWindow() {
447
+ detachFromWindow();
448
+ zoomListener = newZoom -> {
449
+ var position = getLayoutPosition();
450
+ if (position == NO_POSITION) return;
451
+ if (position < mLayoutManager.findFirstVisibleItemPosition()
452
+ || position > mLayoutManager.findLastVisibleItemPosition()) return;
453
+ update(position, newZoom);
454
+ };
455
+ mZoomObserver.addListener(zoomListener);
456
+ }
408
457
  }
409
458
  }
410
459
 
@@ -420,12 +469,11 @@ public class PdfRendererRecyclerView extends RecyclerView {
420
469
  * https://github.com/douglasjunior/react-native-pdf-renderer/issues/3
421
470
  */
422
471
 
423
- float[] values = new float[9];
472
+ var values = new float[9];
424
473
  mMatrix.getValues(values);
425
474
 
426
- float scaleY = values[Matrix.MSCALE_Y];
427
-
428
- int dyWithScale = Math.round(dy / scaleY);
475
+ var scaleY = values[Matrix.MSCALE_Y];
476
+ var dyWithScale = Math.round(dy / scaleY);
429
477
 
430
478
  return super.scrollVerticallyBy(dyWithScale, recycler, state);
431
479
  }
@@ -28,7 +28,6 @@ import android.widget.LinearLayout;
28
28
 
29
29
  import androidx.annotation.NonNull;
30
30
  import androidx.annotation.Nullable;
31
- import androidx.recyclerview.widget.RecyclerView;
32
31
 
33
32
  import com.facebook.react.bridge.ReactApplicationContext;
34
33
  import com.facebook.react.bridge.ReadableMap;
@@ -41,8 +40,7 @@ import java.io.IOException;
41
40
  import java.util.Map;
42
41
 
43
42
  public class PdfRendererViewManager extends SimpleViewManager<ViewGroup> {
44
-
45
- private ReactApplicationContext mReactApplicationContext;
43
+ private final ReactApplicationContext mReactApplicationContext;
46
44
 
47
45
  public PdfRendererViewManager(ReactApplicationContext reactApplicationContext) {
48
46
  this.mReactApplicationContext = reactApplicationContext;
@@ -57,16 +55,15 @@ public class PdfRendererViewManager extends SimpleViewManager<ViewGroup> {
57
55
  @NonNull
58
56
  @Override
59
57
  protected ViewGroup createViewInstance(@NonNull ThemedReactContext themedReactContext) {
60
- LinearLayout layout = new LinearLayout(themedReactContext);
58
+ var layout = new LinearLayout(themedReactContext);
61
59
  layout.setOrientation(LinearLayout.VERTICAL);
62
60
 
63
- LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
61
+ var params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
64
62
 
65
- PdfRendererRecyclerView recyclerView = new PdfRendererRecyclerView(mReactApplicationContext, layout);
63
+ var recyclerView = new PdfRendererRecyclerView(mReactApplicationContext, layout);
66
64
  recyclerView.setLayoutParams(params);
67
65
 
68
66
  layout.addView(recyclerView);
69
-
70
67
  layout.setClipToOutline(true);
71
68
 
72
69
  return layout;
@@ -75,7 +72,8 @@ public class PdfRendererViewManager extends SimpleViewManager<ViewGroup> {
75
72
  @Nullable
76
73
  @Override
77
74
  public Map<String, Object> getExportedCustomBubblingEventTypeConstants() {
78
- Map map = super.getExportedCustomBubblingEventTypeConstants();
75
+ var map = super.getExportedCustomBubblingEventTypeConstants();
76
+ if (map == null) map = MapBuilder.of();
79
77
  map.put("pageChange", MapBuilder.of(
80
78
  "phasedRegistrationNames",
81
79
  MapBuilder.of("bubbled", "onPageChange")
@@ -85,11 +83,13 @@ public class PdfRendererViewManager extends SimpleViewManager<ViewGroup> {
85
83
 
86
84
  @ReactProp(name = "params")
87
85
  public void setParams(ViewGroup layout, @Nullable ReadableMap params) throws IOException {
88
- PdfRendererRecyclerView recyclerView = (PdfRendererRecyclerView) layout.getChildAt(0);
86
+ if (params == null) return;
89
87
 
90
- String source = params.getString("source");
91
- Boolean singlePage = params.hasKey("singlePage") ? params.getBoolean("singlePage") : false;
92
- float maxZoom = params.hasKey("maxZoom") ? Double.valueOf(params.getDouble("maxZoom")).floatValue() : 5;
88
+ var recyclerView = (PdfRendererRecyclerView) layout.getChildAt(0);
89
+
90
+ var source = params.getString("source");
91
+ var singlePage = params.hasKey("singlePage") && params.getBoolean("singlePage");
92
+ var maxZoom = params.hasKey("maxZoom") ? Double.valueOf(params.getDouble("maxZoom")).floatValue() : 5;
93
93
 
94
94
  if (source != null) {
95
95
  recyclerView.updateSource(source);
@@ -106,8 +106,7 @@ public class PdfRendererViewManager extends SimpleViewManager<ViewGroup> {
106
106
 
107
107
  @ReactProp(name = "distanceBetweenPages")
108
108
  public void setDistanceBetweenPages(ViewGroup layout, @Nullable float distanceBetweenPages) {
109
- PdfRendererRecyclerView recyclerView = (PdfRendererRecyclerView) layout.getChildAt(0);
109
+ var recyclerView = (PdfRendererRecyclerView) layout.getChildAt(0);
110
110
  recyclerView.setDistanceBetweenPages(distanceBetweenPages);
111
111
  }
112
-
113
112
  }
@@ -1,17 +1,44 @@
1
1
  import { ViewProps } from 'react-native';
2
- export declare type PdfRendererViewPropsType = ViewProps & {
2
+ export type PdfRendererViewPropsType = {
3
+ /**
4
+ * Styles to be applied to the View.
5
+ *
6
+ * Note: Border radius is not supported.
7
+ */
8
+ style?: ViewProps['style'];
9
+ /**
10
+ * Path to a file stored on device.
11
+ *
12
+ * Ex.: `file:///path/to/file.pdf`
13
+ */
3
14
  source?: string;
4
- distanceBetweenPages: number;
5
- maxZoom: number;
6
- singlePage: boolean;
15
+ /**
16
+ * Distance in `DPI` between pages.
17
+ *
18
+ * Default: 16
19
+ */
20
+ distanceBetweenPages?: number;
21
+ /**
22
+ * Max zoom scale.
23
+ *
24
+ * Default: 5
25
+ */
26
+ maxZoom?: number;
27
+ /**
28
+ * (Experimental)
29
+ *
30
+ * Renders only the first page without scroll. (useful for display thumbnail).
31
+ *
32
+ * Default: false
33
+ */
34
+ singlePage?: boolean;
35
+ /**
36
+ * Invoked on pages scroll.
37
+ *
38
+ * @param page current page number
39
+ * @param totalPages total pages number
40
+ */
7
41
  onPageChange?: (page: number, totalPages: number) => void;
8
42
  };
9
- declare const PdfRendererView: {
10
- (props: PdfRendererViewPropsType): JSX.Element;
11
- defaultProps: {
12
- maxZoom: number;
13
- distanceBetweenPages: number;
14
- singlePage: boolean;
15
- };
16
- };
43
+ declare const PdfRendererView: ({ onPageChange, style, source, singlePage, distanceBetweenPages, maxZoom, }: PdfRendererViewPropsType) => JSX.Element;
17
44
  export default PdfRendererView;
@@ -1,29 +1,4 @@
1
- "use strict";
2
1
  // MIT License
3
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
- if (k2 === undefined) k2 = k;
5
- var desc = Object.getOwnPropertyDescriptor(m, k);
6
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
- desc = { enumerable: true, get: function() { return m[k]; } };
8
- }
9
- Object.defineProperty(o, k2, desc);
10
- }) : (function(o, m, k, k2) {
11
- if (k2 === undefined) k2 = k;
12
- o[k2] = m[k];
13
- }));
14
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
15
- Object.defineProperty(o, "default", { enumerable: true, value: v });
16
- }) : function(o, v) {
17
- o["default"] = v;
18
- });
19
- var __importStar = (this && this.__importStar) || function (mod) {
20
- if (mod && mod.__esModule) return mod;
21
- var result = {};
22
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
23
- __setModuleDefault(result, mod);
24
- return result;
25
- };
26
- Object.defineProperty(exports, "__esModule", { value: true });
27
2
  // Copyright (c) 2023 Douglas Nassif Roma Junior
28
3
  // Permission is hereby granted, free of charge, to any person obtaining a copy
29
4
  // of this software and associated documentation files (the "Software"), to deal
@@ -40,35 +15,33 @@ Object.defineProperty(exports, "__esModule", { value: true });
40
15
  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
41
16
  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
42
17
  // SOFTWARE.
43
- const react_1 = __importStar(require("react"));
44
- const react_native_1 = require("react-native");
45
- const PdfRendererNative = (0, react_native_1.requireNativeComponent)('RNPdfRendererView');
46
- const styles = react_native_1.StyleSheet.create({
18
+ import React, { useCallback, useMemo } from 'react';
19
+ import { requireNativeComponent, StyleSheet, } from 'react-native';
20
+ const PdfRendererNative = requireNativeComponent('RNPdfRendererView');
21
+ const styles = StyleSheet.create({
47
22
  default: {
48
23
  backgroundColor: 'gray',
49
24
  flex: 1,
50
25
  },
51
26
  });
52
- const PdfRendererView = (props) => {
53
- const { onPageChange, style, source, singlePage, maxZoom, ...others } = props;
54
- const viewStyles = (0, react_1.useMemo)(() => [styles.default, style, {
27
+ const PdfRendererView = ({ onPageChange, style, source, singlePage = false, distanceBetweenPages = 16, maxZoom = 5, }) => {
28
+ const viewStyles = useMemo(() => [
29
+ styles.default,
30
+ style,
31
+ {
55
32
  // See https://github.com/douglasjunior/react-native-pdf-renderer#limitations
56
33
  borderRadius: null,
57
- }], [style]);
58
- const handlePageChange = (0, react_1.useCallback)((event) => {
34
+ },
35
+ ], [style]);
36
+ const handlePageChange = useCallback((event) => {
59
37
  onPageChange?.(event.nativeEvent.position, event.nativeEvent.total);
60
38
  }, [onPageChange]);
61
- const params = (0, react_1.useMemo)(() => ({
39
+ const params = useMemo(() => ({
62
40
  source,
63
41
  singlePage,
64
42
  maxZoom,
65
43
  }), [source, singlePage, maxZoom]);
66
- return (react_1.default.createElement(PdfRendererNative, { ...others, style: viewStyles, params: params, onPageChange: handlePageChange }));
67
- };
68
- PdfRendererView.defaultProps = {
69
- maxZoom: 5,
70
- distanceBetweenPages: 16,
71
- singlePage: false,
44
+ return (React.createElement(PdfRendererNative, { distanceBetweenPages: distanceBetweenPages, style: viewStyles, params: params, onPageChange: handlePageChange }));
72
45
  };
73
- exports.default = PdfRendererView;
46
+ export default PdfRendererView;
74
47
  //# sourceMappingURL=PdfRendererView.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"PdfRendererView.js","sourceRoot":"","sources":["../src/PdfRendererView.tsx"],"names":[],"mappings":";AAAA,cAAc;;;;;;;;;;;;;;;;;;;;;;;;;AAEd,gDAAgD;AAEhD,+EAA+E;AAC/E,gFAAgF;AAChF,+EAA+E;AAC/E,4EAA4E;AAC5E,wEAAwE;AACxE,2DAA2D;AAE3D,iFAAiF;AACjF,kDAAkD;AAElD,6EAA6E;AAC7E,2EAA2E;AAC3E,8EAA8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AK,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC;IACtE,CAAC,EACD,CAAC,YAAY,CAAC,CACf,CAAC;IAEF,MAAM,MAAM,GAAG,OAAO,CACpB,GAAG,EAAE,CAAC,CAAC;QACL,MAAM;QACN,UAAU;QACV,OAAO;KACR,CAAC,EACF,CAAC,MAAM,EAAE,UAAU,EAAE,OAAO,CAAC,CAC9B,CAAC;IAEF,OAAO,CACL,oBAAC,iBAAiB,IAChB,oBAAoB,EAAE,oBAAoB,EAC1C,KAAK,EAAE,UAAU,EACjB,MAAM,EAAE,MAAM,EACd,YAAY,EAAE,gBAAgB,GAC9B,CACH,CAAC;AACJ,CAAC,CAAC;AAEF,eAAe,eAAe,CAAC"}
package/dist/index.js CHANGED
@@ -1,10 +1,4 @@
1
- "use strict";
2
1
  // MIT License
3
- var __importDefault = (this && this.__importDefault) || function (mod) {
4
- return (mod && mod.__esModule) ? mod : { "default": mod };
5
- };
6
- Object.defineProperty(exports, "__esModule", { value: true });
7
- exports.default = void 0;
8
2
  // Copyright (c) 2023 Douglas Nassif Roma Junior
9
3
  // Permission is hereby granted, free of charge, to any person obtaining a copy
10
4
  // of this software and associated documentation files (the "Software"), to deal
@@ -21,6 +15,5 @@ exports.default = void 0;
21
15
  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
16
  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
17
  // SOFTWARE.
24
- var PdfRendererView_1 = require("./PdfRendererView");
25
- Object.defineProperty(exports, "default", { enumerable: true, get: function () { return __importDefault(PdfRendererView_1).default; } });
18
+ export { default } from './PdfRendererView';
26
19
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA,cAAc;;;;;;AAEd,gDAAgD;AAEhD,+EAA+E;AAC/E,gFAAgF;AAChF,+EAA+E;AAC/E,4EAA4E;AAC5E,wEAAwE;AACxE,2DAA2D;AAE3D,iFAAiF;AACjF,kDAAkD;AAElD,6EAA6E;AAC7E,2EAA2E;AAC3E,8EAA8E;AAC9E,yEAAyE;AACzE,gFAAgF;AAChF,gFAAgF;AAChF,YAAY;AAEZ,qDAA0C;AAAlC,2HAAA,OAAO,OAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc;AAEd,gDAAgD;AAEhD,+EAA+E;AAC/E,gFAAgF;AAChF,+EAA+E;AAC/E,4EAA4E;AAC5E,wEAAwE;AACxE,2DAA2D;AAE3D,iFAAiF;AACjF,kDAAkD;AAElD,6EAA6E;AAC7E,2EAA2E;AAC3E,8EAA8E;AAC9E,yEAAyE;AACzE,gFAAgF;AAChF,gFAAgF;AAChF,YAAY;AAEZ,OAAO,EAAC,OAAO,EAAC,MAAM,mBAAmB,CAAC"}
package/package.json CHANGED
@@ -1,11 +1,10 @@
1
1
  {
2
2
  "name": "react-native-pdf-renderer",
3
3
  "displayName": "React-Native Pdf Renderer",
4
- "version": "1.1.1",
4
+ "version": "1.3.0",
5
5
  "description": "⚛ A zoomable, blazing fast, zero dependencies, pure native, typed PDF Renderer for Android and iOS.",
6
6
  "main": "dist",
7
7
  "license": "MIT",
8
- "private": false,
9
8
  "repository": {
10
9
  "type": "git",
11
10
  "url": "https://github.com/douglasjunior/react-native-pdf-renderer.git"
@@ -35,23 +34,23 @@
35
34
  "react-native": ">=0.60"
36
35
  },
37
36
  "devDependencies": {
37
+ "react": "18.2.0",
38
+ "react-native": "0.74.1",
38
39
  "@babel/core": "^7.20.0",
39
40
  "@babel/preset-env": "^7.20.0",
40
41
  "@babel/runtime": "^7.20.0",
41
- "@react-native-community/eslint-config": "^3.2.0",
42
- "@tsconfig/react-native": "^2.0.2",
43
- "@types/jest": "^29.2.1",
44
- "@types/react": "^18.0.24",
42
+ "@react-native/babel-preset": "0.74.83",
43
+ "@react-native/eslint-config": "0.74.83",
44
+ "@react-native/metro-config": "0.74.83",
45
+ "@react-native/typescript-config": "0.74.83",
46
+ "@types/react": "^18.2.6",
45
47
  "@types/react-test-renderer": "^18.0.0",
46
- "babel-jest": "^29.2.1",
48
+ "babel-jest": "^29.6.3",
47
49
  "eslint": "^8.19.0",
48
- "jest": "^29.2.1",
49
- "metro-react-native-babel-preset": "0.73.8",
50
- "prettier": "^2.4.1",
51
- "react": "18.2.0",
52
- "react-native": "0.71.4",
50
+ "jest": "^29.6.3",
51
+ "prettier": "2.8.8",
53
52
  "react-test-renderer": "18.2.0",
54
- "typescript": "4.8.4"
53
+ "typescript": "5.0.4"
55
54
  },
56
55
  "types": "dist"
57
56
  }