最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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?

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?

Share Improve this question asked Nov 14, 2017 at 13:35 Zoe SunZoe Sun 891 gold badge2 silver badges7 bronze badges 1
  • Not sure, but according to the doc, create an array of string and call doc.text(yourArray,10,10). rawgit./MrRio/jsPDF/master/docs/global.html#text – Nathan Meunier Commented Nov 14, 2017 at 13:47
Add a ment  | 

1 Answer 1

Reset to default 7

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-ponent 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 ments, 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)
发布评论

评论列表(0)

  1. 暂无评论