React Native下载打开pdf文件

本文原创首发于公众号:ReactNative开发圈,转载需注明出处。http://mp.weixin.qq.com/s/rS-4XppSPMKfSwrwUvmtLw

使用到的组件

  • react-native-fs 文件下载组件 GitHub - johanneslumpe/react-native-fs: Native filesystem access for react-native

  • react-native-pdf-view pdf显示组件 GitHub - cnjon/react-native-pdf-view: React Native PDF View



组件安装

cd到你的项目目录下,执行下面的命令安装

 
 
  1. npm install react-native-fs --save

  2. react-native link react-native-fs

  3. npm i react-native-pdf-view --save

  4. react-native link react-native-pdf-view

示例代码

首先下载pdf文件到本地,react-native-pdf-view组件现在只能支持显示手机本地pdf

 
 
  1.   var DownloadFileOptions = {

  2.            fromUrl: pdfDownloadURL,          // URL to download file from

  3.            toFile: this.pdfPath         // Local filesystem path to save the file to

  4.        }

  5.        var result = RNFS.downloadFile(DownloadFileOptions);

  6.        console.log(result);

  7.        var _this = this;

  8.        result.then(function (val) {

  9.            _this.setState({

  10.                isPdfDownload: true,

  11.            });

  12.        }, function (val) {

  13.            console.log('Error Result:' + JSON.stringify(val));

  14.        }

  15.        ).catch(function (error) {

  16.            console.log(error.message);

  17.        });

显示pdf,因为可能有多页,所以在打开第一页后,利用onLoadComplete事件获取到一共有多少页,然后动态加载后面的几页

 
 
  1. render() {

  2.        if (!this.state.isPdfDownload) {

  3.            return (

  4.                <View style={styles.container}>

  5.                    <Text>Downloading</Text>

  6.                </View>

  7.            );

  8.        }

  9.        var pages = [];

  10.        for (var i = 2; i < this.state.pageCount + 1; i++) {

  11.            pages.push(

  12.                <PDFView ref={(pdf) => { this.pdfView = pdf; } }

  13.                    key={"sop" + i}

  14.                    path={this.pdfPath}

  15.                    pageNumber={i}

  16.                    style={styles.pdf} />

  17.            );

  18.        }

  19.        return (

  20.            <ScrollView style={styles.pdfcontainer}>

  21.                <PDFView ref={(pdf) => { this.pdfView = pdf; } }

  22.                    key="sop"

  23.                    path={this.pdfPath}

  24.                    pageNumber={1}

  25.                    onLoadComplete={(pageCount) => {

  26.                        this.setState({ pageCount: pageCount });

  27.                        console.log(`pdf共有: ${pageCount}页`);

  28.                    } }

  29.                    style={styles.pdf} />

  30.                {pages.map((elem, index) => {

  31.                    return elem;

  32.                })}

  33.            </ScrollView>

  34.        )

  35.    }

完整代码: GitHub地址:https://github.com/forrest23/reacttest

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值