Professional Documents
Culture Documents
How To Build React-Native Bridge and Get PDF Viewer by Maksym Rusynyk
How To Build React-Native Bridge and Get PDF Viewer by Maksym Rusynyk
Search Medium
Listen Share
React Native allows to build native applications using JavaScript language and has
vast amount of components and functionality available. But some components or
features are not available by default. And sometimes it is necessary to improve
the performance of some components. In these cases native implementation can
be used.
This article describes how to make a bridge between JavaScript and a native
implementation, and provides an example how to build a PDF Viewer for React
Native to render PDF documents.
Getting started
First prepare a project. Open a terminal and run:
$ create-react-native-app react-native-PDFView
$ cd react-native-PDFView
$ yarn eject
and open it in the IDE. I prefer Atom, therefore in terminal I can do:
$ atom .
Java implementation
In Android Studio:
PDFViewPackage
According to official documentation PDFViewPackage should implements
ReactPackage and missing createNativeModules and createViewManagers methods
should be added:
public List<ViewManager>
createViewManagers(ReactApplicationContext reactContext) {
return Arrays.<ViewManager>asList(new
PDFViewManager(reactContext));
}
}
PDFViewManager
PDFViewManager should extends SimpleViewManager<PDFView> and implements
missing methods:
@Override
public String getName() {
return REACT_CLASS;
}
@Override
public PDFView createViewInstance(ThemedReactContext context)
{
if (pdfView == null) {
pdfView = new PDFView(context);
}
return pdfView;
}
}
with the error that PDFView should extend View , which has to be fixed by
implementing PDFView .
PDFView
At this moment all classes are ready and PDFViewPackage can be registered.
Register PDFViewPackage
In MainApplication.java there is the getPackages method. Right below
MainReactPackage the PDFViewPackage package can be registered:
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new PDFViewPackage()
);
}
JavaScript implementation
In Atom create a new folder PDFView and create two files:
• index.js
• RNPDFView.js
PDFView/RNPDFView.js
This is main file where the PDFView react native component should be required
and component interface has to be defined:
const componentInterface = {
name: 'PDFView',
propTypes: {
...ViewPropTypes,
},
};
At this moment only ViewPropTypes are used to check that the component is
properly defined and can be used. All other properties will be added later.
PDFView/index.js
This file is optional, and can be used as wrapper component, to handle
error/loading states, or to use flow types, etc:
/* @flow */
import React from 'react';
constructor(props: Props) {
super(props);
}
render() {
return <RNPDFView {...this.props} />;
}
}
App.js
The bridge is ready and can be imported and used:
...
return (
<View style={styles.container}>
<PDFView style={styles.pdfView} />
</View>
);
Restart the app and you will get a green rectangle like this:
Actually that’s all for the bridge, except properties and callbacks. And if they are
not required it is already possible to implement the component in Java.
• resource — String value to define the resource to render. Can be url, filePath or
base64 data
const componentInterface = {
name: 'PDFView',
propTypes: {
onError: PropTypes.func,
onLoad: PropTypes.func,
resource: PropTypes.string,
resourceType: PropTypes.string,
...ViewPropTypes,
},
};
type Props = {
onError: (Error) => void,
onLoad: () => void,
resource: string,
resourceType: 'url' | 'base64' | 'file',
};
And define method onError to pass only nativeEvent from the component:
constructor(props: Props) {
super(props);
this.onError = this.onError.bind(this);
}
render() {
const { onError, ...remainingProps } = this.props;
return <RNPDFView {...remainingProps} onError={this.onError} />;
}
Also default properties can be defined to simplify the usage of the component:
static defaultProps = {
onError: () => {},
onLoad: () => {},
resourceType: 'url',
};
@ReactProp(name = "resource")
public void setResource(PDFView pdfView, String resource) {
pdfView.setResource(resource);
}
@ReactProp(name = "resourceType")
public void setResourceType(PDFView pdfView, String resourceType)
{
pdfView.setResourceType(resourceType);
}
@Override
public void onAfterUpdateTransaction(PDFView pdfView) {
super.onAfterUpdateTransaction(pdfView);
pdfView.render();
}
@Override
public void onDropViewInstance(PDFView pdfView) {
super.onDropViewInstance(pdfView);
pdfView.onDrop();
}
@Override
public void loadComplete(int numberOfPages) {
reactNativeEvent("onLoad", null);
}
@Override
public void onError(Exception ex) {
reactNativeEvent("onError", "error: " + t.getMessage());
}
And in PDFViewManager
Results
The implementation of the bridge and PDF Viewer is ready. Define the document
you would like to render:
<View style={styles.container}>
<PDFView
style={styles.pdfView}
React Native Pdf JavaScript Native App Components
onError={error => console.log('onError', error) }
onLoad={() => console.log('onLoad') }
resource="http://www.pdf995.com/samples/pdf.pdf" />
</View>
..reload the application and instead of green rectangle you will get PDF
document:
Follow
You don’t need GUI — how to control iOS Simulator from command line
Back in past, if you used a computer, you mostly used a command line user interface. Today
for development you use GUI (Xcode, Simulator…
119 3
4.6K 17
56K 253
57
15
2.5K 29
Lists
Modern Marketing
34 stories · 163 saves
6.1K 59
18.5K 152
AL Anany
The ChatGPT Hype Is Over — Now Watch How Google Will Kill ChatGPT.
It never happens instantly. The business game is longer than you know.
11.2K 353
2.3K 29