set_option("searchpath={" . $searchpath . "}"); /* This means we must check return values of load_font() etc. */ $p->set_option("errorpolicy=return"); $p->set_option("stringformat=utf8"); 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("Courier", "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 containing four cells * ---------------------------------- */ /* Set the current row */ $row = 1; $cellopts = $optlist . "colwidth=" . $c1; $tbl = $p->add_table_cell($tbl, 1, $row, "Web Color Name", $cellopts); if ($tbl == 0) throw new Exception("Error: " . $p->get_errmsg()); $cellopts = $optlist . "colwidth=" . $c2; $tbl = $p->add_table_cell($tbl, 2, $row, "Hex RGB Value", $cellopts); if ($tbl == 0) throw new Exception("Error: " . $p->get_errmsg()); $cellopts = $optlist . "colwidth=" . $c3; $tbl = $p->add_table_cell($tbl, 3, $row, " Color", $cellopts); if ($tbl == 0) throw new Exception("Error: " . $p->get_errmsg()); $cellopts = $optlist . "colwidth=" . $c4; $tbl = $p->add_table_cell($tbl, 4, $row, " RGB Value in PDFlib", $cellopts); if ($tbl == 0) throw new Exception("Error: " . $p->get_errmsg()); /* ------------------------------------------------ * For each color, add a row containing three cells * ------------------------------------------------ */ $row = 2; for ($i = 0; $i < count($colors); $i+=2) { /* -------------------------------------------------------------- * Add the HTML color name in the first column of the current row * -------------------------------------------------------------- */ $cellopts = $optlist . "colwidth=" . $c1; $tbl = $p->add_table_cell($tbl, 1, $row, $colors[$i+1], $cellopts); if ($tbl == 0) throw new Exception("Error: " . $p->get_errmsg()); /* --------------------------------------------- * Add the HTML color value in the second column * --------------------------------------------- */ $cellopts = $optlist . "colwidth=" . $c2; $tbl = $p->add_table_cell($tbl, 2, $row, "#" . $colors[$i], $cellopts); if ($tbl == 0) throw new Exception("Error: " . $p->get_errmsg()); /* --------------------------------------------------------------------- * Retrieve the RGB values used in PDFlib for the current color. * Fetch the value of the i-th color. It consists of three pairs of hex * digits. Each pair indicates the Red, Green, or Blue component in the * range 0-FF which corresponds to the range 0-255 (decimal). To convert * each of the three bytes to the range 0-1 divide it by 255. * --------------------------------------------------------------------- */ $color = $colors[$i]; $red = hexdec(substr($color, 0, 2)) / 255.0; $green = hexdec(substr($color, 2, 2)) / 255.0; $blue = hexdec(substr($color, 4, 2)) / 255.0; /* -------------------------------------------------------------- * Add a rectangle filled with the HTML color in the third 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=" . $c3 . " matchbox={fillcolor=" . $colors[$i+1] . "}"; $tbl = $p->add_table_cell($tbl, 3, $row, "", $cellopts); if ($tbl == 0) throw new Exception("Error: " . $p->get_errmsg()); /* --------------------------------------------------------------------- * Add the PDFlib color value in the fourth column of the current row. * Allow a maximum (and a minimum) of four digits in the fraction * portion of the number and output the PDFlib color values accordingly. * --------------------------------------------------------------------- */ $cellopts = $optlist . " colwidth=" . $c4 . " marginleft=20"; $tbl = $p->add_table_cell($tbl, 4, $row, sprintf("%.4f", $red) . " " . sprintf("%.4f", $green) . " " . sprintf("%.4f", $blue), $cellopts); if ($tbl == 0) throw new Exception("Error: " . $p->get_errmsg()); $row++; } /* 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. * ----------------------------------------------------------------- */ $optlist = "header=1 stroke={ {line=horother linewidth=0.3}}"; do { $p->begin_page_ext(0, 0, "width=a4.width height=a4.height"); /* Place the table instance */ $result = $p->fit_table($tbl, $llx, $lly, $llx + $c1 + $c2 + $c3 + $c4, $lly + $nrows * $rowheight, $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 (Exception $e) { echo($e->getMessage()); exit(1); } $p = 0; ?>