react-native-pdf icon indicating copy to clipboard operation
react-native-pdf copied to clipboard

Android: `onScaleChanged` reports a scale change when the PDF view is resized (e.g. on device rotation)

Open GCyganek opened this issue 1 month ago • 0 comments

Hi, we are facing an issue at Expensify on Android, where onScaleChanged fires with a different scale when the PDF view is resized, e.g. when rotating the device between portrait and landscape, even though the user didn't zoom.

We hit this in our attachment carousel, which treats a scale different from 1 as "zoomed in". After rotating, the carousel arrows were hidden (because scale changes from 1 to a different value):

  • Original downstream issue: https://github.com/Expensify/App/issues/103207

Affected code

  • android/src/main/java/org/wonday/pdf/PdfView.java
    • onLayerDrawn(...) reports scaleChanged| + pageWidth / originalWidth
    • originalWidth is the page width of the first draw and is never updated
    • the event is emitted whenever the drawn page width or height changes

Expected behavior

onScaleChanged is only emitted when the zoom changes, and the reported scale is relative to the page size fitted to the current view size. Without zooming it stays 1 after rotating.

This is how iOS already behaves: it reports _pdfView.scaleFactor / _fixScaleFactor, recalculates _fixScaleFactor for the new view size on every resize, and only emits the event when that value changes.

Actual behavior

  • Portrait → landscape: onScaleChanged is called with ~2 without zooming
  • Landscape → portrait: onScaleChanged is called with ~0.5 without zooming

Root cause

With fit policies, AndroidPdfViewer recalculates the page size for the new view size when the view is resized, so the drawn page width changes. onLayerDrawn divides it by the page width of the first draw, so every resize is reported as a zoom change by the ratio of the new to the old view width.

Proposed fix

Report the viewer's zoom instead and only emit the event when it changes:

float zoom = getZoom();
if (lastZoom>0 && zoom!=lastZoom) {
    ...
    event.putString("message", "scaleChanged|"+zoom);
    ...
}
lastZoom = zoom;

getZoom() is relative to the page size fitted to the current view size, so it isn't affected by resizing, which matches the iOS behavior.

Validation

We confirmed the behavior in Expensify with temporary JS logging:

  • before:
    • portrait → landscape reported 2 without zooming
    • landscape → portrait reported 0.5 without zooming
  • after reporting getZoom():
    • scale changes are not reported to JS, same as on iOS when only changing device orientation, zooming via gestures works correctly

GCyganek avatar Oct 12 '26 14:10 GCyganek