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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With