Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
rn-view-pdf
Advanced tools
Library for displaying PDF documents in react-native
2.8.2
is used. It's also possible to override it and use 3.1.0-beta.1
(this version allows to handle links, etc. and will be used when Android PdfViewer stable version is released). To change the version, define it in your build file:buildscript {
ext {
...
pdfViewer = "3.1.0-beta.1"
}
...
}
ios - uses WKWebView. Targets iOS9.0 and above
zero NPM dependencies
$ npm install react-native-view-pdf --save
$ react-native link react-native-view-pdf
In your Xcode project directory open Podfile and add the following line:
pod 'RNPDF', :path => '../node_modules/react-native-view-pdf'
And install:
pod install
android/app/src/main/java/[...]/MainApplication.java
import com.rumax.reactnative.pdfviewer.PDFViewPackage;
to the imports at the top of the filenew PDFViewPackage()
to the list returned by the getPackages()
methodandroid/settings.gradle
:
include ':react-native-view-pdf'
project(':react-native-view-pdf').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-view-pdf/android')
android/app/build.gradle
:
compile project(':react-native-view-pdf')
The Android project tries to retrieve the following properties:
from the ext
object if you have one defined in your Android's project root (you can read more about it here). If not, it falls back to its current versions (check the gradle file for additional information).
N/A
Android | iOS |
---|---|
// With Flow type annotations (https://flow.org/)
import PDFView from 'react-native-view-pdf';
// Without Flow type annotations
// import PDFView from 'react-native-view-pdf/lib/index';
const resources = {
file: Platform.OS === 'ios' ? 'downloadedDocument.pdf' : '/sdcard/Download/downloadedDocument.pdf',
url: 'https://www.ets.org/Media/Tests/TOEFL/pdf/SampleQuestions.pdf',
base64: 'JVBERi0xLjMKJcfs...',
};
export default class App extends React.Component {
render() {
const resourceType = 'base64';
return (
<View style={{ flex: 1 }}>
{/* Some Controls to change PDF resource */}
<PDFView
fadeInDuration={250.0}
style={{ flex: 1 }}
resource={resources[resourceType]}
resourceType={resourceType}
onLoad={() => console.log(`PDF rendered from ${resourceType}`);}
onError={() => console.log('Cannot render PDF', error)}
/>
</View>
);
}
}
Use the demo project to:
Name | Android | iOS | Description |
---|---|---|---|
resource | ✓ | ✓ | A resource to render. It's possible to render PDF from file , url or base64 |
resourceType | ✓ | ✓ | Should correspond to resource and can be: file , url or base64 |
fileFrom | ✗ | ✓ | iOS ONLY: In case if resourceType is set to file , there are different way to search for it on iOS file system. Currently Documents and app’s bundle are supported. |
onLoad | ✓ | ✓ | Callback that is triggered when loading is completed |
onError | ✓ | ✓ | Callback that is triggered when loading has failed. And error is provided as a function parameter |
style | ✓ | ✓ | A style |
fadeInDuration | ✓ | ✓ | Fade in duration (in ms, defaults to 0.0) to smoothly fade the webview into view when pdf loading is completed |
urlProps | ✓ | ✓ | Extended properties for url type that allows to specify HTTP Method, HTTP headers and HTTP body |
onPageChanged | ✓ | ✗ | Callback that is invoked when page is changed. Provides active page and total pages information |
onScrolled | ✓ | ✓ | Callback that is invoked when PDF is scrolled. Provides offset value in a range between 0 and 1. Currently only 0 and 1 are supported. |
reload
Allows to reload the PDF document. This can be useful in such cases as network issues, document is expired, etc. To reload the document you will need a ref
to the component:
...
render() {
return (
<PDFView
...
ref={(ref) => this._pdfRed = ref} />
);
}
And trigger it by calling reloadPDF
:
reloadPDF = async () => {
const pdfRef = this._pdfRef;
if (!pdfRef) {
return;
}
try {
await pdfRef.reload();
} catch (err) {
console.err(err.message);
}
}
Or check the demo project which also includes this functionality.
On android for the file
type it is required to request permissions to
read/write. You can get more information in PermissionsAndroid
section from react native or Request App Permissions from android
documentation. Demo
project provides an example how to implement it using Java, check the MainActivity.java and AndroidManifest.xml files.
Before trying file
type in demo project, open sdcard/Download
folder in Device File Explorer
and store some downloadedDocument.pdf
document that you want to render.
On iOS, using resource file
will make the component lookup in two places. First, it will attempt to locate the file in the Bundle. If it cannot locate it there, it will search the Documents directory. For more information on the iOS filesystem access at runtime of an application please refer the official documentation.
Note here that the resource will always need to be a relative path from the Documents directory for example and also do NOT put the scheme in the path (so no file://.....
).
You can find an example of both usage of the Bundle and Documents directory for rendering a pdf from file
on iOS in the demo project.
In demo project you can also run the simple server to serve PDF file. To do this navigate to demo/utils/
and start the server
node server.js
. (Do not forget to set proper IP adress of the server
in demo/App.js
)
-keep class com.shockwave.**
FAQs
React native Pdf viewer implementation
We found that rn-view-pdf demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.