【问题标题】:Error: cannot create document: File not in PDF format or corrupted, while converting base64 string to pdf using react-native-pdf错误:无法创建文档:文件不是 PDF 格式或已损坏,同时使用 react-native-pdf 将 base64 字符串转换为 pdf
【发布时间】:2021-07-05 03:47:20
【问题描述】:

我正在尝试使用 https://www.npmjs.com/package/react-native-pdf 将 base64 字符串转换为 pdf

我正在使用“react-native”:“0.64.0”,这是我的尝试,

import React, {useState, useEffect} from 'react';
import {Button,Image,View,Platform,Alert,StyleSheet,Dimensions,Text,} from 'react-native';
import Pdf from 'react-native-pdf';

export default function ImagePickerCamera() {
  const source = {
    uri:
      'data:application/pdf;base64,/9j/4AAQSkZJRgA..............4R//Z',
    cache: true, // tried with cache: false, not works
  };
  return (
    <View style={styles.container}>
      <Text>test</Text>
      <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);
        }}
        onPressLink={uri => {
          console.log(`Link presse: ${uri}`);
        }}
        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,
  },
});

我阅读了一些关于这个问题的文章,这是一个特定的问题来响应原生的 android,

https://github.com/wonday/react-native-pdf/issues/174

我没有找到解决方案,我尝试了一些建议,但对我不起作用。 任何人都可以知道,如何解决这个问题反应原生android,

【问题讨论】:

  • 您确定您的网址有效吗?
  • 我想是的,它是一个有效的 URL,我正在使用 base64-image.de 将图像转换为 base64 字符串,如果我使用 pdf URI 效果很好,我正在尝试使用 base64 字符串跨度>

标签: javascript android reactjs react-native pdf


【解决方案1】:

尝试在 URL 中将 ' 替换为 ",如下所示:

const source = {uri:"data:application/pdf;base64,/9j/4AAQSkZJRgA..............4R//Z"};

这里有一些来自docs的示例供参考:

 const source = {uri:'http://samples.leanpub.com/thereactnativebook-sample.pdf',cache:true};

 const source = require('./test.pdf');  // ios only
 const source = {uri:'bundle-assets://test.pdf'};

 const source = {uri:'file:///sdcard/test.pdf'};
 const source = {uri:"data:application/pdf;base64,JVBERi0xLjcKJc..."};

【讨论】:

  • 我用 " " 试过了,还是不行。
  • 尝试完全删除cache
  • 你确定它会起作用吗?你为什么随便说点什么?
  • 我没试过;我只是告诉你我会怎么做。如果我有这个问题。如果这件事没有任何效果,那肯定意味着问题不在显示的代码中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-10
  • 2017-01-29
  • 2013-09-25
  • 1970-01-01
相关资源
最近更新 更多