Thank you for submitting an issue to jsPDF. Please read carefully.
Are you using the latest version of jsPDF?
Yes (1.3.5)
Have you tried using jspdf.debug.js?
Yes
Steps to reproduce
What I saw
The save function silently fails, and a console log error displays with an Invalid string length error:
index.js:2177 Error in function Array.join (native): Invalid string length RangeError: Invalid string length
at Array.join (native)
at buildDocument (jspdf.debug.js:1051)
at getArrayBuffer (jspdf.debug.js:1073)
at getBlob (jspdf.debug.js:1083)
at Object.<anonymous> (jspdf.debug.js:1112)
at Object.__safeCallWrapper [as output] (jspdf.debug.js:621)
at Object.API.save (jspdf.debug.js:2191)
What I expected
I expected the pdf file to save.
Below is the code that I'm running when this error is generated:
function saveToPDF(tiles) {
var pdfDoc = new jsPDF({
orientation: "l",
unit: "pt",
format: [612, 792] // 72pts per inch
})
var margins = {
top: 36,
bottom: 36,
left: 36,
width: 720,
height: 540
}
var pdfHTML = document.getElementsByClassName("pdfPreview")
var pageLength = pdfHTML.length
var pageCanvases = new Array(pageLength)
var canvasOperations = []
for(var i = 0; i < pdfHTML.length; i++) {
(j => {
var createCanvas = html2canvas(pdfHTML[j], {
async: true
}).then(canvas => {
pageCanvases[j] = canvas
}, error => {
alert(error)
})
canvasOperations.push(createCanvas)
})(i)
}
Promise.all(canvasOperations).then(() => {
pageCanvases.forEach((canvas, idx) => {
if(idx > 0) {
pdfDoc.addPage()
}
var img = canvas.toDataURL()
pdfDoc.addImage(img, "jpg", margins.left, margins.top, margins.width, margins.height)
})
pdfDoc.save("file.pdf")
})
}
I had this issue as well our reports generate 3000+ pages with images attached. we were running out of memory but as it stands now. We managed to fix the issue. but we are not using html2canvas.
Actually the problem is that javascript can only handle a specific length of a string. And this is the problem, because the mentioned line is content.join('\n');
We can not modify the string length of the javascript engine. Probably we have to reduce the amount of memory/string length by using compression.
I had this issue as well our reports generate 3000+ pages with images attached. we were running out of memory but as it stands now. We managed to fix the issue. but we are not using html2canvas.
Can you please update which API you are using, please share the link for new API to generate PDF. Thanks
Most helpful comment
Actually the problem is that javascript can only handle a specific length of a string. And this is the problem, because the mentioned line is content.join('\n');
We can not modify the string length of the javascript engine. Probably we have to reduce the amount of memory/string length by using compression.