pdf_import/shift_imported_page new
Place an imported PDF page and shift it horizontally and vertically.
Download PHP Code Switch to Java Code Show Output
<?php
/*
* Place an imported PDF page and shift it horizontally and vertically
*
* Required software: PDFlib+PDI/PPS 9
* Required data: input PDF
*/
/* 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 = "Shift Imported Page";
$infile = "lionel.pdf";
try {
/* Amount to shift imported page to the right and up */
$shift_x = 50;
$shift_y = 50;
$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");
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 page of the input PDF document */
$doc = $p->open_pdi_document($infile, "");
if ($doc == 0)
throw new Exception("Error: " . $p->get_errmsg());
$page = $p->open_pdi_page($doc, 1, "");
if ($page == 0)
throw new Exception("Error: " . $p->get_errmsg());
/* --------------------------------------------------------
* Place imported page at the lower left corner of the page
* --------------------------------------------------------
*/
$p->begin_page_ext(0, 0, "width=a4.height height=a4.width");
/* Place PDF page in lower left corner */
$p->fit_pdi_page($page, 0, 0, "");
$p->end_page_ext("");
/* --------------------------------------------------------
* Output the page at the with a shift of the page
* --------------------------------------------------------
*/
$p->begin_page_ext(0, 0, "width=a4.height height=a4.width");
/* Place PDF page in its original size, but shift it to the right and up */
$p->fit_pdi_page($page, $shift_x, $shift_y, "");
$p->end_page_ext("");
$p->close_pdi_page($page);
$p->close_pdi_document($doc);
$p->end_document("");
/*
* Return the sub-document to the user. If all split documents are to
* be processed, do something different, e.g. write the documents
* to disk and create an HTML page with a list of links for the
* sub-documents.
*/
$buf = $p->get_buffer();
$len = strlen($buf);
header("Content-type: application/pdf");
header("Content-Length: $len");
header("Content-Disposition: inline; filename=" . $outfile);
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($e);
exit(1);
}
$p = 0;
?>