Reputation: 3050
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...
Upvotes: 0
Views: 1574
Reputation: 1000
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'));
});
}
Upvotes: 2