PDFlib Cookbook

cookbook

pdfua/tag_parallel_columns

Download PHP Code      Switch to Java Code     Show Output PDF

<?php
/* $Id: tag_parallel_columns.php,v 1.2 2014/02/03 17:10:24 rjs Exp $
 * tag_parallel_columns
 *
 * 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 9
 * Required data: font
 */

/* This is where the data files are. Adjust as necessary. */
$searchpath = dirname(dirname(dirname(__FILE__)))."/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*/
    $p->set_option("stringformat=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 . ' $Revision: 1.2 $');

    $font = $p->load_font("DejaVuSerif", "unicode", "embedding");
    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_sect1 = $p->begin_item("Sect", "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_sect2 = $p->begin_item("Sect", "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_sect1);
    $p->fit_textline("continued", $x_left, $y2, "tag={tagname=P}");
    $p->end_item($id_sect1);

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

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

$p = 0;

?>