Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a text on a line in node.js pdf kit?

Tags:

node.js

pdfkit

I am using pdfkit node module to generate a pdf. My problem is that I want to insert a text on a dashed line. This is what I am doing:

doc.moveDown(2)
        .moveTo(x+leftMargin, doc.y)
        .lineTo(doc.x, doc.y)
        .lineWidth(0.5)
        .dash(3,{space:3})
        .fillAndStroke(defBlackColor)
        .fill(defBlackColor)
        .fontSize(defFontSize)
    .text('Layover:'+' '+ obj.layover,x + leftMargin + xincr/2,doc.y);

But it returns text just below the dashed line, like this:enter image description here

And I want to get:enter image description here

How can I achieve it?

like image 303
Atul Agrawal Avatar asked Jan 06 '23 12:01

Atul Agrawal


1 Answers

We can use .moveTo and split the lines into two and add the text in middle.

Try the code I've posted below, it is working for me:

doc.moveTo(200, 200)       // this is your starting position of the line, from the left side of the screen 200 and from top 200
   .lineTo(400, 200)       // this is the end point the line 
   .dash(5, { space: 10 }) // here we are formatting it to dash
   .text("text goes here", 410, 195) // the text and the position where the it should come
    doc.moveTo(500, 200)   //again we are giving a starting position for the text
   .lineTo(800, 200)       //end point
   .dash(5, {space: 10})   //adding dash
   .stroke() 

returns: enter image description here

like image 127
Yogaraj Saravanan Avatar answered Jan 17 '23 18:01

Yogaraj Saravanan