pdf_import/rotate_pages updated
Rotate the pages of an existing PDF document.
Download PHP Code Switch to Java Code Show Output
<?php
/*
* Rotate Pages:
* Rotate the pages of an existing PDF document
*
* Import a PDF page and place it in the output document with a different
* orientation using different methods:
* - option "orientate": the simplest approach
* - option "rotate": requires translation of the reference point
* - option "transform": allows arbitrary transformations of the page
* including rotations which are not necessarily a multiple of 90 degrees
*
* Required software: PDFlib+PDI/PPS 9
* Required data: PDF document
*/
/* 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 = "Rotate Pages";
$pdffile = "PDFlib-real-world.pdf";
try {
$p = new pdflib();
$p->set_option("searchpath={" . $searchpath . "}");
$p->set_option("searchpath={" . $fontpath . "}");
/* This means we must check return values of open_pdi_document() 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);
/* *************************************************
* Variant 1: PDI page option "orientate"
* This is the simplest method; it can be used for each of
* the four directions north/east/south/west without any
* additional options or adjustments.
*/
/* Open input PDF */
$indoc = $p->open_pdi_document($pdffile, "");
if ($indoc == 0)
throw new Exception("Error: " . $p->get_errmsg());
$page = $p->open_pdi_page($indoc, 1, "");
if ($page == 0)
throw new Exception("Error: " . $p->get_errmsg());
/* Page size with swapped width and height */
$p->begin_page_ext(50, 50, "");
/* Orientate the page to the west and use the lower left corner
* (0, 0) as reference point for placing the page.
*
* The option "adjustpage" changes the dimensions of the output
* page to those of the placed PDI page.
*/
$p->fit_pdi_page($page, 0, 0, "orientate=west adjustpage");
$p->close_pdi_page($page);
$p->close_pdi_document($indoc);
$p->end_page_ext("");
/* *************************************************
* Variant 2: PDI page fitting option "rotate"
*/
/* Open input PDF */
$indoc = $p->open_pdi_document($pdffile, "");
if ($indoc == 0)
throw new Exception("Error: " . $p->get_errmsg());
$page = $p->open_pdi_page($indoc, 1, "");
if ($page == 0)
throw new Exception("Error: " . $p->get_errmsg());
/* Retrieve page dimension with pCOS and supply output page size as
* swapped width and height of the input page.
*/
$pagewidth = $p->pcos_get_number($indoc, "pages[0]/width");
$pageheight = $p->pcos_get_number($indoc, "pages[0]/height");
$p->begin_page_ext($pageheight, $pagewidth, "");
/* Use the "rotate" option; we must move the reference point
* for placing the page from the lower left corner (0, 0) of the
* output page to the lower right corner (pageheight, 0).
*/
$p->fit_pdi_page($page, $pageheight, 0, "rotate=90");
$p->close_pdi_page($page);
$p->close_pdi_document($indoc);
$p->end_page_ext("");
/* *************************************************
* Variant 3: PDI page open option "transform"
* The most general approach is the "transform" option.
* It must be applied to $p->open_pdi_page(). Since it sticks to
* the imported page, the page can be placed at the lower left corner
* and $p->fit_pdi_page() doesn't require any changes.
*/
/* Open input PDF */
$indoc = $p->open_pdi_document($pdffile, "");
if ($indoc == 0)
throw new Exception("Error: " . $p->get_errmsg());
/* Translate and rotate the page; additional transformations can
* be added and will be applied in the specified order.
*/
$page = $p->open_pdi_page($indoc, 1, "transform={translate={" . $pageheight . " 0} rotate=90}");
if ($page == 0)
throw new Exception("Error: " . $p->get_errmsg());
/* Retrieve page dimension with pCOS and supply output page size as
* swapped width and height of the input page.
*/
$pagewidth = $p->pcos_get_number($indoc, "pages[0]/width");
$pageheight = $p->pcos_get_number($indoc, "pages[0]/height");
$p->begin_page_ext($pageheight, $pagewidth, "");
/* No additional options required for fitting the page */
$p->fit_pdi_page($page, 0, 0, "");
$p->close_pdi_page($page);
$p->close_pdi_document($indoc);
$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=rotate_pages.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($e);
exit(1);
}
$p = 0;
?>