pdfua/table_pdfua1
Demonstrate automatic table tagging.
Download PHP Code Switch to Java Code Show Output
<?php
/*
*
* Demonstrate automatic table tagging
*
* required software: PDFlib/PDFlib+PDI/PPS 10
* required data: image file (dummy text created within the program)
*/
/* This is where the data files are. Adjust as necessary. */
$searchpath = dirname(__FILE__,3)."/input";
$imagefile = "nesrin.jpg";
$title = "table_pdfua1";
$p = null;
try {
$p = new pdflib();
$tf = 0;
$tbl = 0;
$rowmax = 5;
$colmax = 5;
$llx = 50; $lly = 50; $urx = 550; $ury = 700;
/* Dummy text for filling a cell with multi-line Textflow */
$tf_text =
"Sample text created with the Textflow feature in order to " .
"create multiline content within a table cell.";
/*
* Set the search path for fonts and images etc.
*/
$p->set_option(
"errorpolicy=exception SearchPath={" . $searchpath . "}");
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");
/* -------------------- Add table cells -------------------- */
/* ---------- Row 1: table header */
$row = 1;
$col = 1;
$font = $p->load_font("NotoSerif-Regular", "unicode", "");
if ($font == 0)
throw new Exception("Error loading font: " . $p->get_errmsg());
$optlist = "fittextline={position=center fontsize=14 font=" . $font . "}";
$tbl = $p->add_table_cell($tbl, $col, $row, "Header " . $col, $optlist);
$col++;
$tbl = $p->add_table_cell($tbl, $col, $row, "Header " . $col, $optlist);
$col++;
$tbl = $p->add_table_cell($tbl, $col, $row, "Header " . $col, $optlist);
$col++;
$tbl = $p->add_table_cell($tbl, $col, $row, "Header " . $col, $optlist);
$col++;
$tbl = $p->add_table_cell($tbl, $col, $row, "Header " . $col, $optlist);
/* ---------- Row 2: various kinds of content */
/* ----- Simple text cell */
$row++;
$col = 1;
$optlist = "fittextline={font=" . $font . " fontsize=10 orientate=west}";
$tbl = $p->add_table_cell($tbl, $col, $row, "vertical line", $optlist);
/* ----- Colorized background */
$col++;
$optlist = "fittextline={font=" . $font . " fontsize=10} "
. "matchbox={fillcolor={rgb 0.9 0.5 0}}";
$tbl = $p->add_table_cell($tbl, $col, $row, "some color", $optlist);
/* ----- Multi-line text with Textflow */
$col++;
$optlist = "fontname=NotoSerif-Regular charref fontsize=8";
$tf = $p->add_textflow($tf, $tf_text, $optlist);
$optlist = "margin=2 textflow=" . $tf;
$tbl = $p->add_table_cell($tbl, $col, $row, "", $optlist);
/* ----- Rotated image */
$col++;
$image = $p->load_image("auto", $imagefile, "");
if ($image == 0)
throw new Exception("Error loading image: " . $p->get_errmsg());
$optlist = "image=" . $image .
" fitimage={orientate=west" .
" tag={tagname=Figure Alt={Nesrin at Zabrisky point}}}";
$tbl = $p->add_table_cell($tbl, $col, $row, "", $optlist);
/* ----- Diagonal stamp */
$col++;
$optlist = "fittextline={font=" . $font . " fontsize=10 stamp=ll2ur "
. "tag={tagname=Artifact} }";
$tbl = $p->add_table_cell($tbl, $col, $row, "Artifact", $optlist);
/* ---------- Fill $row 3 and above with their numbers */
for ($row++; $row <= $rowmax; $row++) {
for ($col = 1; $col <= $colmax; $col++) {
$num = "Col " . $col . "/Row " . $row;
$optlist = "colwidth=20% fittextline={font=" . $font
. " fontsize=10}";
$tbl = $p->add_table_cell($tbl, $col, $row, $num, $optlist);
}
}
/* ---------- Place the table on one or more pages ---------- */
/*
* Loop until all of the table is placed; create new pages as long
* as more table instances need to be placed.
*/
do {
$p->begin_page_ext(0, 0, "width=a4.width height=a4.height");
$p->create_bookmark("Tagged table demo", "");
$p->fit_textline("Tagged table demo", 50, 750,
"fontname=NotoSerif-Regular " .
"fontsize=16 tag={tagname=H1}");
/*
* Shade every other $row; draw lines for all table cells. Add
* "showcells showborder" to visualize cell borders
*/
$optlist = "header=1 fill={{area=rowodd fillcolor={gray 0.9}}} "
. "stroke={{line=other}} "
. "tag={tagname=Table Summary={Sample Tagged table}} "
. "caption={fitbox={0r 100% 0r 20r} "
. "matchbox={fillcolor=Lavender borderwidth=1.0} "
. "tag={tagname=P} "
. "fittextline={font=" . $font . " fontsize=14} "
. "text={Table Caption}}";
/* Place the table instance */
$result = $p->fit_table($tbl, $llx, $lly, $urx, $ury, $optlist);
if ($result == "_error")
throw new Exception("Couldn't place table : "
. $p->get_errmsg());
$p->end_page_ext("");
}
while ($result == "_boxfull");
/* Check the $result; "_stop" means all is ok. */
if (!$result == "_stop") {
if ($result == "_error") {
throw new Exception("Error when placing table: "
. $p->get_errmsg());
}
else {
/*
* Any other return value is a user exit caused by the
* "return" option; this requires dedicated code to deal
* with.
*/
throw new Exception("User return found in Textflow");
}
}
/* This will also delete Textflow handles used in the table */
$p->delete_table($tbl, "");
$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;
?>