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); $font = $p->load_font("Helvetica-Bold", "unicode", ""); if ($font == 0) throw new Exception("Error: " . $p->get_errmsg()); /* ---------------------------------- * Create the first page in A4 format * ---------------------------------- */ $p->begin_page_ext(0, 0, "width=a4.width height=a4.height"); /* Draw a violet rectangle in A5 format */ $p->setcolor("fill", "rgb", 0.7, 0, 0.6, 0); $p->rect(87, 123, 421, 595); $p->fill(); /* Output some explanatory text */ $p->fit_textline("A4 page uncropped", 50, 50, "font=" . $font . " fontsize=20 fillcolor={gray 0}"); /* Create a "GoTo" action for opening the current page (page=0) with * a zoom to completely fit it into the window (type=fitwindow) */ $optlist = "destination={page=0 type=fitwindow}"; $action = $p->create_action("GoTo", $optlist); /* Finish the page with the "GoTo" action being applied. When the page * is opened in the viewer it will completely fit into the window. */ $p->end_page_ext("action={open=" . $action . "}"); /* ------------------------------------------------------------- /* Create the second page in A4 format and crop it to an A5 page * ------------------------------------------------------------- */ $p->begin_page_ext(0, 0, "width=a4.width height=a4.height"); /* Draw a violet rectangle in A5 format */ $p->setcolor("fill", "rgb", 0.7, 0, 0.6, 0); $p->rect(87, 123, 421, 595); $p->fill(); /* Output some explanatory text */ $p->fit_textline("A4 page cropped to A5", 95, 140, "font=" . $font . " fontsize=20 fillcolor={gray 0}"); /* Create a "GoTo" action for opening the current page with the same * zoom as chosen for the A4 page. This is accomplished by supplying * the size of the A4 page as the rectangle to completely fit into * the window. */ $optlist = "destination={page=0 type=fitrect left=0 bottom=0 " . "right=595 top=842}"; $action = $p->create_action("GoTo", $optlist); /* Finish the page and crop it to the A5 format. In addition, the * "GoTo" action is applied. When the page is opened in the viewer it * will be displayed with the same zoom as the A4 page. */ $p->end_page_ext("cropbox={87 123 508 718} action={open=" . $action . "}"); $p->end_document(""); $buf = $p->get_buffer(); $len = strlen($buf); header("Content-type: application/pdf"); header("Content-Length: $len"); header("Content-Disposition: inline; filename=crop_page.pdf"); print $buf; } catch (PDFlibException $e) { echo("PDFlib exception occurred:\n". "[" . $e->get_errnum() . "] " . $e->get_apiname() . ": " . $e->get_errmsg() . "\n"); exit(1); } catch (Exception $e) { echo($e->getMessage()); exit(1); } $p = 0; ?>