【问题标题】:Zend PDF wysiwyg editor outputZend PDF 所见即所得编辑器输出
【发布时间】:2012-12-16 14:58:52
【问题描述】:

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

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

我已经用一个名为 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;
}

【问题讨论】:

    标签: php zend-framework wysiwyg zend-pdf


    【解决方案1】:

    上面的代码可能是大多数人在使用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 中。

    【讨论】:

      猜你喜欢
      • 2011-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-10
      相关资源
      最近更新 更多