gpt4 book ai didi

pdf - react-native-pdf-view - 如何使用 base64 或 blob 填充 pdfView

转载 作者:行者123 更新时间:2023-12-01 11:24:37 25 4
gpt4 key购买 nike

我正在使用 react-native-pdf-view库,我在用 pdf 填充 PDFView 时遇到问题。

我的项目是如何工作的,我从服务器收到一个 base64 pdf,然后我使用库 react-native-fs 将其保存到 android 文件系统。像这样:
(这工作正常)

saveFile(filename){

var base64Image = this.state.imageBase64;

// create a path you want to write to
var path = RNFS.DocumentDirectoryPath + '/' + filename;

// write the file
RNFS.writeFile(path, base64Image, 'base64').then((success) => {
console.log('FILE WRITTEN!');
this.setState(
{pdf_dirPath: path}
);
this._display_fileAttachment()
})
.catch((err) => {
console.log(err.message);
});
}

然后我尝试用这个填充 pdf View :
<PDFView
ref={(pdf)=>{this.pdfView = pdf;}}
src={"path/To/base64/pdf"}
style={styles.pdf}
/>

问题

react-native-pdf-view 是否必须采用文件位置,还是可以采用 base64 pdf 或 blob。

如果可以使用 base64 或 blob,请解释或提供有关如何执行的示例代码。
谢谢

备注: This StackOverflow question非常相似,但我需要知道如何以何种形式从文件系统中保存或检索 base64 以及如何填充 pdf。

最佳答案

似乎有一种更简单的方法可以做到这一点。只需告诉 RNFletchBlob 像这样直接保存到磁盘。请注意,您稍后必须清理文件。

RNFetchBlob
.config({
// add this option that makes response data to be stored as a file.
fileCache : true,
appendExt : 'pdf'
})
.fetch('GET', 'http://www.example.com/file/example.zip', {
//some headers ..
})
.then((res) => {
// the temp file path
this.setState({fileLocation: res.path()});
console.log('The file saved to ', res.path())
})

然后后来

<PDFView 
ref={(pdf)=>{this.pdfView = pdf;}}
path={this.state.fileLocation}
onLoadComplete = {(pageCount)=>{
this.pdfView.setNativeProps({
zoom: 1.0
});
console.log("Load Complete. File has " + pageCount + " pages.");
}}
style={styles.pdf}/>

关于pdf - react-native-pdf-view - 如何使用 base64 或 blob 填充 pdfView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38658716/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com