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

javascript - jsPDF 2.3 Align text - Stack Overflow

programmeradmin3浏览0评论

I am using the latest version of jdPDF (2.3.0). In the documentation, it says that to center text, passed an object in the 4th parameter with the align: 'center' option, but that doesn't work. I also don't understand why I should specify an X if I want my text to be centered.

Here is my code :

const pdf = new window.jspdf.jsPDF();
pdf.text(`Hello world`, null, 20, { align: 'center'} );
pdf.save("TEST");

jsPDF text documentation : .html#text

I'm using jsPDF from CDN link.

I am using the latest version of jdPDF (2.3.0). In the documentation, it says that to center text, passed an object in the 4th parameter with the align: 'center' option, but that doesn't work. I also don't understand why I should specify an X if I want my text to be centered.

Here is my code :

const pdf = new window.jspdf.jsPDF();
pdf.text(`Hello world`, null, 20, { align: 'center'} );
pdf.save("TEST");

jsPDF text documentation : http://raw.githack./MrRio/jsPDF/master/docs/jsPDF.html#text

I'm using jsPDF from CDN link.

Share Improve this question asked Jan 28, 2021 at 12:40 BeweelamBeweelam 1,14612 silver badges13 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

(x, y) is the position on the document where the anchor point of the text is. The text alignment is relative to this point. This means to have the text centered on the document set x to the value exactly in the middle of the space (x about 105 for A4) and align: 'center'.

pdf.text(`Hello world`, pdf.internal.pageSize.getWidth() / 2, 20, { align: 'center'} );

visualized

                   (x,y)                         
                     .
                     text align left


                     .
             text align center


                     .
      text align right
发布评论

评论列表(0)

  1. 暂无评论