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.com/MrRio/jsPDF/master/docs/jsPDF.html#text
I'm using jsPDF from CDN link.
(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