5

モバイル Cordova アプリ内からjsPDF ライブラリ ( https://github.com/MrRio/jsPDF ) を使用して PDF を生成しようとしています。現在、Android 4.0.4 デバイスでアプリをテストしていますが、Windows モバイル 8 でも実行する必要があります。PDF ドキュメント内のテキストは正しく表示されますが、画像はスクランブルされています。下の画像を参照してください

生成されたPDFのイメージ

このページ ( https://coderwall.com/p/nc8hia ) は、Cordova で画像を表示する jsPDF に問題があることを示しているように見えましたが (コメントを参照)、著者はフォローアップを投稿しませんでした。Cordova で jsPDF を使用して、生成された PDF に画像を適切に追加できた人はいますか? 私のコードは以下のとおりです。支援やアドバイスをいただければ幸いです。

function demoReceipt() {
    var img = new Image();

    img.onError = function() {
        alert('Cannot load image: "' + url + '"');
    };
    img.onload = function() {
        createPdf2(img);
    };
    img.src = 'img/testlogo.png';
}



function createPdf2(myLogo) {
    //  var doc = new jsPDF('p', 'pt', 'jontype');

    var doc = new jsPDF('p', 'pt', 'letter');

    doc.setProperties({
        title : 'Fueling Receipt',
        author : 'Jon Hoffman',
        creater : 'Jon Hoffman'
    });

    doc.addImage(myLogo, 'PNG', 5, 5, 140, 30);
    doc.setFontSize(12);
    doc.text(10, 40, 'Sample PDF receipt');
    doc.setFontSize(8);
    doc.text(10, 45, 'Smaller text - new');

    var pdfOutput = doc.output();

    //NEXT SAVE IT TO THE DEVICE'S LOCAL FILE SYSTEM
    //Requires  cordova plugin add org.apache.cordova.file
    console.log("file system...");
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) {

        console.log(fileSystem.name);
        console.log(fileSystem.root.name);
        console.log(fileSystem.root.fullPath);

        fileSystem.root.getDirectory("myPDFs", {
            create : true,
            exclusive : false
        }, function(dir) {

            fileSystem.root.getFile("myPDFs/test.pdf", {
                create : true
            }, function(entry) {
                var fileEntry = entry;
                console.log(entry);

                entry.createWriter(function(writer) {
                    writer.onwrite = function(evt) {
                        console.log("write success");
                    };

                    console.log("writing to file");
                    writer.write(pdfOutput);
                }, function(error) {
                    console.log(error);
                });

            }, function(error) {
                console.log(error);
            });
        }, function(error) {
        });
    }, function(event) {
        console.log(evt.target.error.code);
    });
}
4

1 に答える 1