简体   繁体   中英

JsPDF .save() doesn't work

This code let me download the pdf, but give an error in putting it in the iframe:

docNormale.save('Ordini.pdf');
var iframe = document.getElementById('outputPDFnormale');
iframe.style.width = '60%';
iframe.style.height = '650px';
iframe.src = docNormale.output('datauristring');

This code let me insert the pdf in the iframe, but give an error while downloading the pdf:

    var iframe = document.getElementById('outputPDFnormale');
    iframe.style.width = '60%';
    iframe.style.height = '650px';
    iframe.src = docNormale.output('datauristring');
    docNormale.save('Ordini.pdf');

The error is the same both cases, and it is: Uncaught TypeError: Cannot read property 'toFixed' of undefined

Im in latest Chrome

This is a bug/limitation on the lib, save() is an output operation and two such consecutive operations aren't supported atm, so feel free to file a bug on their issues tracker: https://github.com/MrRio/jsPDF/issues

Meanwhile, this should work:

var rawdata = docNormale.output();

var len = rawdata.length,
    ab = new ArrayBuffer(len),
    u8 = new Uint8Array(ab);

    while(len--) u8[len] = rawdata.charCodeAt(len);

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

saveAs(blob, 'Ordini.pdf');

var iframe = document.getElementById('outputPDFnormale');
iframe.style.width = '60%';
iframe.style.height = '650px';
iframe.src = URL.createObjectURL(blob);

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