Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting the current x, y position when adding contents to jsPDF

Tags:

i am trying to create pdf using jsPDF is there any function which will return the current cursor position on writing contents to the pdf doc?

like image 958
riyas va Avatar asked Feb 01 '17 11:02

riyas va


2 Answers

The following code snippet shows a possible approach to derive the new y position after adding a line or block of text (you may test it on https://parall.ax/products/jspdf):

var doc = new jsPDF()
var text = 'This is a text without real content but with 59 characters.'

var lineHeight = doc.getLineHeight(text) / doc.internal.scaleFactor
var splittedText = doc.splitTextToSize(text, 50)
var lines = splittedText.length  // splitted text is a string array
var blockHeight = lines * lineHeight
var yPos = 10
var xPos = 10
doc.text(xPos, yPos, splittedText)
yPos += blockHeight
doc.text(xPos, yPos, '----- This text follows the previous text block.')
yPos += lineHeight
doc.text(xPos, yPos, '----- LineHeight=' + lineHeight + ' / blockHeight=' + blockHeight)
yPos += lineHeight
doc.text(xPos, yPos, '----- doc.internal.scaleFactor = ' + doc.internal.scaleFactor)

If you change the width defined as 2nd argument in the splitTextTosize() function (e.g. from 50 to 80), the y position of the next lines is adjusted accordingly.

like image 105
mcaviola Avatar answered Oct 13 '22 00:10

mcaviola


I faced the same question and i search for a method or similar to get the current position. But i did not find one.

My solution is very simple. I have created local variables for x and y. I set them one time and after that i calculate the new position and save the value in the respective variable. So you know your current cursor position all the time.

like image 21
Batajus Avatar answered Oct 13 '22 00:10

Batajus