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 normal font */ $font = $p->load_font("Helvetica", "unicode", ""); if ($font == 0) throw new Exception("Error: " . $p->get_errmsg()); /* Load bold font */ $boldfont = $p->load_font("Helvetica-Bold", "unicode", ""); if ($boldfont == 0) throw new Exception("Error: " . $p->get_errmsg()); /* Start the page */ $p->begin_page_ext($pagewidth, $pageheight, ""); /* Output some descriptive text */ $p->setfont($boldfont, 11); $p->fit_textline("Use the \"textflow\" option of add_table_cell() ", $llx, $ury + 30, ""); $p->fit_textline("to spread a Textflow over several cells", $llx, $ury + 15, ""); /* ----------------- * Add an image cell * ----------------- * * The image is placed in a cell starting in column 1 row 2. The column * width is 90 points. The cell margins are set to 4 points. */ $image = $p->load_image("auto", $imagefile, ""); if ($image == 0) throw new Exception("Error: " . $p->get_errmsg()); $optlist = "image=" . $image . " colwidth=" . $c1 . " margin=4"; $tbl = $p->add_table_cell($tbl, 1, 2, "", $optlist); if ($tbl == 0) throw new Exception("Error: " . $p->get_errmsg()); /* Add the Textflow */ $optlist = "font=" . $font . " fontsize=8 leading=110% charref"; $tf = $p->add_textflow(0, $tf_text, $optlist); if ($tf == 0) throw new Exception("Error: " . $p->get_errmsg()); /* Prepare the option list for the Textflow cell * * Use the "continuetextflow" option to continue the Textflow in further * cells. * To avoid any space from the top add the Textflow cell use * "fittextflow={firstlinedist=capheight}". Then add a margin of 4 * points. */ $optlist = "textflow=" . $tf . " fittextflow={firstlinedist=capheight} " . "colwidth=" . $c2 . " rowheight=" . $rowheight . " margin=4 continuetextflow"; /* Add the Textflow table cell in column 1 row 1 */ $tbl = $p->add_table_cell($tbl, 1, 1, "", $optlist); if ($tbl == 0) throw new Exception("Error: " . $p->get_errmsg()); /* Continue the Textflow table cell in column 2 row 2 */ $tbl = $p->add_table_cell($tbl, 2, 2, "", $optlist); if ($tbl == 0) throw new Exception("Error: " . $p->get_errmsg()); /* Continue the Textflow table cell in column 1 row 3 */ $tbl = $p->add_table_cell($tbl, 1, 3, "", $optlist); if ($tbl == 0) throw new Exception("Error: " . $p->get_errmsg()); /* Colorize the table cell in column 2 row 1 and column 2 row 3 using a * matchbox */ $optlist = "matchbox={fillcolor={rgb 0.8 0.8 0.87}}"; $tbl = $p->add_table_cell($tbl, 2, 1, "", $optlist); if ($tbl == 0) throw new Exception("Error: " . $p->get_errmsg()); $tbl = $p->add_table_cell($tbl, 2, 3, "", $optlist); if ($tbl == 0) throw new Exception("Error: " . $p->get_errmsg()); /* Define the option list for fitting the table. * * The "stroke" option specifies the table ruling. The * "line=frame linewidth=0.8" suboptions define an outside ruling with a * line width of 0.8 and the "line=other linewidth=0.3" suboptions * define a cell ruling with a line width of 0.3. */ $optlist = "stroke={{line=frame linewidth=0.8} " . "{line=other linewidth=0.3}}"; /* Place the table instance */ $result = $p->fit_table($tbl, $llx, $lly, $urx, $ury, $optlist); /* Check the result; "_stop" means all is ok */ if (!$result == "_stop") { if ($result == "_error") throw new Exception("Error: " . $p->get_errmsg()); else { /* Other return values require dedicated code to deal with */ } } /* Delete the table handle. This will also delete any Textflow handles * used in the table */ $p->delete_table($tbl, ""); $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=spread_text_over_cells.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; ?>