node.jspdf

How to give space between words in drawText pdf-lib method in nodejs


I want to give space between word in my text while I'm creating a PDF document but I do not know how I can do this.

This is my code:

firstPage.drawText(`2 5 5 5 5 5 5 5`, {
  x: 274,
  y: 655,
  font: courierBoldFont,
  size: 15,
  color: rgb(0, 0.5, 1),
});

I want space between word while adding string into it but I do not know. I want result like this 5 5 5 5 5 5 5 but string is displaying too close in the PDF like this 55555555555555


Solution

  • Using courier font the inter character spacing should be perfectly even as it is monospaced, just add extra spacing to suit as you have done.

    Your code should look spaced out like this, however I suspect your font definition courierBoldFont, may be wrong

    enter image description here

    Wrong definition of courier bold is not the same large spaces

    enter image description here

    I used sample https://jsfiddle.net/jw4b5rku/4/ for corrections to test courier behaviour so try your your code and versions of

    <script src="https://unpkg.com/pdf-lib@1.4.0"></script>
    <script src="https://unpkg.com/downloadjs@1.4.7"></script>
    to replace sample and see if its results differ when using courier