如何通过React Native在第三方应用程序中打开pdf文件?

时间:2018-12-14 09:15:03

标签: react-native

我不想使用RN库来创建pdf查看器,我想单击文件图标,并询问我要在哪个已安装的应用程序中打开文件。

有没有允许我这样做的图书馆?

4 个答案:

答案 0 :(得分:2)

我无法使其与本地pdf文件的链接一起使用。然后,我进行了更多搜索,发现了react-native-file-viewer。有了这个,您可以打开pdf文件,就像这样:

 import FileViewer from "react-native-file-viewer";
 ... 


 try {
     await FileViewer.open(url, { showOpenWithDialog: true, showAppsSuggestions: true });
 } catch (e) {
     console.warn(TAG, "An error occurred", JSON.stringify(e));
 }

注意:传递给open函数的uri不需要任何file://前缀。就像正常的路径,例如RNFS.ExternalStorageDirectoryPath + "/mypdf.pdf"

答案 1 :(得分:1)

我在项目中使用了rn-fetch-blob。在那之后,做起来很容易;

Android

const android = RNFetchBlob.android;
android.actionViewIntent(path, 'application/pdf');

iOS

RNFetchBlob.ios.openDocument(path);

答案 2 :(得分:0)

可能,您可以从react native本身使用Linking来打开pdf文件,例如:

Linking.openURL(url).catch((err) => {
      console.log(err)
});

如果url是本地的,则可能要使用类似以下内容的东西:

import RNFS from 'react-native-fs';

file://${RNFS.DocumentDirectoryPath}/file.pdf

或者,如果在线,则网址可以是:

http://www.example.com/file.pdf

您也可以尝试使用此可用软件包:

react-native-view-pdf

在这里让我知道如何进行。如果这样可以解决您的问题,请投票。 :D)

答案 3 :(得分:0)

加载远程PDF的简便方法,

Linking.openURL(url).catch((err) => {
  console.log(err)

})