How to calculate font size of input field in PDF in node js

35 Views Asked by At

I facing the issue while scaling the font size in pdf. I'm using pdf-lib package of node js. I've used font.widthOfTextAtSize() this method to get the width of the text and based on that I'm calculating maxWidth. In the last I'm calculating fontSize. but it's not an accurate match with the input box that I'm getting in pdf.

let fontSize = 12;
      let maxWidth =50;
      if (maxWidth) {
        let textWidth = font.widthOfTextAtSize(textData, fontSize);
        if(textWidth< 70) maxWidth = 50
        else if(textWidth< 100) maxWidth = 70
        else if(textWidth< 140) maxWidth = 100
        else if(textWidth< 160) maxWidth = 120
        else if(textWidth< 200) maxWidth = 130
        else if(textWidth > 200) maxWidth = 200
        
        while (textWidth > maxWidth && fontSize > 5) {
          fontSize -= 1;
          textWidth = font.widthOfTextAtSize(answer, fontSize);
        }
      }

I've tried Puppeteer and pdf-filler packages but have not gotten any proper solution. Thanks in advance.

0

There are 0 best solutions below