我遇到了同样的问题,确实没有插件可以正确显示 PDF。
所以我做了不同的事情:我在本地下载 PDF,然后让操作系统使用其默认应用程序打开 PDF。这样一来,PDF 应用程序就会接管,并且该应用程序通常包含我们可能需要的所有内容。
为了请求打开文件,我使用了文件打开器 2 插件:https://github.com/pwlin/cordova-plugin-file-opener2。
这是我的代码:
var me = this;
var directory = null;
// Request permission to store a file
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fs) {
// The URL defined by cordova according to the operating system
directory = fs.root.nativeURL;
// Creating a temporary file in this memory area
fs.root.getFile(fileName, {
create: true,
exclusive: false
}, function(fileEntry) {
// Opening a channel to write to this file
fileEntry.createWriter(function(fileWriter) {
var sliceSize = 1024;
var byteCharacters = atob(me.cordovaBinaryValue);
var bytesLength = byteCharacters.length;
var slicesCount = Math.ceil(bytesLength / sliceSize);
var byteArrays = new Array(slicesCount);
for (var sliceIndex = 0; sliceIndex < slicesCount; ++sliceIndex) {
var begin = sliceIndex * sliceSize;
var end = Math.min(begin + sliceSize, bytesLength);
var bytes = new Array(end - begin);
for (var offset = begin, i = 0; offset < end; ++i, ++offset) {
bytes[i] = byteCharacters[offset].charCodeAt(0);
}
byteArrays[sliceIndex] = new Uint8Array(bytes);
}
// Creating our binary PDF structure object
var binaryData = new Blob(byteArrays, {
type: 'application/pdf'
});
// Write the content in the pdf file
fileWriter.write(binaryData);
fileWriter.onwriteend = function(e) {
// Now that the file is locally registered, you have to open it with the OS
cordova.plugins.fileOpener2.open(
directory + fileName, 'application/pdf', {
error: function(e) {
alert('Error status: ' + e.status + ' - Error message: ' + e.message);
},
success: function() {
// Normally we have nothing to do here since it is the moment when the pdf opens
}
});
};
// Write error in the file
fileWriter.onerror = function(e) {
alert('Write failed: ' + e.toString());
};
});
},
function(err) {
alert('error getting file! ' + err);
});
},
// No writing permission
function(err) {
alert('error getting persistent fs! ' + err);
});
希望对你有帮助。