Zoe Sun
Zoe Sun

Reputation: 89

jsPDF: How to use doc.text() to print strings in separate lines?

I have a loop that prints strings in a JSON object.

for (var i in list){
    doc.text(list[i]['id'] + '  ' + list[i]['name'], 10 ,10)
}

In the pdf file, all strings overlapped in the first line. I tried adding '\n' but it didn't work. How do I use doc.text() to print strings in separate lines?

Upvotes: 3

Views: 12887

Answers (1)

Mads Marquart
Mads Marquart

Reputation: 500

The function you're using takes the parameters (coordinates) x and y like so: doc.text(text, x, y, flags). So to print the strings in seperate lines, you should add something to the y-component of the coordinate each time the loop runs. Example:

for (var i in list){
    doc.text(list[i]['id'] + '  ' + list[i]['name'], 10, 10 + 10*i)
}

Source: jsPDF.text

EDIT: As said in the comments, you could also just pass an array of the text you want to display like this:

var text = []
for (var i in list){
    text.push(list[i]['id'] + '  ' + list[i]['name'])
}
doc.text(text, 10, 10)

Upvotes: 7

Related Questions