SickHippie
SickHippie

Reputation: 1402

Using php wordwrap to wrap at different lengths on each line, then print with Zend PDF?

Like the title says, I need to use php (wordwrap or whatever works) to split a string into lines and print onto a PDF using Zend, but each line is a different length and starts at a different X and Y position (for $page->drawText).

Example - there's a text string that is about 500 characters total - no really long words (maybe 7 or 8 chars max), fixed width font. I need to print it onto a series of lines in a pdf. The first line is max 84 chars (x = 172), the next 3 are max 108 chars (x = 53), the last one is 95 characters (x = 150). Each line is a set 11 points down (Y2 = Y1 - 11), although ideally I could set the Y for each new line as well, just in case.

Here's a couple code snippets that have pointed me in the right direction - http://www.php.net/manual/en/function.wordwrap.php#97380 & http://www.ehow.com/how_2226138_wrap-text-zendpdf.html

Neither really covers a way to start the new lines at different points for each line, though. Maybe it's just because it's Monday, but I'm having a hard time getting this one together.

Any ideas?

Upvotes: 0

Views: 1034

Answers (1)

SickHippie
SickHippie

Reputation: 1402

I ended up with this function here, which splits it up and returns as an array. Then I check if (strlen($new[4]) > 95) and truncate it with substr.

function pdfWrapSplit($text, $lines, $firstWidth, $secondWidth)
{
    $text = wordwrap($text, $firstWidth, "|");
    $lastPos = 1;
    for ($i=0;$i<$lines;$i++)
    {
        $lastPos = strpos($text, '|', $lastPos+1);   
        if ($lastPos === FALSE)
            break;
    }
    $text = substr($text, 0, $lastPos) . "|" . wordwrap(str_replace('|',' ',substr($text, $lastPos)), $secondWidth, '|');
    $new = explode('|', $text);
    return $new; 
}

Upvotes: 1

Related Questions