douglasjunior / react-native-pdf-renderer

⚛ A zoomable, blazing fast, zero dependencies, pure native, typed PDF Renderer for Android and iOS.
MIT License
131 stars 3 forks source link

Zooming inside a large PDF file crashes on Android #26

Closed Maniae closed 3 days ago

Maniae commented 4 days ago

On Android, zooming inside a large/complex PDF file (over ~6Mo) causes the app to crash:

java.lang.RuntimeException: Canvas: trying to draw too large(134806560bytes) bitmap. android.graphics.RecordingCanvas.throwIfCannotDraw(RecordingCanvas.java:266) android.graphics.BaseRecordingCanvas.drawBitmap(BaseRecordingCanvas.java:94) android.graphics.drawable.BitmapDrawable.draw(BitmapDrawable.java:549) android.widget.ImageView.onDraw(ImageView.java:1449) android.view.View.draw(View.java:23351) android.view.View.updateDisplayListIfDirty(View.java:22210) android.view.View.draw(View.java:23076) android.view.ViewGroup.drawChild(ViewGroup.java:4544) androidx.recyclerview.widget.RecyclerView.drawChild(RecyclerView.java:5545) android.view.ViewGroup.dispatchDraw(ViewGroup.java:4302) com.github.douglasjunior.reactNativePdfRenderer.modules.PdfRendererRecyclerView.dispatchDraw(PdfRendererRecyclerView.java:156)

EDIT : This doesn't crash on version 1.2.0 (But the pdf rendering is kinda blurry)

douglasjunior commented 4 days ago

This is because now the Bitmap is created by multiplying the zoom scale:

https://github.com/douglasjunior/react-native-pdf-renderer/blob/85e5478a79c93333d5a8e0061c032a3b53029fe5/android/src/main/java/com/github/douglasjunior/reactNativePdfRenderer/modules/PdfRendererRecyclerView.java#L415-L416

The best solution would be to crop the canvas to only the visible area, but this could be complex to handle when the user navigates within the zoomed area.

For now, you can limit the zoom to a level that doesn't cause a crash.

douglasjunior commented 3 days ago

Android has a limitation on Bitmap size when rendering to a canvas: : https://stackoverflow.com/search?q=java.lang.RuntimeException%3A+Canvas%3A+trying+to+draw+too+large

To quickly solve this, I added a prop maxPageResolution to limit the page resolution when zooming on Android.

Available in version 1.4.0: https://github.com/douglasjunior/react-native-pdf-renderer/releases/tag/1.4.0