set_option("searchpath={" . $searchpath . "}"); /* This means we must check return values of load_font() etc. */ $p->set_option("errorpolicy=return"); /* Start the document */ 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("NotoSerif-Regular", "unicode", ""); if ($font == 0) throw new Exception("Error: " . $p->get_errmsg()); /* * Create a 3D view that only defines the background color. */ $optlist = "type=PRC background={fillcolor=mediumslateblue}"; if (($view = $p->create_3dview("Default", $optlist)) == 0) throw new Exception("Error: " . $p->get_errmsg()); make_3d_page($p, $font, $view, "View model along x axis", $JS_X_AXIS); make_3d_page($p, $font, $view, "View model along y axis", $JS_Y_AXIS); make_3d_page($p, $font, $view, "View model along z axis", $JS_Z_AXIS); $p->end_document(""); $buf = $p->get_buffer(); $len = strlen($buf); header("Content-type: application/pdf"); header("Content-Length: $len"); header("Content-Disposition: inline; filename=javascript_for_3d_camera.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; /** * Display the 3D annotation and the JavaScript to position/align the * camera side-by-side on a landscape PDF page. * * @param p * PDFlib object * @param data * 3D data * @param view * 3D default view * @param font * font for displaying the JavaScript code * @param title * title to display above the JavaScript code * @param javascript * JavaScript code to position and align the camera * * @throws PDFlibException * @throws Exception */ function make_3d_page($p, $font, $view, $title, $javascript){ /** * Page width in points (landscape). */ $WIDTH = 842; /** * Page height in points (landscape). */ $HEIGHT = 595; /** * Margin around textflow and 3D annotation. */ $MARGIN = 50; $tf = 0; $element_width = ($WIDTH - 3 * $MARGIN) / 2; $element_height = $HEIGHT - 2 * $MARGIN; $tf_xpos = $MARGIN; $tf_ypos = $MARGIN; $tf_width = $element_width; $_3d_xpos = 2 * $MARGIN + $element_width; $_3d_ypos = $MARGIN; $_3d_width = $element_width; $optlist = "font=" . $font . " fontsize=14 underline=true"; $tf = $p->add_textflow($tf, $title . "\n\n", $optlist); $optlist = "font=" . $font . " fontsize=12 underline=false"; $tf = $p->add_textflow($tf, "JavaScript code:\n\n", $optlist); $tf = $p->add_textflow($tf, $javascript, ""); $p->begin_page_ext($WIDTH, $HEIGHT, ""); /* Create a bookmark for jumping to this page */ $p->create_bookmark($title, ""); $p->fit_textflow($tf, $tf_xpos, $tf_ypos, $tf_xpos + $tf_width, $tf_ypos + $element_height, "fitmethod=auto"); /* * Load some 3D data with the view defined above */ $data = $p->load_3ddata("riemann.prc", "type=PRC views={" . $view . "} " . "script={" . $javascript . "}"); if ($data == 0) throw new Exception("Error: " . $p->get_errmsg()); /* * Create an annotation containing the loaded 3D data with the * defined 3D view as the initial view */ $optlist = "contents=PRC 3Ddata= " . $data . " " . "3Dactivate={enable=open} 3Dinitialview=" . $view; $p->create_annotation($_3d_xpos, $_3d_ypos, $_3d_xpos + $_3d_width, $_3d_ypos + $element_height, "3D", $optlist); $p->end_page_ext(""); } ?>