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"); 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"); /* Based on the imported page output several pages with the blocks * being filled with data related to different persons */ for ($i = 0; $i < $npersons; $i++) { /* 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, ""); /* Loop over all blocks on the page */ for ($j = 0; $j < $nblocks; $j++) { /* Fill the j-th block with the corresponding entry of the * persons array. The "bordercolor" and "linewidth" options are * only used to illustrate the block rectangles */ $optlist = "bordercolor={gray 0.5} linewidth=0.25"; if ($p->fill_textblock($inpage, $blocknames[$j], $persons[$i][$j], $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=business_cards.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; ?>