blocks/create_blocks_with_poca
Create PDFlib Blocks with the POCA interface.
Download PHP Code Switch to Java Code Show Output
<?php
/*
* Create PDFlib Blocks with the POCA interface
*
* Required software: PPS 9
* Required data: none
*/
/* This is where the data files are. Adjust as necessary. */
$searchpath = dirname(__FILE__,3)."/input";
$title = "Create PDFlib Blocks with POCA";
try {
$p = new PDFlib();
$p->set_option("searchpath={" . $searchpath . "}");
/* This means we must check return values of load_font() etc. */
$p->set_option("errorpolicy=return");
if ($p->begin_document("", "") == 0)
throw new Exception("Error: " . $p->get_errmsg());
$p->set_info("Creator", "PDFlib Cookbook");
$p->set_info("Title", $title );
$p->begin_page_ext(0, 0, "width=a4.width height=a4.height");
$font = $p->load_font("NotoSerif-Bold", "unicode", "");
if ($font == 0)
throw new Exception("Error: " . $p->get_errmsg());
$p->fit_textline(
"This document contains PDFlib Blocks generated with POCA",
50, 800, "font=" . $font . " fontsize=12");
/* Create the Block dictionary */
$blockdict = $p->poca_new("containertype=dict usage=blocks");
/* ---------------------------------------------------------
* Create a Text Block
* ---------------------------------------------------------
*/
$blockname = "job_title";
$textblock = $p->poca_new("containertype=dict usage=blocks " .
"type=name key=Type value=Block");
$container1 = $p->poca_new("containertype=array usage=blocks " .
"type=integer values={70 640 300 700}");
$p->poca_insert($textblock, "type=array key=Rect value=" . $container1);
$p->poca_insert($textblock, "type=name key=Name value=" . $blockname);
$p->poca_insert($textblock, "type=name key=Subtype value=Text");
$p->poca_insert($textblock, "type=name key=fitmethod value=auto");
$p->poca_insert($textblock, "type=string key=fontname value=NotoSerif-Regular");
$p->poca_insert($textblock,
"type=string key=defaulttext value={Block created with POCA}");
$p->poca_insert($textblock, "type=float key=fontsize value=12");
$p->poca_insert($textblock, "type=percentage key=horizscaling value=105%");
$p->poca_insert($textblock,
"type=color key=fillcolor value={rgb 0 0.3 0.57}");
$container2 = $p->poca_new("containertype=dict usage=blocks " .
"type=integer key=format value=5");
$p->poca_insert($textblock, "type=dict key=Custom value=" . $container2);
/*
* Hook up the Block in the page's Block dictionary. The PDFlib
* Block specification requires that the key of the dictionary
* entry is identical to the "Name" value inside the block
* dictionary
*/
$p->poca_insert($blockdict, "type=dict key=" . $blockname
. " direct=false value=" . $textblock);
/* ---------------------------------------------------------
* Create an Image Block
* ---------------------------------------------------------
*/
$blockname = "logo";
$imageblock = $p->poca_new("containertype=dict usage=blocks " .
"type=name key=Type value=Block");
$container3 = $p->poca_new("containertype=array usage=blocks " .
"type=integer values={70 440 300 600}");
$p->poca_insert($imageblock, "type=array key=Rect value=" . $container3);
$p->poca_insert($imageblock, "type=name key=Name value=" . $blockname);
$p->poca_insert($imageblock, "type=name key=Subtype value=Image");
$p->poca_insert($imageblock, "type=name key=fitmethod value=auto");
/* Hook up the Block in the page's Block dictionary */
$p->poca_insert($blockdict, "type=dict key=" . $blockname
. " direct=false value=" . $imageblock);
/* ---------------------------------------------------------
* Create a PDF Block
* ---------------------------------------------------------
*/
$blockname = "pdflogo";
$pdfblock = $p->poca_new("containertype=dict usage=blocks " .
"type=name key=Type value=Block");
$container4 = $p->poca_new("containertype=array usage=blocks " .
"type=integer values={70 240 300 400}");
$p->poca_insert($pdfblock, "type=array key=Rect value=" . $container4);
$p->poca_insert($pdfblock, "type=name key=Name value=" . $blockname);
$p->poca_insert($pdfblock, "type=name key=Subtype value=PDF");
$p->poca_insert($pdfblock, "type=name key=fitmethod value=meet");
/* Hook up the Block in the page's Block dictionary */
$p->poca_insert($blockdict, "type=dict key=" . $blockname
. " direct=false value=" . $pdfblock);
/* Hook up the Block dictionary in the current page */
$p->end_page_ext("blocks=" . $blockdict);
/* Clean up */
$p->poca_delete($blockdict, "recursive=true");
$p->end_document("");
$buf = $p->get_buffer();
$len = strlen($buf);
header("Content-type: application/pdf");
header("Content-Length: $len");
header("Content-Disposition: inline; filename=create_blocks_with_poca.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;
?>