set_option("searchpath={" . $searchpath . "}"); /* This means we must check return values of load_font() etc. */ $p->set_option("errorpolicy=return"); $p->set_option("stringformat=utf8"); 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 the input PDF having A4 page size */ $indoc = $p->open_pdi_document($pdffile, ""); if ($indoc == 0) throw new Exception("Error: " . $p->get_errmsg()); $endpage = $p->pcos_get_number($indoc, "length:pages"); /* Loop over all pages of the input document: * Place the imported page without any scaling */ for ($pageno = 1; $pageno <= $endpage; $pageno++) { $page = $p->open_pdi_page($indoc, $pageno, ""); if ($page == 0) throw new Exception("Error: " . $p->get_errmsg()); /* Start an A4 output page (210mm x 297mm) */ $p->begin_page_ext(0, 0, "width=a4.width height=a4.height"); /* Place the imported page without any scaling */ $p->fit_pdi_page($page, 0, 0, ""); $p->end_page_ext(""); $p->close_pdi_page($page); } /* Loop over all pages of the input document: * Place the imported page while scaling it down to A5 */ for ($pageno = 1; $pageno <= $endpage; $pageno++) { $page = $p->open_pdi_page($indoc, $pageno, ""); if ($page == 0) throw new Exception("Error: " . $p->get_errmsg()); /* Start an A5 output page (148mm x 210mm) */ $p->begin_page_ext(421, 595, ""); /* Place the imported page while scaling it down to A5 */ $p->fit_pdi_page($page, 0, 0, "boxsize={421 595} fitmethod=meet"); $p->end_page_ext(""); $p->close_pdi_page($page); } $p->end_document(""); $buf = $p->get_buffer(); $len = strlen($buf); header("Content-type: application/pdf"); header("Content-Length: $len"); header("Content-Disposition: inline; filename=scale_down_imported_pages.pdf"); print $buf; } catch (PDFlibException $e) { echo("PDFlib exception occurred in scale_down_imported_pages sample:\n" . "[" . $e->get_errnum() . "] " . $e->get_apiname() . ": " . $e->get_errmsg() . "\n"); exit(1); } catch (Exception $e) { echo($e); exit(1); } $p = 0; ?>