问题描述
我一直在尝试使用react native打开pdf文件,但是我尝试的每个库都会产生错误.我尝试了下面的代码:
I have been trying to open a pdf file using react native, but every library that I try generates an error. I tried the code below:
import React, { Component } from 'react';
import Pdf from 'react-native-pdf';
class OpenBGReport extends Component {
render() {
const source = {uri:'http://samples.leanpub.com/thereactnativebook-sample.pdf',cache:true};
return (
<View style={styles.container}>
<Pdf
source={source}
onLoadComplete={(numberOfPages,filePath)=>{
console.log(`number of pages: ${numberOfPages}`);
}}
onPageChanged={(page,numberOfPages)=>{
console.log(`current page: ${page}`);
}}
onError={(error)=>{
console.log(error);
}}
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,
height:Dimensions.get('window').height,
}
});
export default OpenBGReport;
但是,在安装react-native-pdf后,我的项目停止工作并返回: null不是一个对象(正在评估"rnfetchblob.documentdir")
However, after installing react-native-pdf, my project stop working and returns: null is not an object (evaluating 'rnfetchblob.documentdir')
在react native中打开和显示URL的pdf的最佳方法是什么?
What is the best way to open and display a pdf from a URL in react native?
谢谢
推荐答案
Expo不支持安装本机模块,而 react-native-pdf
是本机模块.可能的解决方案:
Expo doesn't support installing native modules and react-native-pdf
is a native module. Possible solutions:
-
如果您确实要使用本机模块,则应使用
react-native-cli
或弹出expo项目.您可以在存储库常见问题解答"部分中找到更详细的答案( https://github.com/wonday/react-native-pdf#faq ).
If you really want to use native modules, you should use
react-native-cli
or eject expo project. You can find a more detailed answer in the repository FAQ section (https://github.com/wonday/react-native-pdf#faq).
您始终可以使用react-native的 Linking
API在手机浏览器中打开PDF文件.
You can always open PDF files in the phone browser by using react-native's Linking
API.
有一个软件包可以在博览会项目中显示PDF文件( https://github.com/xcarpentier/rn-pdf-reader-js ).从未使用过该软件包,而且由于它仅支持Android平台,因此它似乎也很粗略,而在iOS上,则需要使用WebView来显示pdf.此外,它在npm上没有大量下载,并且该项目本身很陈旧.
There is a package to display PDF files in expo projects (https://github.com/xcarpentier/rn-pdf-reader-js). Never used the package, and it also seems very sketchy as it supports only the Android platform, and on iOS, you would need to use WebView to display pdf. Also, it doesn't have a lot of downloads on npm and the project itself is prettry stale.
这篇关于在Expo React Native Project中从URL查看pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!