I recently added <ActivityIndicator>
in my code so as to display the loader while the external link loads in the <WebView>
.
This works fine for a lot of different links, except those with PDF, for example: http://www.pdf995.com/samples/pdf.pdf
With this, it shows the loader initially but then renders blank page. In iOS, I do see the page number when I try to scroll down. It just doesnt display the PDF contents.
If I remove startInLoadingState={true}
from the <WebView>
, it works fine but the loader is not displayed.
I need to get this working with the loader displayed in both - iOS and Android.
Expo Snack demo: https://snack.expo.io/rk8o0TSSE
Code:
import React from 'react';
import {
ActivityIndicator,
Dimensions,
WebView
} from 'react-native';
export default class InAppBrowser extends React.Component {
renderLoadingView() {
const dimensions = Dimensions.get('window');
const marginTop = dimensions.height/2 - 75;
return (
<ActivityIndicator
animating = {true}
color = '#0076BE'
size = 'large'
hidesWhenStopped={true}
style={{marginTop}}
/>
);
}
render() {
const uri = 'http://www.pdf995.com/samples/pdf.pdf';
return (
<WebView
renderLoading={this.renderLoadingView}
source={{uri}}
startInLoadingState={true}
/>
);
}
}
What am I missing here?
To add a PDF viewer in React Native, we can add a WebView and use Google Drive's PDF viewer. to add a WebView with the source set to an object with the uri set to the Google Drive PDF viewer URL. As a result, we should see the PDF viewer rendered in the WebView.
The very first and the easiest way of displaying the PDF file is to display it in the WebView. All you need to do is just put WebView in your layout and load the desired URL by using the webView. loadUrl() function. Now, run the application on your mobile phone and the PDF will be displayed on the screen.
React Native uses a JavaScript runtime, but the UI is not HTML and it doesn't use a WebView. You use JSX and React Native specific components to define the UI. It provides a native-level performance and look and feel but some UI parts have to be configured separately for iOS and Android.
For now, I am going with the following workaround:
I am embedding the remote PDF URL into Google Drive viewer URL. This works fine in iOS and Android and renders the ActivityIndicator
as well.
Demo snack with fix: https://snack.expo.io/H1XnDz9rN
import React from 'react';
import {
ActivityIndicator,
Dimensions,
WebView
} from 'react-native';
export default class InAppBrowser extends React.Component {
renderLoadingView() {
const dimensions = Dimensions.get('window');
const marginTop = dimensions.height/2 - 75;
return (
<ActivityIndicator
animating = {true}
color = '#0076BE'
size = 'large'
hidesWhenStopped={true}
style={{marginTop}}
/>
);
}
render() {
let uri = 'http://www.pdf995.com/samples/pdf.pdf';
if (/\.pdf$/.test(uri)) {
uri = `https://drive.google.com/viewerng/viewer?embedded=true&url=${uri}`;
}
return (
<WebView
renderLoading={this.renderLoadingView}
source={{uri}}
startInLoadingState={true}
/>
);
}
}
Source: https://github.com/rumax/react-native-PDFView/issues/40#issuecomment-410298445
P.S. I will change the accepted answer if a better answer comes around.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With