0

我目前正在构建一个 PDF 编辑器。我在实现标签处理时遇到了问题。

我想允许以下标签:[h1],[h2],[h3],[h4],[h4],[h5],[h6],[strong]

我用一个名为drawText(下面的代码)的方法构建了一个类。

[h1] 标签会改变字体大小和字体粗细。正如您在代码中看到的那样,我正在输出文本行。示例文本行:这是您的[strong]登机牌[/strong],请将此 PDF 文件保存在您的智能手机或平板电脑上并[strong]在登机口出示[/strong]。

我想让 [strong] 粗体之间的文本。要使用 Zend_PDF 执行此操作,我需要使用粗体文本设置 TTF 文件,然后找到当前 X 坐标并调用 $this->pdf()->drawText(text, X-coordinate, Y-coordinate, charset)。我已经思考并尝试了几个小时来编写使这成为可能的代码(尝试使用explode,preg_match_all等),但我无法让它工作......

我相信我不是唯一一个遇到这个问题的人,我希望有人已经考虑过这一点,并且可以通过告诉他或她是如何做到的来提供一点帮助......

希望收到某人的来信,并提前致谢!

/**
 * 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;
}
4

1 回答 1

0

上面的代码可能是大多数人在使用 渲染文本时开始的地方Zend_Pdf,但不幸的是,您将不得不开发一些更复杂的东西来实现您的目标。

首先,您需要跟踪当前的 x 和 y 位置,以及当前的字体类型和大小。

然后,您将需要一个辅助函数/方法来计算以当前字体和大小呈现时一大块文本需要多少空间。

然后我会建议分解你的渲染代码如下:

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
}

我有一个示例类(https://github.com/jamesggordon/Wrap_Pdf),它实现了writeText()getStringWidth()函数/方法,还包括所有其他内容,如当前位置、当前样式等。如果你不知道该writeParagraph()函数的代码让我知道,我会将其包含在Wrap_Pdf.

于 2012-12-18T01:08:08.050 回答