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

≡放荡痞女 提交于 2020-01-02 02:24:53

问题


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.


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/20598693/can-i-mix-font-weights-in-the-same-paragraph-when-using-pdfkit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!