Search code examples
javascriptjspdf

How do I draw a rectangle encircling a text in jsPDF?


I am writing a text into a pdf like this:

var splitTitle = doc.splitTextToSize(str, 180);
doc.text(15, 15, splitTitle);

This is the output:

output

What I want to do is to draw a rectangle around the text. The text height can be dynamic depending on the length of the text, so how do I proceed?


Solution

  • Getting the text dimensions by using getTextDimensions() solved the issue:

    doc.rect(15,15,doc.getTextDimensions(splitTitle).w,doc.getTextDimensions(splitTitle).h)