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:
- Get text as an array of words
- Add words one at a time to a 'current line' variable, and call GetStringWidth() on it to determine the width of the current line
- 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?