0
votes

I'm currently building a PDF editor. I have a problemen with implementing the processing of the tags.

I want to allow the following tags: [h1],[h2],[h3],[h4],[h4],[h5],[h6],[strong]

I've build an class with a method called drawText(code below).

The [h1] tag will change the font size and the font weight. As you can see in the code I'm outputting lines of text. Example text line: This is your [strong]boarding pass[/strong], please save this PDF file on your smartphone or tablet and [strong]show it at the gate[/strong].

I'd like to make the text between the [strong] bold. To do this with Zend_PDF I need to set the TTF file with the bold text and then find the current X-coordinate and call $this->pdf()->drawText(text, X-coordinate, Y-coordinate, charset). I've been thinking and trying for hours to write the code which makes this possible(tried using explode, preg_match_all, etc), but I can't get it to work...

I believe I'm not the only one with this problem, and I hope someone has thought about this and can help a little by telling how he or she did it...

Hope to hear from someone and thanks in advance!

/**
 * drawSplittedText()
 * 
 * @param array $text
 * @return object Application_Plugin_PdfPlugin
 */
public function drawSplittedText(Array $text)
{
    // Count the number of rows.
    $textRowCount = count($text);

    $i = 0;        

    foreach ($text as $row)
    {           
        // Replace tabs, because they're not outputted properly.
        $row = str_replace("\t", '    ', $row);

        // If the character encoding of the currrent row not is UTF-8, convert the row characters to UTF-8.
        if (($rowEncoding = mb_detect_encoding($row)) != 'UTF-8') {
            $row = iconv($rowEncoding, 'UTF-8', $row);
        }

        // Output row on PDF
        $this->pdf()->drawText($row, $this->_defaultMarginleft, $this->currentY, 'UTF-8');

        $this->newLine();

        ++$i;               
    }

    return $this;
}
1

1 Answers

0
votes

The code above is probably where most people start when rendering text with Zend_Pdf, but unfortunately you are going to have to develop something a litte more complex to achieve your goals.

Firstly, you are going to need to keep track of the current x and y location, along with the current font type and size.

Then you'll need a helper function/method to calculate how much space a chunk of text is going to need when rendered in the current font and size.

I would then suggest breaking up your rendering code as follows:

function writeParagraph( $text )
{
    // Looks for the next tag and sends all text before that tag to the
    // writeText() function. When it gets to a tag it changes the current
    // font/size accordingly, then continues sending text until it runs out
    // of text or reaches another tag. If you need to deal with nested tags
    // then this function may have to call itself recursively.
}

function writeText( $text )
{
    // The first thing this function needs to do is call getStringWidth() to
    // determine the width of the text that it is being asked to render and if
    // the line is too long, shorten it. In practice, a better approach is to
    // split the words into an array based on the space between each word and
    // then use a while() loop to start building the string to be rendered
    // (start with first word, then add second word, then add third word, etc),
    // in each iteration testing the length of the current string concatenated
    // with the next word to see if the resulting string will still fit. If you
    // end up with a situation where adding the next word to the current string
    // will result in a string that is too long, render the current string and
    // a line feed then start the process again, using the next word as the first
    // word in the new string. You will probably want to write a bonus line feed
    // at the end as well (unless, of course, you just wrote one!).
}

function getStringWidth( $str )
{
    // This needs to return the width of $str
}

I have a sample class (https://github.com/jamesggordon/Wrap_Pdf) that implements the writeText() and getStringWidth() functions/methods, plus includes all of the other stuff, like current location, current style, etc. If you can't figure out the code for the writeParagraph() function let me know and I'll include it in Wrap_Pdf.