Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I mix font weights in the same paragraph when using pdfkit?

I'm trying to find a way to use bold font weights for inline emphasis in pdfkit

Unfortunately I cannot find a way to change the font without forcing a line break (bad for inline emphasis...).

I was trying something like:

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

but this will output

Hello

World

I also digged through the source but could not find any option to prevent this.

Anyone has any idea or workaround to tackle this problem?

EDIT:

All I could come up with by now is a ugly hack looking like this:

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

which is working but far from flexible and maintainable.

like image 712
m90 Avatar asked Dec 15 '13 19:12

m90


People also ask

What is font weight?

What is Font Weight? Font weight is the “value” placed on your font that will determine how bold or light your text will appear. There are many “values” that you can use that provide a great deal of flexibility towards creating the font weight that works best for how you want to display your text.

How do I add a page in PDFKit?

Adding pagesThe first page of a PDFKit document is added for you automatically when you create the document unless you provide autoFirstPage: false . Subsequent pages must be added by you.


2 Answers

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

like image 88
Hart Liddell Avatar answered Sep 29 '22 03:09

Hart Liddell


Basically you need to set options with lineBreak : false,

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

This will make the Hello not break line, 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

like image 37
Andrew Avatar answered Sep 29 '22 03:09

Andrew