简体   繁体   English

Zend PDF所见即所得编辑器输出

[英]Zend PDF wysiwyg editor output

I'm currently building a PDF editor. 我目前正在构建PDF编辑器。 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] 我想允许以下标签:[h1],[h2],[h3],[h4],[h4],[h5],[h6],[strong]

I've build an class with a method called drawText(code below). 我已经建立了一个名为drawText(下面的代码)的方法的类。

The [h1] tag will change the font size and the font weight. [h1]标签将更改字体大小和字体粗细。 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]. 示例文本行:这是您的[strong]登机证[/ strong],请将此PDF文件保存在智能手机或平板电脑上,然后[strong]在登机口[/ strong]显示。

I'd like to make the text between the [strong] bold. 我想在[strong]粗体之间输入文本。 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). 为此,我需要使用粗体文本设置TTF文件,然后找到当前的X坐标,然后调用$ 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... 我一直在思考并尝试了数小时来编写使这成为可能的代码(尝试使用explode,preg_match_all等),但我无法使其正常工作...

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;
}

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. 上面的代码可能是大多数人使用Zend_Pdf渲染文本时的起点,但是不幸的是,您将不得不开发一些更复杂的东西才能实现目标。

Firstly, you are going to need to keep track of the current x and y location, along with the current font type and size. 首先,您将需要跟踪当前的x和y位置以及当前的字体类型和大小。

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 . 我有一个示例类(https://github.com/jamesggordon/Wrap_Pdf),该类实现writeText()getStringWidth()函数/方法,还包括所有其他内容,例如当前位置,当前样式等。您无法弄清楚writeParagraph()函数的代码,请告诉我,并将其包含在Wrap_Pdf

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM