简体   繁体   English

在基于phonegap的应用程序上使用jspdf生成客户端pdf

[英]Generate client-side pdf with jspdf on phonegap based apps

i try to generate a pdf from local datas. 我尝试从本地数据生成pdf。

I had problems with the ArrayBuffer() and the Uint8Array objects. 我在ArrayBuffer()和Uint8Array对象上遇到了问题。 The solution was to add a js implemention i found in the internet. 解决方案是添加我在互联网上找到的js实现。

Now there is a error on this line: 现在这行有一个错误:

E/Web Console(21515): Uncaught TypeError: Illegal constructor at file:///android_asset/www/libs/jspdf.js:973

This is the line: 这是一行:

blob = new Blob([array], {type: "application/pdf"});

I added BlobBuilder.js and Blob.js (like in the jspdf example). 我添加了BlobBuilder.js和Blob.js(就像jspdf示例中一样)。

In general, is it possible to to that with jspdf? 总的来说,用jspdf可以做到吗? (i found a lot of problems with jspdf) (我发现jspdf有很多问题)

How can i solve this problem? 我怎么解决这个问题?

What can i do to generate pdfs on browser, android and ios..? 我该怎么做才能在浏览器,Android和iOS上生成PDF。

Thanks for help and have a nice day :-) 感谢您的帮助,祝您有美好的一天:-)

try  
{
    blob = new Blob([data], {type: "application/pdf"});
    console.debug("case 1");
}
catch (e)  
{
    window.BlobBuilder = window.BlobBuilder ||
                   window.WebKitBlobBuilder ||
                      window.MozBlobBuilder ||
                      window.MSBlobBuilder;
    if (e.name == 'TypeError' && window.BlobBuilder)  
    {
        var bb = new BlobBuilder();
        bb.append(data);
        blob = bb.getBlob("application/pdf");
        console.debug("case 2");
    }
    else if (e.name == "InvalidStateError")  
    {
         // InvalidStateError (tested on FF13 WinXP)
         blob = new Blob([array], {type: "application/pdf"});
         console.debug("case 3");
    }
    else  
    {
        // We're screwed, blob constructor unsupported entirely   
        console.debug("Errore");
    }
}

Blob constructor doesn't work in android (no support for all the web browsers). Blob构造函数在android中不起作用(不支持所有网络浏览器)。 The quick solution is, use Phonegap filewriter to create PDF file. 快速解决方案是,使用Phonegap文件编写器创建PDF文件。 Below is the changes to jsPDF.js file: 以下是对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
        };

Add cordova file plugin from CLI as "cordova plugin add org.apache.cordova.file" to your project. 从CLI将cordova文件插件添加为“ cordova plugin add org.apache.cordova.file”到您的项目。

Next implement write() function using phonegap filewriter API like below: 接下来使用如下的phonegap文件编写器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);
 }
}

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

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