1 | # react-native-pdf
|
2 | [![npm](https://img.shields.io/npm/v/react-native-pdf.svg?style=flat-square)](https://www.npmjs.com/package/react-native-pdf)
|
3 |
|
4 | A react native PDF view component (cross-platform support)
|
5 |
|
6 | ### Feature
|
7 |
|
8 | * read a PDF from url/local file/asset and can cache it.
|
9 | * display horizontally or vertically
|
10 | * drag and zoom
|
11 | * double tap for zoom
|
12 | * support password protected pdf
|
13 |
|
14 | ### Installation
|
15 | We use [`rn-fetch-blob`](https://github.com/joltup/rn-fetch-blob) to handle file system access in this package,
|
16 | So you should install react-native-pdf and rn-fetch-blob
|
17 |
|
18 | ```bash
|
19 | npm install rn-fetch-blob --save
|
20 | npm install react-native-pdf --save
|
21 |
|
22 | react-native link rn-fetch-blob
|
23 | react-native link react-native-pdf
|
24 | ```
|
25 |
|
26 | ### FAQ
|
27 |
|
28 | Q1. After installation and running, I can not see the pdf file.
|
29 | A1: maybe you forgot to excute ```react-native link``` or it does not run correctly.
|
30 | You can add it manually. For detail you can see the issue [`#24`](https://github.com/wonday/react-native-pdf/issues/24) and [`#2`](https://github.com/wonday/react-native-pdf/issues/2)
|
31 |
|
32 | Q2. When running, it shows ```'Pdf' has no propType for native prop RCTPdf.acessibilityLabel of native type 'String'```
|
33 | A2. Your react-native version is too old, please upgrade it to 0.47.0+ see also [`#39`](https://github.com/wonday/react-native-pdf/issues/39)
|
34 |
|
35 | Q3. When I run the example app I get a white screen / the loading bar isn't progressing on IOS.
|
36 | A3. Check your uri, if you hit a pdf that is hosted on a `http` you will need to add an exception for the server hosting the pdf in the ios `info.plist`. Here is an example :
|
37 |
|
38 | ```
|
39 | <key>NSAppTransportSecurity</key>
|
40 | <dict>
|
41 | <key>NSExceptionDomains</key>
|
42 | <dict>
|
43 | <key>yourserver.com</key>
|
44 | <dict>
|
45 | <!--Include to allow subdomains-->
|
46 | <key>NSIncludesSubdomains</key>
|
47 | <true/>
|
48 | <!--Include to allow HTTP requests-->
|
49 | <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
|
50 | <true/>
|
51 | <!--Include to specify minimum TLS version-->
|
52 | <key>NSTemporaryExceptionMinimumTLSVersion</key>
|
53 | <string>TLSv1.1</string>
|
54 | </dict>
|
55 | </dict>
|
56 | </dict>
|
57 | ```
|
58 |
|
59 | Q4. why doesn't it work with react native expo?.
|
60 | A4. Expo does not support native module. you can read more expo caveats [`here`](https://facebook.github.io/react-native/docs/getting-started.html#caveats)
|
61 |
|
62 |
|
63 | ### ChangeLog
|
64 |
|
65 | v5.0.1
|
66 | 1. add paging support (ios and android)
|
67 | 2. add RTL support (ios)
|
68 | 3. fix position when set page (ios)
|
69 |
|
70 | v5.0.0 (**break change**)
|
71 | 1. use iOS PDFKit to show pdf (iOS SDK>=11)
|
72 | 2. use js+native to show pdf (iOS SDK<11, the same with 4.0.0)
|
73 | 3. support pdf with layers (iOS SDK>=11)
|
74 | 4. support pdf with links (iOS SDK>=11)
|
75 | 5. fix zoom (iOS SDK>=11)
|
76 |
|
77 |
|
78 | v4.0.0 (**break change**)
|
79 | 1. replace dependence lib ```react-native-fetch-blob``` with ```rn-fetch-blob```
|
80 | if you upgrade from an old version, you should
|
81 | ```
|
82 | react-native unlink react-native-fetch-blob
|
83 | npm uninstall react-native-fetch-blob
|
84 |
|
85 | npm install rn-fetch-blob --save
|
86 | react-native link rn-fetch-blob
|
87 | ```
|
88 |
|
89 | [[more]](https://github.com/wonday/react-native-pdf/releases)
|
90 |
|
91 | ### Example
|
92 |
|
93 | ```js
|
94 | /**
|
95 | * Copyright (c) 2017-present, Wonday (@wonday.org)
|
96 | * All rights reserved.
|
97 | *
|
98 | * This source code is licensed under the MIT-style license found in the
|
99 | * LICENSE file in the root directory of this source tree.
|
100 | */
|
101 |
|
102 | import React from 'react';
|
103 | import { StyleSheet, Dimensions, View } from 'react-native';
|
104 |
|
105 | import Pdf from 'react-native-pdf';
|
106 |
|
107 | export default class PDFExample extends React.Component {
|
108 | render() {
|
109 | const source = {uri:'http://samples.leanpub.com/thereactnativebook-sample.pdf',cache:true};
|
110 | //const source = require('./test.pdf'); // ios only
|
111 | //const source = {uri:'bundle-assets://test.pdf'};
|
112 |
|
113 | //const source = {uri:'file:///sdcard/test.pdf'};
|
114 | //const source = {uri:"data:application/pdf;base64,..."};
|
115 |
|
116 | return (
|
117 | <View style={styles.container}>
|
118 | <Pdf
|
119 | source={source}
|
120 | onLoadComplete={(numberOfPages,filePath)=>{
|
121 | console.log(`number of pages: ${numberOfPages}`);
|
122 | }}
|
123 | onPageChanged={(page,numberOfPages)=>{
|
124 | console.log(`current page: ${page}`);
|
125 | }}
|
126 | onError={(error)=>{
|
127 | console.log(error);
|
128 | }}
|
129 | style={styles.pdf}/>
|
130 | </View>
|
131 | )
|
132 | }
|
133 | }
|
134 |
|
135 | const styles = StyleSheet.create({
|
136 | container: {
|
137 | flex: 1,
|
138 | justifyContent: 'flex-start',
|
139 | alignItems: 'center',
|
140 | marginTop: 25,
|
141 | },
|
142 | pdf: {
|
143 | flex:1,
|
144 | width:Dimensions.get('window').width,
|
145 | }
|
146 | });
|
147 |
|
148 | ```
|
149 |
|
150 |
|
151 | ### Configuration
|
152 |
|
153 | | Property | Type | Default | Description | iOS | Android | FirstRelease |
|
154 | | ------------- |:-------------:|:----------------:| ------------------- | ------| ------- | ------------ |
|
155 | | source | object | not null | PDF source like {uri:xxx, cache:false}. see the following for detail.| ✔ | ✔ | <3.0 |
|
156 | | page | number | 1 | initial page index | ✔ | ✔ | <3.0 |
|
157 | | scale | number | 1.0 | zoom scale, 1<=scale<=3| ✔ | ✔ | <3.0 |
|
158 | | horizontal | bool | false | draw page direction, if you want to listen the orientation change, you can use [[react-native-orientation-locker]](https://github.com/wonday/react-native-orientation-locker)| ✔ | ✔ | <3.0 |
|
159 | | fitWidth | bool | false | if true fit the width of view, can not use fitWidth=true together with scale| ✔ | ✔ | <3.0, abandoned from 3.0 |
|
160 | | fitPolicy | number | 2 | 0:fit width, 1:fit height, 2:fit both(default)| ✔ | ✔ | 3.0 |
|
161 | | spacing | number | 10 | the breaker size between pages| ✔ | ✔ | <3.0 |
|
162 | | password | string | "" | pdf password, if password error, will call OnError() with message "Password required or incorrect password." | ✔ | ✔ | <3.0 |
|
163 | | style | object | {backgroundColor:"#eee"} | support normal view style, you can use this to set border/spacing color... | ✔ | ✔ | <3.0 |
|
164 | | activityIndicator | Component | <ProgressBar/> | when loading show it as an indicator, you can use your component| ✔ | ✔ | <3.0 |
|
165 | | activityIndicatorProps | object | {color:'#009900',progressTintColor:'#009900'} | activityIndicator props | ✔ | ✔ | 3.1 |
|
166 | | enableAntialiasing | bool | true | improve rendering a little bit on low-res screens, but maybe course some problem on Android 4.4, so add a switch | ✖ | ✔ | <3.0 |
|
167 | | enablePaging | bool | false | only show one page in screen | ✔ | ✔ | 5.0.1 |
|
168 | | enableRTL | bool | false | scroll page as "page3, page2, page1" | ✔ | ✖ | 5.0.1 |
|
169 | | onLoadProgress | function(percent) | null | callback when loading, return loading progress (0-1) | ✔ | ✔ | <3.0 |
|
170 | | onLoadComplete | function(numberOfPages, path, {width, height}) | null | callback when pdf load completed, return total page count and pdf local/cache path | ✔ | ✔ | <3.0 |
|
171 | | onPageChanged | function(page,numberOfPages) | null | callback when page changed ,return current page and total page count | ✔ | ✔ | <3.0 |
|
172 | | onError | function(error) | null | callback when error happened | ✔ | ✔ | <3.0 |
|
173 | | onPageSingleTap | function(page) | null | callback when page was single tapped | ✔ | ✔ | 3.0 |
|
174 | | onScaleChanged | function(scale) | null | callback when scale page | ✔ | ✔ | 3.0 |
|
175 |
|
176 | #### parameters of source
|
177 |
|
178 | | parameter | Description | default | iOS | Android |
|
179 | | ------------ | ----------- | ------- | --- | ------- |
|
180 | | uri | pdf source, see the forllowing for detail.| required | ✔ | ✔ |
|
181 | | cache | use cache or not | false | ✔ | ✔ |
|
182 | | expiration | cache file expired seconds (0 is not expired) | 0 | ✔ | ✔ |
|
183 | | method | request method when uri is a url | "GET" | ✔ | ✔ |
|
184 | | headers | request headers when uri is a url | {} | ✔ | ✔ |
|
185 |
|
186 | #### types of source.uri
|
187 |
|
188 | | Usage | Description | iOS | Android |
|
189 | | ------------ | ----------- | --- | ------- |
|
190 | | `{uri:"http://xxx/xxx.pdf"}` | load pdf from a url | ✔ | ✔ |
|
191 | | `{require("./test.pdf")}` | load pdf relate to js file (do not need add by xcode) | ✔ | ✖ |
|
192 | | `{uri:"bundle-assets://path/to/xxx.pdf"}` | load pdf from assets, the file should be at android/app/src/main/assets/path/to/xxx.pdf | ✖ | ✔ |
|
193 | | `{uri:"bundle-assets://xxx.pdf"}` | load pdf from assets, you must add pdf to project by xcode. this does not support folder. | ✔ | ✖ |
|
194 | | `{uri:"base64data"}` | load pdf from base64 string | ✔ | ✔ |
|
195 | | `{uri:"file:///absolute/path/to/xxx.pdf"}` | load pdf from local file system | ✔ | ✔ |
|
196 |
|
\ | No newline at end of file |