Is it possible to mix font weight in the same paragraph when using pdfkit?

I am trying to find a way to use bold font for inline highlighting in pdfkit

Unfortunately, I cannot find a way to change the font without forcing line breaks (bad for inline highlighting ...).

I tried something like:

pdf.text('Hello ', LEFT, 200).font(bold).text('World!'); 

but it will output

Hello

World

I also dug up the source, but could not find any way to prevent this.

Anyone have any ideas or workarounds to solve this problem?

EDIT

All I could think of was an ugly hack looking like this:

 pdf.text('Hello ', LEFT, 200).moveUp(1).font(bold).text('World!', {indent: pdf.widthOfString('Hello ')}); 

which works, but far from flexible and maintainable.

+7
javascript pdf pdf-generation node-pdfkit
source share
2 answers

Basically you need to set parameters using lineBreak: false,

 pdf.text('Hello ', LEFT, 200, { //here it is, lineBreak : false }).font(bold).text('World!'); 

This will make the string Hello not break, so the next World will print on the same line.

I found this in:

 node_modules\pdfkit\js\mixins\text.js, line 130 

pdfkit version: 0.2.6

+12
source share

The documented way to handle this is continued .

 pdf.font('Helvetica-Bold').text('Hello ', { continued: true }).font('Helvetica').text('World!'); 

http://pdfkit.org/docs/text.html

+1
source share

All Articles