[英]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我正在尝试使用https://www.npmjs.com/package/react-native-pdf将 base64 字符串转换为 pdf
I am using "react-native": "0.64.0", Here is my try,我正在使用“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,
},
});
I read some article for this issues, this is a specific problem to react native for android,我为这个问题阅读了一些文章,这是针对 android 反应原生的特定问题,
https://github.com/wonday/react-native-pdf/issues/174 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,谁能知道,如何解决这个问题反应原生 android,
Try replacing '
with "
in URL, like this:尝试在 URL 中将'
替换为"
,如下所示:
const source = {uri:"data:application/pdf;base64,/9j/4AAQSkZJRgA..............4R//Z"};
Just for reference here are some examples from docs :仅供参考,这里有一些来自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.