views:

233

answers:

1

Hi guys, so I'm using FPDF in PHP to programmatically generate a PDF file from images and text (not HTML).

One of the biggest issues I've been having is being able to wordwrap text around an image. My current algorithm looks like:

  1. Get text as an array of words
  2. Add words one at a time to a 'current line' variable, and call GetStringWidth() on it to determine the width of the current line
  3. Once I reach a pre-determined max width, I pass off the current line to an array of lines, and start on a new 'current line'.

Doing this allows me to get an array of lines that shouldn't be breaking improperly, however I've discovered that because my text is justified-aligned, GetStringWidth() can't accurately give me the width of the line when it has been justified.

I've dug into FPDF's MultiCell method to try and figure out how it breaks justified text properly but can't really make heads nor tails of it. It seems to boil down to a similar algorithim (and it writes each line using Cell ) but it never actually seems to calculate the width, it writes out PDF "code" such as 0.375 Tw.

Does anyone know how to calculate the width of justified text, given a string and a max width?

A: 

not sure if this helps but I have a related problem - needing to know how many lines a MultiCell will take up.

I did this by using GetStringWidth() / $maxWidth and getting the ceil() of that.

I can then work out the estimated height (as I know the line height I am using) and use that figure (in my case to switch columns or not).

Perhaps feeding the base text into GetStringWidth() and estimating height this way will allow you to determine an appropriate place to break the text into the multiple MultiCell()s.

realcals
How accurate was this for you? From my original question, I am doing something with the same intention (whereby I manually "count" the words in the lines that I'll be writing)
Seguer
working well in test! I need to have some user testing to catch all the wierd cases...but GetStringWidth() does seem to return the correct # of lines.
realcals