Jspdf: Invalid String Length When Saving PDF

Created on 27 Apr 2018  路  4Comments  路  Source: MrRio/jsPDF

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

  1. Add images to a jsPDF object that would generate a file string at around 384859244 characters. (~ 130 pages with full page canvas images generated via html2canvas)
  2. Save File.
  3. Observe Array.join error due to string being too long. (jspdf.debug.js; line 1051)

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.

Wontfix jspdf.js

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.

All 4 comments

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

arulmb0136 picture arulmb0136  路  4Comments

Pinank picture Pinank  路  3Comments

palmoni picture palmoni  路  4Comments

yankeeBrit picture yankeeBrit  路  3Comments

MelanieCroce picture MelanieCroce  路  4Comments