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 font */ $font = $p->load_font("NotoSerif-Bold", "unicode", ""); if ($font == 0) throw new Exception("Error: " . $p->get_errmsg()); $p->begin_page_ext(0, 0, "width=a4.width height=a4.height"); $p->setfont($font, $fsize); $p->setcolor("fillstroke", "rgb", 1, 0, 0, 0); /* ------------------------------------------------------------------ * Use simple text output functions for text output. The current text * position is moved to the end of the output text. * ------------------------------------------------------------------ */ /* Set the position for text output on the page */ $p->set_text_pos($llx, $ury + 50); /* Output text at the current text position */ $p->show("LORE IPSUM "); /* Output text at the end of the preceding text */ $p->show("DOLOR SIT AMET,"); /* Retrieve the current text position */ $textx = $p->get_option("textx", ""); $texty = $p->get_option("texty", ""); /* Output text while setting the current text position to about one * line below */ $p->show_xy("CONSECTETUR", $llx, $texty -= ($fsize*1.2)); /* ----------------------------------------------------- * Output a text line based on the current text position * ----------------------------------------------------- */ $p->fit_textline("ADIPISICING ELIT...", $llx, ($texty -= $fsize*1.2), ""); /* ----------------------------------------------------------------- * Output a Textflow and use the current text position at the end of * the Textflow to stroke some lines * ----------------------------------------------------------------- */ /* Create some amount of dummy text and feed it to a Textflow * object with alternating options. */ for ($i=1; $i<=$count; $i++) { $num = $i . " "; $tf = $p->add_textflow($tf, $num, $optlist2); if ($tf == 0) throw new Exception("Error: " . $p->get_errmsg()); $tf = $p->add_textflow($tf, $text, $optlist1); if ($tf == 0) throw new Exception("Error: " . $p->get_errmsg()); } /* Retrieve the current text position */ $textx = $p->get_option("textx", ""); $texty = $p->get_option("texty", ""); $texty -= 30; /* Loop until all of the text is placed. "_boxfull" means we must * continue because there is more text */ do { $optlist = "verticalalign=justify linespreadlimit=120% "; $result = $p->fit_textflow($tf, $llx, $lly, $urx, $texty, $optlist); } while ($result == "_boxfull"); /* Draw a Z line after the Textflow if the Textflow has been placed * correctly */ if ($result == "_stop") { /* Get the current text position at the end of the Textflow */ $textx = $p->get_option("textx", ""); $texty = $p->get_option("texty", ""); /* Stroke Z line at the end of the Textflow */ $p->setcolor("stroke", "rgb", 1, 0, 0, 0); $p->setlinewidth(2); $texty += 4; $p->moveto($textx, $texty); $p->lineto($urx, $texty); $p->lineto($llx, $lly); $p->lineto($urx, $lly); $p->stroke(); } /* Check for errors */ else { /* "_boxempty" happens if the box is very small and doesn't * hold any text at all. */ if ($result == "_boxempty") throw new Exception ("Error: Textflow box too small"); 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 '" . $result . "' found in Textflow"); } } $p->delete_textflow($tf); $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=current_text_position.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; ?>