Pdflib:如何使用add_flowtext()创建首字下沉

时间:2017-04-28 08:04:58

标签: php pdflib

我使用了cookbook example并对其进行了修改,以使用add_textflow()代替create_textflow(),但我无法让它发挥作用。它不是解释宏而是将其写为纯文本。我做错了什么?

从文档中我了解到我应该内联定义宏 - 或者我是否误解了它?尝试过我能想到的一切......

$tf = 0;
$optlist = "";

$llx = 100; $lly = 50; $urx = 450; $ury = 800;
$t_fontsize = 16;      // font size of the text
$t_leading = 20;       // leading of the text
$c_num = 3;            // no. of lines for the drop cap to cover

$c_textrise = -(($c_num - 1) * $t_leading); // text rise of the drop cap
$c_fontsize = -($c_textrise * 1.8);     // font size of the drop cap

try {
    $p = new pdflib();
    $p->set_option("errorpolicy=exception");
    $p->set_option("stringformat=utf8");
    $p->begin_document("", "");

    $optlist = "fontname=Helvetica fontsize=" . $t_fontsize . " encoding=unicode ";

    $text = "<macro " .
    "{cap_start {fontsize=" . $c_fontsize . " leading=" . $t_leading .
    "            textrise=" . $c_textrise .
    " matchbox={createwrapbox boxheight={leading textrise}}} " .
    "cap_end {matchbox=end fontsize=" . $t_fontsize . " textrise=0}}>";
    $text .= "<&cap_start>O<&cap_end>ver the mountains... ";

    $tf = $p->add_textflow($tf, $text, $optlist);



    $text =
    "Paper Planes are the ideal way of " .
    "passing the time. We offer revolutionary " .
    "new develop&shy;ments of the traditional com&shy;mon " .
    "paper planes. If your lesson, conference, or lecture " .
    "turn out to be deadly boring, you can have a wonderful time " .
    "with our planes. ";

    $tf = $p->add_textflow($tf, $text, $optlist);


    do {
        $p->begin_page_ext(0, 0, "width=a4.width height=a4.height");
        $result = $p->fit_textflow($tf, $llx, $lly, $urx, $ury, "");
        $p->end_page_ext("");
    } while ($result == "_boxfull" || $result == "_nextpage");

    /* Check for errors */
    if (!$result == "_stop") {
        if ($result == "_boxempty")
            throw new Exception ("Error: Textflow box too small");
        else {
            throw new Exception ("User return '" . $result .
                "' found in Textflow");
        }
    }

    $p->delete_textflow($tf);

    $p->end_document("");
    $buf = $p->get_buffer();
    $len = strlen($buf);

    header("Content-type: application/pdf");
    header("Content-Length: $len");
    header("Content-Disposition: inline; filename=drop_caps.pdf");
    print $buf;

} catch (PDFlibException $e) {
    die("PDFlib exception occurred:\n".
        "[" . $e->get_errnum() . "] " . $e->get_apiname() .
        ": " . $e->get_errmsg() . "\n");
} catch (Exception $e) {
    die($e->getMessage());
}

$p = 0;

1 个答案:

答案 0 :(得分:1)

这很简单:只有create_texflow()支持内联选项(因为您必须在单个块中指定数据)。使用add_textflow(),您有可能&#34;构建&#34;对于具有相关选项的每个文本片段,文本流句柄分多步进行。

在您的情况下,它可能看起来像以下方式:

 $optlistmacro = " macro " .
"{cap_start {fontsize=" . $c_fontsize . " leading=" . $t_leading .
"            textrise=" . $c_textrise .
" matchbox={createwrapbox boxheight={leading textrise}}} " .
"cap_end {matchbox=end fontsize=" . $t_fontsize . " textrise=0}}";


$tf = $p->add_textflow($tf, "", $optlist . $optlistmacro);
$tf = $p->add_textflow($tf, "O", "&cap_start");
$tf = $p->add_textflow($tf, "ver the mountains... ", "&cap_end");

在这个代码片段中,我做的第一个add_textflow()没有文字,但是全部 宏设置。然后它放置&#34; O&#34;使用宏(&cap_start) 选项列表。以下文字&#34;山脉......&#34;已经完成了 在下一个add_textflow()中,作为选项,我放置&cap_end