0

私は現在PDFエディタを構築しています。タグの処理の実装に問題があります。

次のタグを許可したい: [h1]、[h2]、[h3]、[h4]、[h4]、[h5]、[h6]、[strong]

drawText(以下のコード) というメソッドを使用してクラスを作成しました。

[h1]タグはフォントサイズとフォントの太さを変更します。コードでわかるように、テキスト行を出力しています。テキスト行の例: これはあなたの [strong]搭乗券[/strong] です。この PDF ファイルをスマートフォンまたはタブレットに保存し、[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
}

writeText()および関数/メソッドを実装するサンプル クラス (https://github.com/jamesggordon/Wrap_Pdf)getStringWidth()に加えて、現在の場所、現在のスタイルなど、他のすべてのものを含めています。関数のコードがwriteParagraph()私に知らせてくれたので、それを に含めますWrap_Pdf

于 2012-12-18T01:08:08.050 に答える