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

2023-12-05

我正在使用反应本机 pdf-view库,我在用 pdf 填充 PDFView 时遇到问题。

我的项目的工作原理是,我从服务器接收一个 base64 pdf,然后使用该库将其保存到 android 文件系统反应本机FS像这样:
(这很好用)

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 视图:

<PDFView
  ref={(pdf)=>{this.pdfView = pdf;}}
  src={"path/To/base64/pdf"}
  style={styles.pdf}
 />

Question

React-native-pdf-view 是否必须获取文件位置,或者是否可以获取 Base64 pdf 或 blob。

如果它可以采用 base64 或 blob,请解释或提供有关如何执行此操作的示例代码。
Thanks

Nb: 这个 StackOverflow 问题非常相似,但我需要知道如何以何种形式从文件系统保存或检索 base64 以及如何填充 pdf。


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

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}/>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

react-native-pdf-view - 如何使用 base64 或 blob 填充 pdfView 的相关文章

随机推荐