簡體   English   中英

錯誤:無法創建文檔:文件不是 PDF 格式或損壞,同時使用 react-native-pdf 將 base64 字符串轉換為 pdf

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

我正在嘗試使用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 中將'替換為" ,如下所示:

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..."};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM