blocks/fill_converted_formfields
Output an imported PDF page with its Blocks filled with different personalized data.
Download PHP Code Switch to Java Code Show Output Show Input (form_with_blocks.pdf)
<?php
/*
* Fill converted form fields:
* Output an imported PDF page with its blocks being filled with different
* personalized data
*
* Import a PDF page whose form fields have been converted to blocks.
* Fill some blocks representing text fields, checkboxes and radio buttons
* appropriately.
*
* Required software: PPS 10
* Required data: PDF document containing blocks
*/
/* This is where the data files are. Adjust as necessary. */
$searchpath = dirname(__FILE__,3)."/input";
/* By default annotations are also imported. In some cases this
* requires the Noto fonts for creating annotation appearance streams.
* We therefore set the searchpath to also point to the font directory.
*/
$fontpath = dirname(__FILE__,3)."/resource/font";
$outfile = "";
$title = "Fill Converted Form Fields";
$infile = "form_with_blocks.pdf";
/* The following blocks are contained in the imported page:
* "plane model" representing a text field
* "quantity" representing a text field
* "color" and "color_1" representing two radio buttons
* "perforation" and "glossy" representing two checkboxes
*/
try {
$p = new PDFlib();
$p->set_option("searchpath={" . $searchpath . "}");
$p->set_option("searchpath={" . $fontpath . "}");
/* This means we must check return values of load_font() etc. */
$p->set_option("errorpolicy=return");
$p->set_option("escapesequence=true");
if ($p->begin_document($outfile, "") == 0)
throw new Exception("Error: " . $p->get_errmsg());
$p->set_info("Creator", "PDFlib Cookbook");
$p->set_info("Title", $title);
/* Open a PDF containing blocks */
$indoc = $p->open_pdi_document($infile, "");
if ($indoc == 0)
throw new Exception("Error: " . $p->get_errmsg());
/* Open the first page */
$inpage = $p->open_pdi_page($indoc, 1, "");
if ($inpage == 0)
throw new Exception("Error: " . $p->get_errmsg());
/* Get the width and height of the imported page */
$width = $p->pcos_get_number($indoc, "pages[0]/width");
$height = $p->pcos_get_number($indoc, "pages[0]/height");
/* Start the output page with the size given by the imported page */
$p->begin_page_ext($width, $height, "");
/* Place the imported page on the output page */
$p->fit_pdi_page($inpage, 0, 0, "");
/* Fill the "plane model" block with some text */
if ($p->fill_textblock($inpage, "plane model", "Long Distance Glider", "") == 0)
trigger_error("Warning: " . $p->get_errmsg() . "\n");
/* Fill the "quantity" text block with a quantity of 1 */
if ($p->fill_textblock($inpage, "quantity", "1", "") == 0)
trigger_error("Warning: " . $p->get_errmsg() . "\n");
/* Fill the "color" block (representing a radiobutton) with a circle.
* The circle is supplied as character reference &.#6C; ("l"). The
* encoding "builtin" will retrieve the circle symbol from the encoding
* which is integrated in the * ZapfDingbats font.
* The following symbols are represented by the respective characters in
* the ZapfDingbats font: Check=4 (&.#34;), Circle=1 (&.#6C;), Cross=8
* (&.#38;), Diamond=u (&.#75;), Square=n (&.#6E;), Star=H (&.#48;)
*/
$text_optlist = "fontname=ZapfDingbats encoding=builtin position=center charref=true";
if ($p->fill_textblock($inpage, "color", "&.#6C;",
$text_optlist) == 0)
trigger_error("Warning: " . $p->get_errmsg() . "\n");
/* Fill the "color_1" block (for a radiobutton) with a space. Otherwise,
* the border of the block representing the empty check box would not be
* shown in the output document.
*/
$text_optlist = "fontname=ZapfDingbats encoding=builtin position=center";
if ($p->fill_textblock($inpage, "color_1", " ",
$text_optlist) == 0)
trigger_error("Warning: " . $p->get_errmsg() . "\n");
/* Fill the "perforation" block (for a checkbox) with a check mark
* encoded in Unicode.
*/
$text_optlist = "fontname=ZapfDingbats encoding=unicode fontsize=12 position=center ";
if ($p->fill_textblock($inpage, "perforation", "\xe2\x9c\x94",
$text_optlist) == 0)
trigger_error("Warning: " . $p->get_errmsg() . "\n");
/* Fill the "glossy" block (for a checkbox) with a space. Otherwise,
* the border of the block representing the empty check box would not be
* shown in the output document.
*/
$text_optlist = "fontname=ZapfDingbats encoding=unicode fontsize=12 position=center";
if ($p->fill_textblock($inpage, "glossy", " ",
$text_optlist) == 0)
trigger_error("Warning: " . $p->get_errmsg() . "\n");
$p->end_page_ext("");
$p->close_pdi_page($inpage);
$p->end_document("");
$p->close_pdi_document($indoc);
$buf = $p->get_buffer();
$len = strlen($buf);
header("Content-type: application/pdf");
header("Content-Length: $len");
header("Content-Disposition: inline; filename=fill_converted_formfields.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;
?>