【问题标题】:react-native-pdf-view - How to populate pdfView with base64 or blobreact-native-pdf-view - 如何使用 base64 或 blob 填充 pdfView
【发布时间】:2016-07-29 12:21:07
【问题描述】:

我正在使用 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 视图:

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

问题

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

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

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

【问题讨论】:

  • 看起来像source-code,它似乎不接受base64,你必须提供一个路径。为了解决这个问题,我会console.logpath/to/pdf 确认它已保存并且是一个好的 PDF。
  • @Kyle Finley 感谢您指出这一点。问题似乎是保存文件。查看这篇文章了解更多详情。 stackoverflow.com/questions/38662309/… 谢谢。

标签: pdf react-native pdf-viewer react-native-android react-native-fs


【解决方案1】:

似乎有一种更简单的方法可以做到这一点。只需告诉 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}/>

【讨论】:

    【解决方案2】:

    对于任何有同样问题的人,这里是解决方案。 解决方案

    react-native-pdf-view 必须取pdf_base64的文件路径。

    首先,我使用react-native-fetch-blob 从服务器请求pdf base64。 (因为 RN fetch API 还不支持 BLOB)。

    另外,我发现 react-native-fetch-blob 也有一个 FileSystem API,它比“react-native-fs”库更好地记录和更容易理解。 (Check out its FileSystem API documentation)

    接收base64 pdf并保存到文件路径:

    var RNFetchBlob = require('react-native-fetch-blob').default;
    
    const DocumentDir = RNFetchBlob.fs.dirs.DocumentDir;
    
    getPdfFromServer: function(uri_attachment, filename_attachment) {
       return new Promise((RESOLVE, REJECT) => {
    
          // Fetch attachment
          RNFetchBlob.fetch('GET', config.apiRoot+'/app/'+uri_attachment)
          .then((res) => {
           
              let base64Str = res.data;
              let pdfLocation = DocumentDir + '/' + filename_attachment;
    
              RNFetchBlob.fs.writeFile(pdfLocation, pdf_base64Str, 'base64');
              RESOLVE(pdfLocation);
          })
       }).catch((error) => {
           // error handling
           console.log("Error", error)
       });
    }
    

    我做错了,而不是像在上面的示例中那样将 pdf_base64Str 保存到文件位置。我是这样保存的:

    var pdf_base64= 'data:application/pdf;base64,'+pdf_base64Str;
    

    这是错误的。

    使用文件路径填充 PDF 视图:

    <PDFView
        ref={(pdf)=>{this.pdfView = pdf;}}
        src={pdfLocation}
        style={styles.pdf}
    />
            
    

    【讨论】:

    • 什么是“解决”功能?它似乎没有在任何地方定义,当我尝试运行你的代码时给了我一个错误。
    • @Pedram "Resolve() 返回一个使用给定值解析的 Promise 对象。 - developer.mozilla.org/en/docs/Web/JavaScript/Reference/…
    • 哦,我明白了。这只是当你的承诺得到解决时传入的回调。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-09-29
    • 2016-11-17
    • 2016-11-25
    • 2021-01-08
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    相关资源
    最近更新 更多