Allow changing styles within wrapped text #60

Closed
wants to merge 2 commits into
from

Conversation

Projects
None yet
3 participants
Contributor

ef4 commented Feb 17, 2012

This adds a new option named continued to text. If you set it to true, the wrapping state will be left in place so the next call to text will pick up where it left off.

Using this you can switch styles like fillColor and font and then continue outputting text within the same flow.

Here is a demo of the feature (running in-browser with pdfkit-www, doesn't work on IE).

booo commented Mar 8, 2013

@devongovett Any plans to merge this?

booo commented Mar 8, 2013

@ef4 I'm currently debugging this one:

window.open(new PDFDocument().text("a", {continued: true}).text(" b").dataURI())

Any idea why this is rendered without a space between the a and b?

Contributor

ef4 commented Mar 20, 2013

Hi @booo, sorry for the slow reply. I think that issue is caused by the second trim call in the _wrap function, but I haven't had a chance to dig in further.

However, trailing space works:

window.open(new PDFDocument().text("a ", {continued: true}).text("b").dataURI())

booo commented Apr 13, 2013

Funny round trip time...

I think if you change

words = text.match(WORD_RE) or []

to

words = text.match(WORD_RE) or [text]

it should work.

Owner

devongovett commented Feb 16, 2014

I just added a continued option to the text method so that this is possible. You add the option to calls where you know you have more text you need to add in another style.

The following example adds text with three sections (part1, part2, part3) in different fonts and colors.

doc.font('Helvetica', 13)
   .text(part1, 100, 300, { continued: yes, indent: 50 })
   .fillColor('red')
   .text(part2, { continued: yes })
   .font('Times', 13)
   .fillColor('green')
   .text(part3)

Closing this issue. Let me know if there are bugs or you need something else!

Owner

devongovett commented Feb 16, 2014

Also note that you don't need to repeat your options for each continued piece of text. The same line wrapper instance is reused, so options line indent, wordSpacing, etc. are preserved. However, you can override them for each part if you wish.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment