PDFlib Cookbook

cookbook

pdfua/parallel_columns_pdfua1

Demonstrate how to tag multiple columns in parallel with activate_item().

Download PHP Code  Switch to Java Code  Show Output 

<?php
/*
 * parallel_columns_pdfua1
 *
 * This topic demonstrates how to tag multiple columns in parallel with
 * activate_item(). In order to work around Acrobat problems no direct
 * content should be created after activate_item(), but only new structure
 * elements.
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 10
 * Required data: font
 */

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

$outfile = "";
$title = "Tag parallel columns";

$p = null;
$x_left = 50; $x_right = 350; $y1 = 600; $y2 = 580;

try {
    $p = new pdflib();

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

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

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

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

    $font = $p->load_font("NotoSerif-Regular", "unicode", "");
    if ($font == 0)
        throw new Exception("Error: " . $p->get_errmsg());

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

    $id_div1 = $p->begin_item("Div", "Title={Left column}");
    $p->setfont($font, 12);

    /* Start with a H1 for the left column */
    $p->fit_textline("Start of left column...", $x_left, $y1,
        "tag={tagname=H1} fontsize=18");

    /*
     * Since we keep the first P open we must explicitly designate the
     * root element as parent for the right column.
     */
    $id_div2 = $p->begin_item("Div", "parent=0 Title={Right column}");
    $p->fit_textline("Start of right column...", $x_right, $y1,
        "tag={tagname=H1} fontsize=18");

    /* Return to the left column and add another structure element */
    $p->activate_item($id_div1);
    $p->fit_textline("continued", $x_left, $y2, "tag={tagname=P}");
    $p->end_item($id_div1);

    /* Now return to the right column again and add an element */
    $p->activate_item($id_div2);
    $p->fit_textline("also continued", $x_right, $y2, "tag={tagname=P}");
    $p->end_item($id_div2);

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

$p = 0;

?>