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

onLoadComplete prop is not getting fired[Android]

Open SimantaRajSarma opened this issue 1 year ago • 27 comments

What react-native version are you using? v0.76.5

What react-native-pdf version are you using? v6.7.6 What platform does your issue occur on? (android/ios/both) android Describe your issue as precisely as possible :

  1. Steps to reproduce the issue or to explain in which case you get the issue https://github.com/SimantaRajSarma/Openpaper
  2. Interesting logs nothing Join a screenshot or video of the problem on the simulator or device?

Show us the code you are using? https://github.com/SimantaRajSarma/Openpaper

SimantaRajSarma avatar Dec 19 '24 10:12 SimantaRajSarma

Also seeing this with latest version

wijskinner avatar Dec 19 '24 13:12 wijskinner

Also seeing this with latest version

are you saying it was working in previous versions?

SimantaRajSarma avatar Dec 19 '24 14:12 SimantaRajSarma

Hi, I also ran into this problem. It was working in RN version 75.2, not working on 76.5

sora-guilllermom avatar Dec 19 '24 19:12 sora-guilllermom

For our apps, reverting to 6.7.5 solves the issue.

We had ~6.7.5 in package.json and a recent PR ended up upgrading it to 6.7.6.

Removing the ~ fixed it.

nobelharvards avatar Dec 20 '24 10:12 nobelharvards

For our apps, reverting to 6.7.5 solves the issue.

We had ~6.7.5 in package.json and a recent PR ended up upgrading it to 6.7.6.

Removing the ~ fixed it.

didn't work though! can you share the code?

SimantaRajSarma avatar Dec 20 '24 10:12 SimantaRajSarma

onLoadComplete prop is not working in my case as well. I am also using react native v0.76.5 and react native pdf v6.7.6. it was working with react native v0.75.2 but with latest version it is not working.

kumarPraveen08 avatar Dec 27 '24 06:12 kumarPraveen08

onLoadComplete prop is not working in my case as well. I am also using react native v0.76.5 and react native pdf v6.7.6. it was working with react native v0.75.2 but with latest version it is not working.

it is working with react native v0.76.5 and react native pdf v6.7.5

kumarPraveen08 avatar Dec 27 '24 06:12 kumarPraveen08

Is there any solution?

mdaldoukhi avatar Dec 31 '24 11:12 mdaldoukhi

I believe I found a temporary solution: loadComplete and onPageChanged seem to dispatch at the same time, and loadComplete ends up being ignored. A simple delay on loadComplete seems to work.

react-native-pdf+6.7.6.patch

flavioftkode avatar Jan 03 '25 17:01 flavioftkode

Hello,

Just confirming what everyone here already knows, onLoadComplete does not fire when using react-native 0.76.5. However, downgrading react-native-pdf to 6.7.5 resolves the issue.

palmtown avatar Jan 08 '25 00:01 palmtown

We're also experiencing this issue and have had to downgrade to fix. A solution would be much appreciated :pray: Thank you

[My internal ref BDEV-1998]

violuke avatar Jan 13 '25 13:01 violuke

Just confirming, that i am also having this issue. A fix would be amazing, thank you

leahaeussermann avatar Jan 17 '25 08:01 leahaeussermann

Also happening here in the latest react-native-pdf 6.7.6 version with RN 0.76.5

lucianv avatar Jan 23 '25 11:01 lucianv

I used onLoadComplete to get number of pages. The issue also happened for React Native v0.76.6 and React Native PDF v6.7.6. I modified the code to use onPageChanged temporarily to get number of pages.

x9bbb avatar Jan 24 '25 19:01 x9bbb

Still happening in react-native-pdf 6.7.7 version with RN 0.76.7.

antoniogoulao avatar Feb 12 '25 16:02 antoniogoulao

Still happening in react-native-pdf 6.7.7 version with RN 0.76.7.

Bonitoflakes avatar Mar 28 '25 12:03 Bonitoflakes

+1 It is still occurring in react-native-pdf version 6.7.7 and react-native version 0.78.1.

palmtown avatar Mar 31 '25 06:03 palmtown

I believe I found a temporary solution: loadComplete and onPageChanged seem to dispatch at the same time, and loadComplete ends up being ignored. A simple delay on loadComplete seems to work.

react-native-pdf+6.7.6.patch

This worked for me.

Gautham495 avatar Apr 09 '25 08:04 Gautham495

I've tested the patch. It works on expo SDK 52 (react-native=0.76.9) but NOT on SDK 53 (react-native=0.79.2)

roniemartinez avatar May 21 '25 15:05 roniemartinez

Hello, currently having the same issue. Not only the onLoadComplete doesn't trigger but the pdf is only showing one page out of 11

Currently with react-native: 0.74.5 and react-native-pdf: 6.7.7. I tried react-native-pdf: 6.7.5 and it's not working either though.

OnizukaJS avatar Jun 11 '25 16:06 OnizukaJS

Won't work for everyone but my horrible workaround in case it helps someone: (basically using onPageChanged to grab the total number of pages instead of onLoadComplete)

