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

Pdflib: how to create drop cap using add_flowtext()

我将 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;

这很简单:只有 create_texflow() 支持内联选项(因为您必须在单个块中指定数据)。使用 add_textflow(),您可以 "build" 分多个步骤处理文本流,每个文本片段都有相关选项。

在您的情况下,它可能类似于以下方式:

 $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() 没有文字,但所有 宏设置。然后它将 "O" 与宏 (&cap_start) 放在 选项列表。下面的文字"ver the mountains... "就完成了 在下一个 add_textflow() 中,作为选项,我将 &cap_end.