我可以打开链接:https://www.orimi.com/pdf-test.pdf,但当我try 使用链接:http://94.23.154.57:8081/digital-cert/download-file?id=33FB3234-23EC-472B-AB6F-76436DDCFA45&view=1时,我无法打开它.我收到一条错误消息:Error: cannot create document: File not in PDF format or corrupted.我怀疑可能是http导致了问题,但服务器返回了PDF链接,我无法将其更改为HTTPS

import Pdf from 'react-native-pdf';
import {
  View,
} from 'react-native';

const PDFExample = ({route}) => {
  const source = {
    uri: 'http://94.23.154.57:8081/digital-cert/download-file?id=33FB3234-23EC-472B-AB6F-76436DDCFA45&view=1',
    cache: true,
  };
  return (
      <View style={styles.container}>
        <Pdf
          trustAllCerts={false}
          source={source}
          onLoadComplete={(numberOfPages, filePath) => {
            console.log(`Number of pages: ${numberOfPages}`);
          }}
          onPageChanged={(page, numberOfPages) => {
            console.log(`Current page: ${page}`);
          }}
          onError={error => {
            console.log(error);
          }}
          onPressLink={uri => {
            console.log(`Link pressed: ${uri}`);
          }}
          style={styles.pdf}
        />
      </View>
)
}

推荐答案

PDF插件无法打开损坏的格式的PDF,即使我try 下载并打开file://,但它不适用于我.

这也可能表明备份的PDF文件丢失或部分下载,导致PDF文件损坏的原因有很多.我找到了6个理由

  1. 下载过程中出现错误
  2. 使用不兼容的软件
  3. 由一些邮箱服务Provider 进行编码
  4. 硬盘问题
  5. 病毒攻击
  6. 用不同的软件打开同一文件

如果你想修复PDF,那么选中这docs由Adobe修复损坏的PDF.

如果你不想做所有这些事情,那么你也可以用另一种方式打开PDF.

我们有三个选项可以在Reaction Native中打开PDF,您已经完成了第一个选项,现在您可以 Select 其他两个选项

  • Reaction-Native-Webview

  • react native 文件查看器

如果您 Select webview,那么请勾选此answer by Aytek答案

你可以用file viewer打开pdf

Example:

 const path = FileViewer.open(path, { showOpenWithDialog: true }) // absolute-path-to-my-local-file.
  .then(() => {
    // success
  })
  .catch((error) => {
    // error
  });

React-native相关问答推荐

react 本机中的TextInput对齐中的长文本

类似Snapchat的Reaction Native筛选器

React native + react-query + onError 不捕获并处理错误?

找不到变量:firebase/react native(expo)上的 IDBIndex

Javascript setTimeout 立即在 React Native 中运行

react-navigation中的React-native android后退按钮

更改所选标签栏元素的底部边框 colored颜色

如何使用 AND/OR 运算符过滤列表/查询 AWS Amplify JavaScript GraphQL

如何在 Crashlytics (Fabrics) 中有效地对非致命异常进行分组?

如何以编程方式在 react-native 中截屏

如何在 React Native 中删除警告

如何在 react-native 中在图像周围添加阴影

Test suite无法运行 TypeError:Cannot read property ‘default’ of undefined

如何在react native中过滤对象数组?

React native + redux-persist:如何忽略键(黑名单)?

使用带有样式组件的动画

如何使用 Expo 在真实的 iOS 设备上运行应用程序?

如何将样式传递给 React-Native 中的容器组件

仅链接字体的方法

ListView 和 FlatList 有什么区别?