简体   繁体   中英

Error: cannot create document: File not in PDF format or corrupted, while converting base64 string to pdf using react-native-pdf

I am trying to convert a base64 string to pdf using https://www.npmjs.com/package/react-native-pdf

I am using "react-native": "0.64.0", Here is my try,

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,
  },
});

I read some article for this issues, this is a specific problem to react native for android,

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

I did not find a solution, I tried few suggestions, it did not work for me. Can anybody know, how to solve this issue for react native android,

Try replacing ' with " in URL, like this:

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

Just for reference here are some examples from 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..."};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM