react-native-pdf 6.5.0 → 6.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js DELETED
@@ -1,467 +0,0 @@
1
- /**
2
- * Copyright (c) 2017-present, Wonday (@wonday.org)
3
- * All rights reserved.
4
- *
5
- * This source code is licensed under the MIT-style license found in the
6
- * LICENSE file in the root directory of this source tree.
7
- */
8
-
9
- 'use strict';
10
- import React, {Component} from 'react';
11
- import PropTypes from 'prop-types';
12
- import {
13
- requireNativeComponent,
14
- View,
15
- Platform,
16
- ViewPropTypes,
17
- StyleSheet,
18
- Image,
19
- Text
20
- } from 'react-native';
21
-
22
- import ReactNativeBlobUtil from 'react-native-blob-util'
23
-
24
- const SHA1 = require('crypto-js/sha1');
25
- import PdfView from './PdfView';
26
-
27
- export default class Pdf extends Component {
28
-
29
- static propTypes = {
30
- ...ViewPropTypes,
31
- source: PropTypes.oneOfType([
32
- PropTypes.shape({
33
- uri: PropTypes.string,
34
- cache: PropTypes.bool,
35
- cacheFileName: PropTypes.string,
36
- expiration: PropTypes.number,
37
- }),
38
- // Opaque type returned by require('./test.pdf')
39
- PropTypes.number,
40
- ]).isRequired,
41
- page: PropTypes.number,
42
- scale: PropTypes.number,
43
- minScale: PropTypes.number,
44
- maxScale: PropTypes.number,
45
- horizontal: PropTypes.bool,
46
- spacing: PropTypes.number,
47
- password: PropTypes.string,
48
- renderActivityIndicator: PropTypes.func,
49
- enableAntialiasing: PropTypes.bool,
50
- enableAnnotationRendering: PropTypes.bool,
51
- enablePaging: PropTypes.bool,
52
- enableRTL: PropTypes.bool,
53
- fitPolicy: PropTypes.number,
54
- trustAllCerts: PropTypes.bool,
55
- singlePage: PropTypes.bool,
56
- onLoadComplete: PropTypes.func,
57
- onPageChanged: PropTypes.func,
58
- onError: PropTypes.func,
59
- onPageSingleTap: PropTypes.func,
60
- onScaleChanged: PropTypes.func,
61
- onPressLink: PropTypes.func,
62
-
63
- // Props that are not available in the earlier react native version, added to prevent crashed on android
64
- accessibilityLabel: PropTypes.string,
65
- importantForAccessibility: PropTypes.string,
66
- renderToHardwareTextureAndroid: PropTypes.string,
67
- testID: PropTypes.string,
68
- onLayout: PropTypes.bool,
69
- accessibilityLiveRegion: PropTypes.string,
70
- accessibilityComponentType: PropTypes.string,
71
- };
72
-
73
- static defaultProps = {
74
- password: "",
75
- scale: 1,
76
- minScale: 1,
77
- maxScale: 3,
78
- spacing: 10,
79
- fitPolicy: 2, //fit both
80
- horizontal: false,
81
- page: 1,
82
- enableAntialiasing: true,
83
- enableAnnotationRendering: true,
84
- enablePaging: false,
85
- enableRTL: false,
86
- trustAllCerts: true,
87
- usePDFKit: true,
88
- singlePage: false,
89
- onLoadProgress: (percent) => {
90
- },
91
- onLoadComplete: (numberOfPages, path) => {
92
- },
93
- onPageChanged: (page, numberOfPages) => {
94
- },
95
- onError: (error) => {
96
- },
97
- onPageSingleTap: (page, x, y) => {
98
- },
99
- onScaleChanged: (scale) => {
100
- },
101
- onPressLink: (url) => {
102
- },
103
- };
104
-
105
- constructor(props) {
106
-
107
- super(props);
108
- this.state = {
109
- path: '',
110
- isDownloaded: false,
111
- progress: 0,
112
- isSupportPDFKit: -1
113
- };
114
-
115
- this.lastRNBFTask = null;
116
-
117
- }
118
-
119
- componentDidUpdate(prevProps) {
120
-
121
- const nextSource = Image.resolveAssetSource(this.props.source);
122
- const curSource = Image.resolveAssetSource(prevProps.source);
123
-
124
- if ((nextSource.uri !== curSource.uri)) {
125
- // if has download task, then cancel it.
126
- if (this.lastRNBFTask) {
127
- this.lastRNBFTask.cancel(err => {
128
- this._loadFromSource(this.props.source);
129
- });
130
- this.lastRNBFTask = null;
131
- } else {
132
- this._loadFromSource(this.props.source);
133
- }
134
- }
135
- }
136
-
137
- componentDidMount() {
138
- this._mounted = true;
139
- if (Platform.OS === "ios") {
140
- const PdfViewManagerNative = require('react-native').NativeModules.PdfViewManager;
141
- PdfViewManagerNative.supportPDFKit((isSupportPDFKit) => {
142
- if (this._mounted) {
143
- this.setState({isSupportPDFKit: isSupportPDFKit ? 1 : 0});
144
- }
145
- });
146
- }
147
- this._loadFromSource(this.props.source);
148
- }
149
-
150
- componentWillUnmount() {
151
- this._mounted = false;
152
- if (this.lastRNBFTask) {
153
- this.lastRNBFTask.cancel(err => {
154
- });
155
- this.lastRNBFTask = null;
156
- }
157
-
158
- }
159
-
160
- _loadFromSource = (newSource) => {
161
-
162
- const source = Image.resolveAssetSource(newSource) || {};
163
-
164
- let uri = source.uri || '';
165
- // first set to initial state
166
- if (this._mounted) {
167
- this.setState({isDownloaded: false, path: '', progress: 0});
168
- }
169
- const filename = source.cacheFileName || SHA1(uri) + '.pdf';
170
- const cacheFile = ReactNativeBlobUtil.fs.dirs.CacheDir + '/' + filename;
171
-
172
- if (source.cache) {
173
- ReactNativeBlobUtil.fs
174
- .stat(cacheFile)
175
- .then(stats => {
176
- if (!Boolean(source.expiration) || (source.expiration * 1000 + stats.lastModified) > (new Date().getTime())) {
177
- if (this._mounted) {
178
- this.setState({path: cacheFile, isDownloaded: true});
179
- }
180
- } else {
181
- // cache expirated then reload it
182
- this._prepareFile(source);
183
- }
184
- })
185
- .catch(() => {
186
- this._prepareFile(source);
187
- })
188
-
189
- } else {
190
- this._prepareFile(source);
191
- }
192
- };
193
-
194
- _prepareFile = async (source) => {
195
-
196
- try {
197
- if (source.uri) {
198
- let uri = source.uri || '';
199
-
200
- const isNetwork = !!(uri && uri.match(/^https?:\/\//));
201
- const isAsset = !!(uri && uri.match(/^bundle-assets:\/\//));
202
- const isBase64 = !!(uri && uri.match(/^data:application\/pdf;base64/));
203
-
204
- const filename = source.cacheFileName || SHA1(uri) + '.pdf';
205
- const cacheFile = ReactNativeBlobUtil.fs.dirs.CacheDir + '/' + filename;
206
-
207
- // delete old cache file
208
- this._unlinkFile(cacheFile);
209
-
210
- if (isNetwork) {
211
- this._downloadFile(source, cacheFile);
212
- } else if (isAsset) {
213
- ReactNativeBlobUtil.fs
214
- .cp(uri, cacheFile)
215
- .then(() => {
216
- if (this._mounted) {
217
- this.setState({path: cacheFile, isDownloaded: true, progress: 1});
218
- }
219
- })
220
- .catch(async (error) => {
221
- this._unlinkFile(cacheFile);
222
- this._onError(error);
223
- })
224
- } else if (isBase64) {
225
- let data = uri.replace(/data:application\/pdf;base64,/i, '');
226
- ReactNativeBlobUtil.fs
227
- .writeFile(cacheFile, data, 'base64')
228
- .then(() => {
229
- if (this._mounted) {
230
- this.setState({path: cacheFile, isDownloaded: true, progress: 1});
231
- }
232
- })
233
- .catch(async (error) => {
234
- this._unlinkFile(cacheFile);
235
- this._onError(error)
236
- });
237
- } else {
238
- if (this._mounted) {
239
- this.setState({
240
- path: uri.replace(/file:\/\//i, ''),
241
- isDownloaded: true,
242
- });
243
- }
244
- }
245
- } else {
246
- this._onError(new Error('no pdf source!'));
247
- }
248
- } catch (e) {
249
- this._onError(e)
250
- }
251
-
252
-
253
- };
254
-
255
- _downloadFile = async (source, cacheFile) => {
256
-
257
- if (this.lastRNBFTask) {
258
- this.lastRNBFTask.cancel(err => {
259
- });
260
- this.lastRNBFTask = null;
261
- }
262
-
263
- const tempCacheFile = cacheFile + '.tmp';
264
- this._unlinkFile(tempCacheFile);
265
-
266
- this.lastRNBFTask = ReactNativeBlobUtil.config({
267
- // response data will be saved to this path if it has access right.
268
- path: tempCacheFile,
269
- trusty: this.props.trustAllCerts,
270
- })
271
- .fetch(
272
- source.method ? source.method : 'GET',
273
- source.uri,
274
- source.headers ? source.headers : {},
275
- source.body ? source.body : ""
276
- )
277
- // listen to download progress event
278
- .progress((received, total) => {
279
- this.props.onLoadProgress && this.props.onLoadProgress(received / total);
280
- if (this._mounted) {
281
- this.setState({progress: received / total});
282
- }
283
- });
284
-
285
- this.lastRNBFTask
286
- .then(async (res) => {
287
-
288
- this.lastRNBFTask = null;
289
-
290
- if (res && res.respInfo && res.respInfo.headers && !res.respInfo.headers["Content-Encoding"] && !res.respInfo.headers["Transfer-Encoding"] && res.respInfo.headers["Content-Length"]) {
291
- const expectedContentLength = res.respInfo.headers["Content-Length"];
292
- let actualContentLength;
293
-
294
- try {
295
- const fileStats = await ReactNativeBlobUtil.fs.stat(res.path());
296
-
297
- if (!fileStats || !fileStats.size) {
298
- throw new Error("FileNotFound:" + source.uri);
299
- }
300
-
301
- actualContentLength = fileStats.size;
302
- } catch (error) {
303
- throw new Error("DownloadFailed:" + source.uri);
304
- }
305
-
306
- if (expectedContentLength != actualContentLength) {
307
- throw new Error("DownloadFailed:" + source.uri);
308
- }
309
- }
310
-
311
- this._unlinkFile(cacheFile);
312
- ReactNativeBlobUtil.fs
313
- .cp(tempCacheFile, cacheFile)
314
- .then(() => {
315
- if (this._mounted) {
316
- this.setState({path: cacheFile, isDownloaded: true, progress: 1});
317
- }
318
- this._unlinkFile(tempCacheFile);
319
- })
320
- .catch(async (error) => {
321
- throw error;
322
- });
323
- })
324
- .catch(async (error) => {
325
- this._unlinkFile(tempCacheFile);
326
- this._unlinkFile(cacheFile);
327
- this._onError(error);
328
- });
329
-
330
- };
331
-
332
- _unlinkFile = async (file) => {
333
- try {
334
- await ReactNativeBlobUtil.fs.unlink(file);
335
- } catch (e) {
336
-
337
- }
338
- }
339
-
340
- setNativeProps = nativeProps => {
341
- if (this._root){
342
- this._root.setNativeProps(nativeProps);
343
- }
344
- };
345
-
346
- setPage( pageNumber ) {
347
- if ( (pageNumber === null) || (isNaN(pageNumber)) ) {
348
- throw new Error('Specified pageNumber is not a number');
349
- }
350
- this.setNativeProps({
351
- page: pageNumber
352
- });
353
- }
354
-
355
- _onChange = (event) => {
356
-
357
- let message = event.nativeEvent.message.split('|');
358
- //__DEV__ && console.log("onChange: " + message);
359
- if (message.length > 0) {
360
- if (message.length > 5) {
361
- message[4] = message.splice(4).join('|');
362
- }
363
- if (message[0] === 'loadComplete') {
364
- this.props.onLoadComplete && this.props.onLoadComplete(Number(message[1]), this.state.path, {
365
- width: Number(message[2]),
366
- height: Number(message[3]),
367
- },
368
- message[4]&&JSON.parse(message[4]));
369
- } else if (message[0] === 'pageChanged') {
370
- this.props.onPageChanged && this.props.onPageChanged(Number(message[1]), Number(message[2]));
371
- } else if (message[0] === 'error') {
372
- this._onError(new Error(message[1]));
373
- } else if (message[0] === 'pageSingleTap') {
374
- this.props.onPageSingleTap && this.props.onPageSingleTap(Number(message[1]), Number(message[2]), Number(message[3]));
375
- } else if (message[0] === 'scaleChanged') {
376
- this.props.onScaleChanged && this.props.onScaleChanged(Number(message[1]));
377
- } else if (message[0] === 'linkPressed') {
378
- this.props.onPressLink && this.props.onPressLink(message[1]);
379
- }
380
- }
381
-
382
- };
383
-
384
- _onError = (error) => {
385
-
386
- this.props.onError && this.props.onError(error);
387
-
388
- };
389
-
390
- render() {
391
- if (Platform.OS === "android" || Platform.OS === "ios" || Platform.OS === "windows") {
392
- return (
393
- <View style={[this.props.style,{overflow: 'hidden'}]}>
394
- {!this.state.isDownloaded?
395
- (<View
396
- style={styles.progressContainer}
397
- >
398
- {this.props.renderActivityIndicator
399
- ? this.props.renderActivityIndicator(this.state.progress)
400
- : <Text>{`${(this.state.progress * 100).toFixed(2)}%`}</Text>}
401
- </View>):(
402
- Platform.OS === "android" || Platform.OS === "windows"?(
403
- <PdfCustom
404
- ref={component => (this._root = component)}
405
- {...this.props}
406
- style={[{flex:1,backgroundColor: '#EEE'}, this.props.style]}
407
- path={this.state.path}
408
- onChange={this._onChange}
409
- />
410
- ):(
411
- this.props.usePDFKit && this.state.isSupportPDFKit === 1?(
412
- <PdfCustom
413
- ref={component => (this._root = component)}
414
- {...this.props}
415
- style={[{backgroundColor: '#EEE',overflow: 'hidden'}, this.props.style]}
416
- path={this.state.path}
417
- onChange={this._onChange}
418
- />
419
- ):(<PdfView
420
- {...this.props}
421
- style={[{backgroundColor: '#EEE',overflow: 'hidden'}, this.props.style]}
422
- path={this.state.path}
423
- onLoadComplete={this.props.onLoadComplete}
424
- onPageChanged={this.props.onPageChanged}
425
- onError={this._onError}
426
- onPageSingleTap={this.props.onPageSingleTap}
427
- onScaleChanged={this.props.onScaleChanged}
428
- onPressLink={this.props.onPressLink}
429
- />)
430
- )
431
- )}
432
- </View>);
433
- } else {
434
- return (null);
435
- }
436
-
437
-
438
- }
439
- }
440
-
441
-
442
- if (Platform.OS === "android") {
443
- var PdfCustom = requireNativeComponent('RCTPdf', Pdf, {
444
- nativeOnly: {path: true, onChange: true},
445
- })
446
- } else if (Platform.OS === "ios") {
447
- var PdfCustom = requireNativeComponent('RCTPdfView', Pdf, {
448
- nativeOnly: {path: true, onChange: true},
449
- })
450
- } else if (Platform.OS === "windows") {
451
- var PdfCustom = requireNativeComponent('RCTPdf', Pdf, {
452
- nativeOnly: {path: true, onChange: true},
453
- })
454
- }
455
-
456
-
457
- const styles = StyleSheet.create({
458
- progressContainer: {
459
- flex: 1,
460
- justifyContent: 'center',
461
- alignItems: 'center'
462
- },
463
- progressBar: {
464
- width: 200,
465
- height: 2
466
- }
467
- });
package/index.js.flow DELETED
@@ -1,63 +0,0 @@
1
- /**
2
- * @flow strict
3
- */
4
-
5
- import { Component } from 'react';
6
-
7
- import type { Node } from 'react';
8
- import type { FormField, Methods } from 'rn-fetch-blob';
9
- import type { ViewStyleProp } from 'react-native/Libraries/StyleSheet/StyleSheet'
10
-
11
- export type AssetId = number;
12
-
13
- export type FitWidth = 0;
14
- export type FitHeight = 1;
15
- export type FitBoth = 2;
16
-
17
- export type Source = {
18
- body?: string | FormField[],
19
- cache?: boolean,
20
- cacheFileName?: string,
21
- expiration?: number,
22
- headers?: { [key: string]: string },
23
- method?: Methods,
24
- uri: string
25
- };
26
-
27
- export type TableContent = {
28
- children: TableContent[],
29
- mNativePtr: number,
30
- pageIdx: number,
31
- title: string,
32
- };
33
-
34
- export type Props = {
35
- renderActivityIndicator?: (progress: number) => Node,
36
- enableAnnotationRendering?: boolean,
37
- enableAntialiasing?: boolean,
38
- enablePaging?: boolean,
39
- enableRTL?: boolean,
40
- fitPolicy?: FitWidth | FitHeight | FitBoth,
41
- horizontal?: boolean,
42
- maxScale?: number,
43
- minScale?: number,
44
- singlePage?: boolean,
45
- onError?: (error: Error) => void,
46
- onLoadComplete?: (numberOfPages: number, path: string, size: { height: number, width: number }, tableContents: ?TableContent[]) => void,
47
- onLoadProgress?: (percent: number) => void,
48
- onPageChanged?: (page: number, numberOfPages: number) => void,
49
- onPageSingleTap?: (page: number, x: number, y: number) => void,
50
- onScaleChanged?: (scale: number) => void,
51
- onPressLink?: (url: string) => void,
52
- page?: number,
53
- password?: string,
54
- scale?: number,
55
- source: AssetId | Source,
56
- spacing?: number,
57
- style?: ViewStyleProp,
58
- testID?: string
59
- };
60
-
61
- declare export default class Pdf extends Component<Props> {
62
- setPage: (pageNumber: number) => void;
63
- }