I am building a system that needs to permit users to lay out some content (text boxes and graphics) in a web browser using javascript and CSS. I need to then convert that layout into a faithful representation in a PDF. The text needs to be a mix of normal, bold, and italic text.
I am currently using a mix of javascript, CSS, and PHP. I can convert the content to PDF using the PHP class in Zend framework. However, I need to figure out the most efficient way to calculate word wraps and convert text sizing from CSS to PDF.
The sizing can be relative because the user will see a fixed box working area. On some screens, it will be bigger than others (due to varying resolutions and screen sizes), but the relative sizes will remain the same in CSS. Therefore, if I use absolute pixel size for both the outer containing box and the font, I should be able to maintain a proper relative size.
Also, within the PDF I can figure out the appropriate font size using the relative pixel sizes of outer bounding box and font. However, in Zend PDF I can render text lines... but not text within a bounding box. I need an efficient way to translate from CSS to PDF to get similar text wrapping.
One thought is to use a lookup table to calculate the character widths of the Truetype fonts in question, and figure out width from there. We'd have to have a lookup for each character, in each style, for each font used. I'm not sure whether TT fonts use font kerning pairs. If so, the lookup table would have to be even bigger. What a pain.
Because Firefox and other browser already do a bang-up job of calculating text wrap, it would also make sense to figure out how they're doing it and mimic it.
Or, we could do a sample and retry method by creating rendered images of the text in question using PHP. Or we can use the PHP imagettfbbox function (or related functions). The problem is I don't know if they can handle inline calculation of varying fonts (e.g. bold, italic, normal).
I'm hoping someone has had to wrestle with this issue before and can provide a slick answer!
Start Free Trial