{PLATFORM === 'android' ? (
                  // this is a bodge for now because android doesn't call `onLoadComplete` when the PDF is loaded
                  // so we don't know how many pages there are. Doing this works until react-native-pdf fixes upstream
                  // https://github.com/wonday/react-native-pdf/issues/940
                  // https://github.com/wonday/react-native-pdf/issues/899
                  <Pdf
                    // onLoadComplete={(pages) => setTotalPages(pages)}
                    onPageChanged={(_page, totalPages) => {
                      setTotalPages(totalPages);
                    }}
                    source={{ uri: pdfUrl, cache: false }}
                    page={previewPageIndex + 1}
                    // singlePage
                    enablePaging={false}
                    scrollEnabled={false}
                    showsHorizontalScrollIndicator={false}
                    showsVerticalScrollIndicator={false}
                    enableAnnotationRendering={false}
                    enableDoubleTapZoom={false}
                    style={{ flex: 1 }}
                  />
                ) : (
                  <Pdf
                    onLoadComplete={(pages) => setTotalPages(pages)}
                    source={{ uri: pdfUrl, cache: false }}
                    page={previewPageIndex + 1}
                    singlePage
                    enablePaging={false}
                    scrollEnabled={false}
                    showsHorizontalScrollIndicator={false}
                    showsVerticalScrollIndicator={false}
                    enableAnnotationRendering={false}
                    enableDoubleTapZoom={false}
                    style={{ flex: 1 }}
                  />
                )}

harveyappleton avatar Jul 01 '25 16:07 harveyappleton

Make sure to add trustAllCerts={false}

const source = { uri: 'http://samples.leanpub.com/thereactnativebook-sample.pdf', cache: true };

<Pdf
   trustAllCerts={false}
   source={source}
/>

babucarr32 avatar Jul 24 '25 16:07 babucarr32

Dang..................I got here

femibadmus-tolaram avatar Jul 26 '25 20:07 femibadmus-tolaram

patch for 6.7.7 version

react-native-pdf+6.7.7.patch

YOEL311 avatar Aug 05 '25 19:08 YOEL311

I believe I found a temporary solution: loadComplete and onPageChanged seem to dispatch at the same time, and loadComplete ends up being ignored. A simple delay on loadComplete seems to work.

react-native-pdf+6.7.6.patch

This works for me thanks

pf-abdullah avatar Sep 23 '25 14:09 pf-abdullah

FYI: On the latest version (7.0.3 for now), the patch is already applied :-)

https://github.com/wonday/react-native-pdf/blob/a7efe94414bc9cff392340ae11713769bf096b7e/android/src/main/java/org/wonday/pdf/PdfView.java#L112-L114

However, onLoadComplete can still be ignored on low-end devices, as a 10ms delay is not enough for them. One solution is to listen to both onLoadComplete and onPageChanged events.

import { useCallback, useState } from 'react'
import Pdf from 'react-native-pdf'

const Test = () => {
	const [numberOfPages, setNumberOfPages] = useState(1)

	const onLoadComplete = useCallback((newNumberOfPages: number) => {
		setNumberOfPages(newNumberOfPages)
	}, [])

	const onPageChanged = useCallback(
		(_: number, newNumberOfPages: number) => {
			// Prevents to set the same value
			if (numberOfPages !== newNumberOfPages) {
				setNumberOfPages(newNumberOfPages)
			}
		},
		[numberOfPages],
	)

	const onError = useCallback((error: object) => {
		console.error('Opening PDF error: ', error)
	}, [])

	return (
		<Pdf
			key="pdf-renderer"
			source={...}
			onLoadComplete={onLoadComplete}
			onPageChanged={onPageChanged}
			onError={onError}
		/>
	)
}

withSang avatar Oct 23 '25 11:10 withSang

FYI: On the latest version (7.0.3 for now), the patch is already applied :-)

react-native-pdf/android/src/main/java/org/wonday/pdf/PdfView.java

Lines 112 to 114 in a7efe94

if (dispatcher != null) { new Handler(Looper.getMainLooper()).postDelayed(() -> dispatcher.dispatchEvent(tce), 10); } However, onLoadComplete can still be ignored on low-end devices, as a 10ms delay is not enough for them. One solution is to listen to both onLoadComplete and onPageChanged events.

import { useCallback, useState } from 'react' import Pdf from 'react-native-pdf'

const Test = () => { const [numberOfPages, setNumberOfPages] = useState(1)

const onLoadComplete = useCallback((newNumberOfPages: number) => { setNumberOfPages(newNumberOfPages) }, [])

const onPageChanged = useCallback( (_: number, newNumberOfPages: number) => { // Prevents to set the same value if (numberOfPages !== newNumberOfPages) { setNumberOfPages(newNumberOfPages) } }, [numberOfPages], )

const onError = useCallback((error: object) => { console.error('Opening PDF error: ', error) }, [])

return ( <Pdf key="pdf-renderer" source={...} onLoadComplete={onLoadComplete} onPageChanged={onPageChanged} onError={onError} /> ) }

Updating to the latest version worked for me.

yigitkaya2001 avatar Oct 25 '25 12:10 yigitkaya2001