用expo在本机中打开PDF文件

时间:2019-03-04 23:11:29

标签: react-native pdf expo

我正在将React Native与Expo一起使用,并且正在尝试打开pdf文件。我的资产文件夹中已经有文件。我宁愿不脱离博览会。

我尝试使用react-native-pdf,例如:

import React, { Component } from 'react';
import { StyleSheet, Dimensions, View } from 'react-native';

import Pdf from 'react-native-pdf';

export default class PDFExample extends Component {
  render() {
    const source = {uri:'bundle-assets://test.pdf'};
    return (
      <View style={styles.container}>
        <Pdf
          source={source}
          style={styles.pdf} />
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'flex-start',
    alignItems: 'center',
    marginTop: 25,
  },
  pdf: {
    flex:1,
    width:Dimensions.get('window').width,
  }
});

但是没有成功。发生许多错误,我对这个软件包失去了信心。所以我正在寻找一个更简单的解决方案。

问题:
如何在另一个应用程序中打开资产文件。就像让ios或android选择哪个应用程序将打开pdf文件。我可以只使用expo中的托管代码而不使用分离吗?

1 个答案:

答案 0 :(得分:0)

通过yarn或npm安装以下库:
yarn add react-native-webview expo-file-system expo-constants rn-pdf-reader-js

然后您可以像这样显示 PDF:

import React from ‘react’
import { ScrollView, View, } from ‘react-native’
import PDFReader from ‘rn-pdf-reader-js’

export default class Publications extends React.Component {
  render() {
      return (
          <PDFReader
               source={{
               uri: ‘http://www.africau.edu/images/default/sample.pdf’,
               }}
          />
       )
    }
}
相关问题