Blob 构造函数在 android 中不起作用(不支持所有 Web 浏览器)。快速的解决方案是,使用 Phonegap 文件编写器创建 PDF 文件。以下是对jsPDF.js文件的更改:
output = function (type, options) {
var undef, data, length, array, i, blob;
switch (type) {
case undef:
return buildDocument();
case 'save':
if (navigator.getUserMedia) {
if (window.URL === undefined) {
return API.output('dataurlnewwindow');
} else if (window.URL.createObjectURL === undefined) {
return API.output('dataurlnewwindow');
}
}
data = buildDocument();
write(data, options);
break;
case 'datauristring':
case 'dataurlstring':
return 'data:application/pdf;base64,' + btoa(buildDocument());
case 'datauri':
case 'dataurl':
document.location.href = 'data:application/pdf;base64,' + btoa(buildDocument());
break;
case 'dataurlnewwindow':
window.open('data:application/pdf;base64,' + btoa(buildDocument()));
break;
default:
throw new Error('Output type "' + type + '" is not supported.');
}
// @TODO: Add different output options
};
将 CLI 中的 cordova 文件插件作为“cordova plugin add org.apache.cordova.file”添加到您的项目中。
接下来使用 phonegap filewriter API实现write()函数,如下所示:
write = function (data, filename) {
var PERSISTENT = window.PERSISTENT || LocalFileSystem.PERSISTENT;
window.requestFileSystem(PERSISTENT, 0, gotFS, fail);
function gotFS(fileSystem) {
fileSystem.root.getFile(filename, {create: true, exclusive: false}, gotFileEntry, fail);
}
function gotFileEntry(fileEntry) {
fileEntry.createWriter(gotFileWriter, fail);
}
function gotFileWriter(writer) {
writer.write(data);
}
function fail(error) {
console.log(error.code);
}
}