PDFlib Cookbook

cookbook

pdfua/image_with_link_pdfua1

Create PDF/UA-1 document where an image is used as background for a link.

Download PHP Code  Switch to Java Code  Show Output 

<?php
/*
 * Image with Link in PDF/UA-1
 * Create PDF/UA-1 document where an image is used as background for a link
 * 
 * The background image for a link must be tagged as Artifact since Figure
 * is not allowed as child of Link.
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 10
 * Required data: font file, image
 */


/* This is where the data files are. Adjust as necessary. */
$searchpath = dirname(__FILE__,3) . "/input";
$imagefile = "lionel.jpg";

$title = "image_with_link_pdfua1";
$exitcode = 0;

try {
    $p = new pdflib();
    /*
        * errorpolicy=exception means that the program will stop
        * if one of the API functions runs into a problem.
        */
    $p->set_option("errorpolicy=exception searchPath={" . $searchpath . "}");

    $p->begin_document("",
        "pdfua=PDF/UA-1 lang=en " .
        "tag={tagname=Document Title={" . $title . "} }") ;

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

    /* Automatically create spaces between chunks of text */
    $p->set_option("autospace=true");

    $p->begin_page_ext(0, 0,
            "width=a4.width height=a4.height taborder=structure");

    $p->create_bookmark("PDF/UA-1 demo", "");

    $font = $p->load_font("NotoSerif-Regular", "unicode", "");
    $p->setfont($font, 24.0);
    
    /* Create a P tag as container of text and link */
    $id_P = $p->begin_item("P", "");
    
    $p->fit_textline("The image serves as background for a link:", 
        50, 625, "fontsize=12");

    /* ============== Link with background image =================== */
    
    /* Create the Link element. The alternative text describes both
        * the background image and the link
        */
    $id_Link = $p->begin_item("Link", "Alt={Visit Kraxi on the Web}");

    $image = $p->load_image("auto", $imagefile, "");

    /* The background image must be tagged as Artifact; the matchbox
        * provides the geometry for create_annotation() below.
        */
    $p->fit_image($image, 50, 400,
        "tag={tagname=Artifact} scale=0.5 matchbox={name={kraxi}}");
    $p->close_image($image);

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

    /* Create Link annotation on named matchbox "kraxi".
    * This automatically creates an OBJR (object reference) element
    * inside the Link element.
    */
    $optlist = "linewidth=0 usematchbox={kraxi} " .
        "contents={Link to Kraxi Inc. Web site} " .
        "action={activate=" . $action . " } ";
    $p->create_annotation(0, 0, 0, 0, "Link", $optlist);

    /* Close the Link and P structure elements */
    $p->end_item($id_Link);
    $p->end_item($id_P);

    $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=image_with_link_pdfua1.pdf");
    print $buf;
}
catch (PDFlibException $e) {
    echo("PDFlib exception occurred in image_with_link_pdfua1 sample:\n" .
        "[" . $e->get_errnum() . "] " . $e->get_apiname() . ": " .
        $e->get_errmsg() . "\n");
    exit(1);
}
catch (Throwable $e) {
    echo($e);
    exit(1);
}

$p = 0;
?>