'Working with text in pdfkit
How to set characterSpacing from pt/px?
How to set lineHeight from pt/px?
I just can not understand how to translate from points or pixels into a value for pdfkit. Help me please
https://github.com/devongovett/pdfkit
I need to convert html text to pdf. And you need to match the letter spacing and the line spacing.
CSS: Font - 18pt; LineHeight - 1.4 (not pt); letterSpacing - 2pt;
How to set this in pdfkit?
doc.text(element.content, element.left, element.top, {
width : element.width,
align : element.properties.textAlign,
characterSpacing : 2, //???
lineBreak : false,
lineGap : 1.4, //????
});
Solution 1:[1]
When using pdfKit, measurements are in points where 72 points is 1 inch. So characterSpacing is how many "points" between each character. In the example provided of 2, it would be 2 points or 2/72 of an inch.
The lineGap, is the number of points between lines of text. For example, lineGap: 10 is 10/72 of an inch. This represents the space between the bottom of one row and the top of the subsequent row.
This is different than line-height in HTML, where line-height is more like a multiplier of the specified line height.
Solution 2:[2]
This is how it works for me, I hope it helps someone
doc
.fontSize(10)
.font('Helvetica-Bold')
.lineGap(5)
.text('your text', 50, 50, { align: 'right' });
// positioning of your text first 50 is for x, second for y
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Phaedrus |
| Solution 2 | artch |
