set_option("searchpath={" . $searchpath . "}"); /* This means we must check return values of load_font() etc. */ $p->set_option("errorpolicy=return"); if ($p->begin_document($outfile, "") == 0) throw new Exception("Error: " . $p->get_errmsg()); $p->set_info("Creator", "PDFlib Cookbook"); $p->set_info("Title", $title); /* Load the font */ $font = $p->load_font("NotoSerif-Regular", "unicode", ""); if ($font == 0) throw new Exception("Error: " . $p->get_errmsg()); /* Set some general cell options shared between all cells */ $optlist = "fittextline={font=" . $font . " fontsize=" . $fontsize . " position={left center}} rowheight=" . $rowheight . " margin=" . $margin . " "; /* ------------ * Add a header * ------------ */ /* Set the current row */ $row = 1; for ($i=0; $i< $nocol; $i++){ $cellopts = $optlist . "colwidth=" . $colwidth[$i%2]; $tbl = $p->add_table_cell($tbl, $i+1, $row, $header[$i%2], $cellopts); if ($tbl == 0) throw new Exception("Error: " . $p->get_errmsg()); } /* ------------------------------------------------ * For each color, add a row containing three cells * ------------------------------------------------ */ for ($i = 0; $i < count($colors); $i++) { $col = 1 + 2*($i % ($nocol/2)); if ($col == 1 ){ /* when colo is 1 we are in a new row */ $row++; } /* -------------------------------------------------------------- * Add the HTML color name in the odd column * -------------------------------------------------------------- */ $cellopts = $optlist . "colwidth=" . $colwidth[$col % 2]; $tbl = $p->add_table_cell($tbl, $col, $row, $colors[$i], $cellopts); if ($tbl == 0) throw new Exception("Error: " . $p->get_errmsg()); /* -------------------------------------------------------------- * Add a rectangle filled with the HTML color in the even column * -------------------------------------------------------------- * * Since the cell doesn't cover a complete row but only one column it * cannot be filled with color using one of the row-based shading * options. We apply the Matchbox feature instead to fill the rectangle * covered by the cell with the color specified by the HTML color name. */ $cellopts = "colwidth=" . $colwidth[$col%2] . " matchbox={fillcolor=" . $colors[$i] . "}"; $tbl = $p->add_table_cell($tbl, $col+1, $row, "", $cellopts); if ($tbl == 0) throw new Exception("Error: " . $p->get_errmsg()); } /* for */ /* ----------------------------------------------------------------- * Fit the table. Using "header=1" the table header will include the * first line. Using "line=horother linewidth=0.3" the ruling is * specified with a line width of 0.3 for all horizontal lines. * Also draw a vertical line after every second column * ----------------------------------------------------------------- */ for ($i = 2; $i < ($nocol); $i += 2){ $vertline .= "{line=vert" . $i . "} "; } $optlist = "header=1 stroke={ {line=horother linewidth=0.3} " . $vertline . "}"; do { $p->begin_page_ext(0, 0, "width=a4.width height=a4.height"); /* 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()); } } /* 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=web_colornames.pdf"); print $buf; } catch (PDFlibException $e) { echo("PDFlib exception occurred:\n". "[" . $e->get_errnum() . "] " . $e->get_apiname() . ": " . $e->get_errmsg() . "\n"); exit(1); } catch (Throwable $e) { echo("PHP exception occurred: " . $e->getMessage() . "\n"); exit(1); } $p = 0; ?>