简体   繁体   English

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

[英]react-native-pdf-view - How to populate pdfView with base64 or blob

I am using the react-native-pdf-view library and I am having trouble populating the PDFView with a pdf. 我正在使用react-native-pdf-view库,我无法使用pdf填充PDFView。

How my project works is that I receive a base64 pdf from the server where I then save to the android file system by using the library react-native-fs like so: 我的项目如何工作是我从服务器收到base64 pdf,然后通过使用库react-native-fs保存到android文件系统,如下所示:
(This works fine) (这很好)

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);
   });
 }

I then try populate the pdf view with this: 然后我尝试用这个填充pdf视图:

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

Question

Does the react-native-pdf-view have to take a file location or can it take a base64 pdf or a blob. react-native-pdf-view是否必须采用文件位置,还是采用base64 pdf或blob。

If it can take a base64 or blob please explain or give sample code on how to do it. 如果它可以采用base64或blob,请解释或提供有关如何执行的示例代码。
Thanks 谢谢

Nb: This StackOverflow question is very similar but I need to know how in what form to save or retrieve the base64 from the file system and how to populate the pdf. Nb: 这个StackOverflow问题非常相似,但我需要知道如何从文件系统中保存或检索base64以及如何填充pdf。

For anyone having the same problem, here is the solution. 对于任何有相同问题的人,这是解决方案。

Solution

react-nativive-pdf-view must take the file path to the pdf_base64. react-nativive-pdf-view必须将文件路径带到pdf_base64。

Firstly, I used the react-native-fetch-blob to request the pdf base64 from the server.(Because RN fetch API does not yet support BLOBs). 首先,我使用react-native-fetch-blob从服务器请求pdf base64。(因为RN fetch API还不支持BLOB)。

Also I discovered that react-native-fetch-blob also has a FileSystem API which is way better documented and easier to understand than the 'react-native-fs' library. 此外,我发现react-native-fetch-blob还有一个FileSystem API,它比'react-native-fs'库更好地记录并更容易理解。 ( Check out its FileSystem API documentation ) 查看其FileSystem API文档

Receiving base64 pdf and saving it to a file path: 接收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)
   });
}

What I was doing wrong was instead of saving the pdf_base64Str to the file location like I did in the example above. 我做错了不是像我在上面的例子中那样将pdf_base64Str保存到文件位置。 I was saving it like this: 我像这样保存它:

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

which was wrong. 这是错的。

Populate PDF view with file path: 使用文件路径填充PDF视图:

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

It seems like there's a much easier way to do this. 似乎有一种更容易的方法来做到这一点。 Just tell RNFletchBlob to save directly to disk like so. 告诉RNFletchBlob直接保存到磁盘就像这样。 Note that you'll have to clean up the file later. 请注意,您以后必须清理该文件。

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())
  })

And then later 然后是

<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}/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM