PDFlib Cookbook

cookbook

textflow/weblink_in_text

Create a Textflow and integrate colorized Web links in the text.

Download PHP Code  Switch to Java Code  Show Output 

<?php
/*
 * Weblink in Text:
 * Create a Textflow and integrate colorized Web links in the text
 *
 * Using the inline options "matchbox" and "matchbox end" define
 * matchboxes which are used to indicate the pieces of text on which the
 * Web links are to be placed.
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 10
 * Required data: none
 */

/* This is where the data files are. Adjust as necessary. */
$searchpath = dirname(__FILE__,3)."/input";
$outfile = "";
$title = "Weblink in Text";

$tf = 0;

try {
    $p = new pdflib();

    $p->set_option("searchpath={" . $searchpath . "}");

    /* This means we must check return values of load_font() etc. */
    $p->set_option("errorpolicy=return");

    /* Set an output path according to the name of the topic */
    if ($p->begin_document($outfile, "") == 0)
        throw new Exception("Error: " . $p->get_errmsg());

    $p->set_info("Creator", "PDFlib Cookbook");
    $p->set_info("Title", $title );

    /* Create page */
    $p->begin_page_ext(0, 0, "width=a4.width height=a4.height");

    /* Create and fit a Textflow with two Web links. Using the inline
     * options "matchbox" and "matchbox end" define the two matchboxes
     * "kraxiweb" and "kraximail" which are used to indicate the pieces of
     * text on which the Web links are to be placed. Provide the matchboxes
     * with particular text color and decoration.
     */
    $text =
        "For more information about the Giant Wing Paper Plane see the " .
        "Web site of <underline=true fillcolor=teal strokecolor=teal " .
        "matchbox={name=kraxiweb boxheight={fontsize descender}}>Kraxi " .
        "Systems, Inc.<matchbox=end underline=false fillcolor=black" .
        " strokecolor=black> or contact us by email via " .
        "<matchbox={name=kraximail fillcolor=darkturquoise " .
        "boxheight={ascender descender}}>questions@kraxi.com" .
        "<matchbox=end fillcolor=black>. You'll get all " .
        "information about how to fly the Giant Wing in a thunderstorm " .
        "as soon as possible.";

    $optlist =
        "fontname=NotoSerif-Regular fontsize=20 leading=140%";

    $tf = $p->create_textflow($text, $optlist);
    if ($tf == 0)
        throw new Exception("Error: " . $p->get_errmsg());

    $result = $p->fit_textflow($tf, 100, 200, 500, 500, "fitmethod=auto");
    if ($result != "_stop")
    {
        /* Check for errors */
    }

    /* Create URI action */
    $optlist = "url={http://www.kraxi.com}";
    $act = $p->create_action("URI", $optlist);

    /* Create Link annotation on matchbox "kraxiweb" */
    $optlist = "action={activate " . $act . 
        "} linewidth=0 usematchbox={kraxiweb}";
    $p->create_annotation(0, 0, 0, 0, "Link", $optlist);

    /* Create URI action */
    $optlist = "url={mailto:questions@kraxi.com}";
    $act = $p->create_action("URI", $optlist);

    /* Create Link annotation on matchbox "kraximail" */
    $optlist = "action={activate " . $act . 
        "} linewidth=0 usematchbox={kraximail}";
    $p->create_annotation(0, 0, 0, 0, "Link", $optlist);

    $p->end_page_ext("");

    $p->end_document("");

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

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

} catch (PDFlibException $e) {
    echo("PDFlib exception occurred:\n".
        "[" . $e->get_errnum() . "] " . $e->get_apiname() .
        ": " . $e->get_errmsg() . "\n");
    exit(1);
} catch (Throwable $e) {
    echo("PHP exception occurred: " . $e->getMessage() . "\n");
    exit(1);
}

$p = 0;

?>