简体   繁体   中英

Writing PDF on local storage using JsPdf in PhoneGap

I am trying to write a html converted pdf file via jspdf but its not working here is the js method

function gotFileWriter(writer) {
    writer.onwriteend = function(evt) {
        console.log("contents of file now 'some sample text'");
        writer.truncate(11);
        writer.onwriteend = function(evt) {



            console.log("contents of file now 'some sample'");
            writer.seek(4);var pdfOut;
        var pdf = new jsPDF('p','pt','a4');
        pdf.addHTML(document.body,function() {
        pdfOut = pdf.output('datauri');
                });
         // this alerts shows "Undefined"
        alert(pdfOut);
        writer.write(pdfOut);



            writer.onwriteend = function(evt){
                console.log("contents of file now 'some different text'");
            }
        };
    };
            var pdfOut;
        var pdf = new jsPDF('p','pt','a4');
        pdf.addHTML(document.body,function() {
        pdfOut = pdf.output('datauri');
                });
        // this alerts shows "Undefined"
            alert(pdfOut);
        writer.write(pdfOut);


}

Both alerts shows "Undefined".If I create it as .txt file it shows empty file and if i save it as .pdf if creates corrupted file. JsPdf is working fine because I can see pdf.output('datauri'); 's result in base64 string on log...

The addHTML call is async, so you have to wait for completion before trying to use the pdf output. Also, with a FileWriter an "arraybuffer" output is best suitable.

Try this:

    function gotFileWriter(writer) {
      writer.onwriteend = function (evt) {
        console.log("contents of file now 'some sample text'");
        // ...
      };
      var pdf = new jsPDF('p', 'pt', 'a4');
      pdf.addHTML(document.body, function () {
        writer.write(pdf.output('arraybuffer'));
      });
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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