Search code examples
javascriptjqueryjspdf

How to calculate the vertical height in jsPDF?


I'm using the jspdf library and I'm facing some problems in the content position, suppose I have this pdf:

var doc = new jsPDF();
doc.setFontSize(12);
doc.text("some text", 15, 14); //<- vertical height is 14

as you can see I placed the text to x = 15 and y = 14, how can I calculate the used height (y) for add the next content? eg:

 doc.addImage(someImage, 'JPEG', 15, 10, 60, 10);

as you can see I have an image that is:

  • x: 15
  • y: 10
  • width: 60
  • height: 10

but how can I know the used vertical height to add the new content? Because in the example above the image will overlay the text (y = 10).

I'm looking for a function that calculate the used height in the document, so I can know where to place the new content in the (vertical y) height.

Maybe there is another and simple solution to do this?

Thanks in advance.


Solution

  • You can use a work around for this as follows.

    Crete a variable var y=14 and use this variable in your text part.

    doc.text("some text", 15, y);
    

    You can reuse the same variable in order to place image after it. or may be if you need space, you can reuse this variable as

    var img_y=y+10;
    
    doc.addImage(someImage, 'JPEG', 15, img_y, 60, 10);