PDFlib Cookbook

cookbook

pdfua/tag_out_of_order_pdfua1

Demonstrate how to create Tagged PDF out of order.

Download PHP Code  Switch to Java Code  Show Output 

<?php
/*
 * tag_out_of_order_pdfua1
 *
 * This topic demonstrates how to create Tagged PDF out of order, using
 * the following features:
 * - suspend/resume_page() for adding content to a previous page
 * - query the current structure element id and index with get_option()
 * - the "parent" and "index" tagging options for adding structure elements
 *   at a previous location in the structure tree
 *
 * 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 = "Create Tagged PDF content out of order";

$x = 50; $y1 = 600; $y2 = 550;

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

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

    $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, "");

    // ***************************************************
    // First pass: create partial content on each page.
    // ***************************************************

    $id_parent = (int) $p->get_option("activeitemid", "");

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

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

    $p->fit_textline("Heading of Chapter 1", $x, $y1, "tag={tagname=H1}");

    /* Fetch the number of kids of the parent */
    $index1 = (int) $p->get_option("activeitemkidcount", "");

    // Suspend page 1 to resume it later
    $p->suspend_page("");

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

    $p->setfont($font, 24);
    $p->fit_textline("Heading of Chapter 2", $x, $y1, "tag={tagname=H1}");
    $index2 = (int) $p->get_option("activeitemkidcount", "");

    // Suspend page 2 to resume it later
    $p->suspend_page("");

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

    $p->setfont($font, 24);
    $p->fit_textline("Heading of Chapter 3", $x, $y1, "tag={tagname=H1}");
    $index3 = (int) $p->get_option("activeitemkidcount", "");

    // Suspend page 3 to resume it later
    $p->suspend_page("");

    // ***************************************************
    // Second pass: add more content to each page.
    // ***************************************************

    // Revisit page 1
    $p->resume_page("pagenumber=1");

    // insert more content after the H1 element
    $p->fit_textline("Contents of chapter 1...", $x, $y2, "fontsize=12 "
        . "tag={tagname=P parent=" . $id_parent . " index=" . $index1 . "}");

    // Since we inserted an element we must increase subsequent indices
    $index2++;
    $index3++;

    $p->end_page_ext("");

    // Revisit page 2
    $p->resume_page("pagenumber=2");

    // insert more content after the H1 element
    $p->fit_textline("Contents of chapter 2...", $x, $y2, "fontsize=12 "
        . "tag={tagname=P parent=" . $id_parent . " index=" . $index2 . "}");

    // Since we inserted an element we must increase the subsequent
    // index
    $index3++;

    $p->end_page_ext("");

    // Revisit page 3
    $p->resume_page("pagenumber=3");

    // insert more content after the H1 element
    $p->fit_textline("Contents of chapter 3...", $x, $y2, "fontsize=12 "
        . "tag={tagname=P parent=" . $id_parent . " index=" . $index3 . "}");

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

$p = 0;

?>