pdf下载,显示ios和android

时间:2016-07-31 23:27:18

标签: android ios cordova pdf hybrid-mobile-app

目前我正在从事混合应用。我收到base 64 string,我希望以PDF格式下载,并在用户点击下载按钮时打开它。我设法转换为pdf,参考以下链接 它将存储在设备的本地存储中。 在android文件中存储在内部存储中。但是对于ios,pdf不是本地存储中的accessibe,而ibooks没有识别该文件。如何在android和ibooks for ios的下载文件夹中提供pdf以获得更好的用户体验,并在下载完成后打开pdf文件?

convert base 64 to pdf blob

function b64toBlob(b64Data, contentType, sliceSize) {
var input = b64Data.replace(/\s/g, ''),
    byteCharacters = atob(input),
    byteArrays = [],
    offset, slice, byteNumbers, i, byteArray, blob;

contentType = contentType || '';
sliceSize = sliceSize || 512;

for (offset = 0; offset < byteCharacters.length; offset += sliceSize) {
    slice = byteCharacters.slice(offset, offset + sliceSize);

    byteNumbers = new Array(slice.length);
    for (i = 0; i < slice.length; i++) {
        byteNumbers[i] = slice.charCodeAt(i);
    }

    byteArray = new Uint8Array(byteNumbers);

    byteArrays.push(byteArray);
}

//Convert to blob. 
try {
    blob = new Blob(byteArrays, { type: contentType });
}
catch (e) {
    // TypeError old chrome, FF and Android browser
    window.BlobBuilder = window.BlobBuilder ||
                         window.WebKitBlobBuilder ||
                         window.MozBlobBuilder ||
                         window.MSBlobBuilder;
    if (e.name == 'TypeError' && window.BlobBuilder) {
        var bb = new BlobBuilder();
        for (offset = 0; offset < byteArrays.length; offset += 1) {
            bb.append(byteArrays[offset].buffer);
        }                    
        blob = bb.getBlob(contentType);
    }
    else if (e.name == "InvalidStateError") {
        blob = new Blob(byteArrays, {
            type: contentType
        });
    }
    else {
        return null;
    }
}

 return blob;
};

然后下载本身我们需要cordova文件插件:

 function pdfConversion(){
  alert("The paragraph was clicked.");
var fileName="test.pdf";
var fileToSave= b64toBlob(encodedString, 'application/pdf');
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS,    onFileSystemFail);
}
function gotFS(fileSystem) {
    fileSystem.root.getFile(fileName, {create: true}, onFileEntryRetrieved, onFileSystemFail);
}

function onFileEntryRetrieved(fileEntry) {
    console.log("file entry retrieved");
    fileEntry.createWriter(gotFileWriter, onFileSystemFail);
}
   function gotFileWriter (writer) {
      console.log("inside local file system"+JSON.stringify(writer));
      alert("JSON.stringify(writer)"+JSON.stringify(writer));
      console.log("contents of file now 'some sample text'");
        //writer.truncate(11);  
        writer.onwriteend = function(evt) {
            writer.write(fileToSave);
             writer.seek(4);
            }

  writer.onerror = function (e) {
      // you could hook this up with our global error handler, or pass in an error callback
      console.log('Write failed: ' + e.toString());
  };
  writer.write(fileToSave);
  window.open(fileName, '_blank');

   };

   function onFileSystemFail(error) {
        console.log(error.code);
    }

};

1 个答案:

答案 0 :(得分:-1)

在Android中,我们可以访问写入和阅读PDF。但在IOS中,Sandbox访问具有一定的阅读限制。

我设法使用编码字符串打开PDF,如下所示

window.open("data:application/pdf;base64,"+encodedString, "_blank","location=no,hidden=no,closebuttoncaption=Close");