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 . ' $Revision: 1.4 $'); /* 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("DejaVuSerif", "unicode", "embedding"); 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=DejaVuSerif encoding=unicode embedding " . "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=DejaVuSerif encoding=unicode embedding " . "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 (Exception $e) { echo($e); exit(1); } $p = 0; ?>