PDFlib Cookbook

cookbook

pdfua/tagged_list

Download PHP Code      Switch to Java Code      Show Output PDF

<?php
/* $Id: tagged_list.php,v 1.3 2014/02/03 17:10:24 rjs Exp $
 *
 * Demonstrate list tagging
 *
 * required software: PDFlib/PDFlib+PDI/PPS 9
 * required data: font
 */

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

$title = "tagged_list";

try {
    $x1 = 50; $x2 = 65; $y = 700; $leading = 36;

    $p = new pdflib();

    $p->set_option("errorpolicy=exception searchpath={" . $searchpath . "}");
    $p->set_option("stringformat=utf8");

    if ($p->begin_document("",
            "pdfua=PDF/UA-1 lang=en tag={tagname=Document}") == 0)
        throw new Exception("Error: " . $p->get_errmsg());

    $p->set_info("Creator", "PDFlib Cookbook");
    $p->set_info("Title", $title . ' $Revision: 1.3 $');

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

    $p->set_option("charref=true");

    $p->begin_page_ext(595, 842, "");

    $font = $p->load_font("DejaVuSerif", "unicode", "embedding");

    $p->setfont($font, 16);

    $p->create_bookmark("Tagged list demo", "");

    $p->fit_textline("Tagged list demo", $x1, 750,
        "tag={tagname=H1} fontsize=24");

    $id_list = $p->begin_item("L", "ListNumbering=Disc");

    $id_caption = $p->begin_item("Caption", "");
    $p->fit_textline("The following kinds of fruit are available:", $x1,
        $y, "tag={tagname=P}");
    $y -= $leading;
    $p->end_item($id_caption);

    $id_listitem = $p->begin_item("LI", "");
    $p->fit_textline("&#x2022;", $x1, $y, "tag={tagname=Lbl}");
    $p->fit_textline("Apples", $x2, $y, "tag={tagname=LBody}");
    $y -= $leading;
    $p->end_item($id_listitem);

    $id_listitem = $p->begin_item("LI", "");
    $p->fit_textline("&#x2022;", $x1, $y, "tag={tagname=Lbl}");
    $p->fit_textline("Oranges", $x2, $y, "tag={tagname=LBody}");
    $y -= $leading;
    $p->end_item($id_listitem);

    $id_listitem = $p->begin_item("LI", "");
    $p->fit_textline("&#x2022;", $x1, $y, "tag={tagname=Lbl}");
    $p->fit_textline("Bananas", $x2, $y, "tag={tagname=LBody}");
    $y -= $leading;
    $p->end_item($id_listitem);

    $p->end_item($id_list);

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

$p = 0;
?>