簡體   English   中英

在基於phonegap的應用程序上使用jspdf生成客戶端pdf

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

我嘗試從本地數據生成pdf。

我在ArrayBuffer()和Uint8Array對象上遇到了問題。 解決方案是添加我在互聯網上找到的js實現。

現在這行有一個錯誤:

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

這是一行:

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

我添加了BlobBuilder.js和Blob.js(就像jspdf示例中一樣)。

總的來說,用jspdf可以做到嗎? (我發現jspdf有很多問題)

我怎么解決這個問題?

我該怎么做才能在瀏覽器,Android和iOS上生成PDF。

感謝您的幫助,祝您有美好的一天:-)

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構造函數在android中不起作用(不支持所有網絡瀏覽器)。 快速解決方案是,使用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文件編寫器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