PDFlib Cookbook

cookbook

pdfua/list_pdfua1

Demonstrate PDF/UA list tagging.

Download PHP Code  Switch to Java Code  Show Output 

<?php
/*
 *
 * 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(__FILE__,3)."/input";

$title = "list_pdfua1";

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

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

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

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

    $font = $p->load_font("NotoSerif-Regular", "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");

     /* Create both Caption and P elements at once */
    $p->fit_textline("The following kinds of fruit are available:", $x1,
        $y, "tag={tagname Caption tag={tagname=P}}");
    $y -= $leading;

    $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 (Throwable $e) {
    echo($e);
    exit(1);
}

$p = 0;
?>