简体   繁体   中英

Pdflib: how to create drop cap using add_flowtext()

I took the cookbook example and modified it to use add_textflow() instead of create_textflow() but I can't get it to work. Instead of interpreting the macros it just writes it as plain text. What am I doing wrong?

From documentation I got the understanding that I should define the macros inline - or did I misunderstand it? Have tried everything I could think of...

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

this is simple: only create_texflow() support inline options (because you must specify the data in a single chunk). With add_textflow() you have the possibility to "build" the textflow handle in multiple steps, for each text fragment with the related options.

In your case, it might look like the following way:

 $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");

In this code fragment, I do the first add_textflow() without text but all the macros settings. Then it place the "O" with the macro ( &cap_start ) in the option list. The following text "ver the mountains... " is done in the next add_textflow() and as option I place the &cap_end .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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