set_option("SearchPath=" . $searchpath); $p->set_option("SearchPath=" . $fontpath); /* This means we must check return values of load_graphics() etc. */ $p->set_option("errorpolicy=return"); $p->set_info("Creator", "PDFlib Cookbook"); $p->set_info("Title", "associated_files"); if ($p->begin_document($outfile, "compatibility=2.0") == 0) throw new Exception("Error: " . $p->get_errmsg()); /* ********************************************************** * Attach associated file to an image */ $af = $p->load_asset("Attachment", $af_path, "mimetype=text/plain " . "description={Associated file for image} " . "relationship=Data documentattachment=true " . "name={af1}"); if ($af == 0) throw new Exception("Error: " . $p->get_errmsg()); $image = $p->load_image("auto", $imagefile, "associatedfiles={" . $af . "}"); if ($image == -1) throw new Exception("Error: " . $p->get_errmsg()); /* ********************************************************** * Attach associated file to a page */ $af = $p->load_asset("Attachment", $af_path, "mimetype=text/plain " . "description={Associated file for page} " . "relationship=Data documentattachment=true " . "name={af2}"); if ($af == 0) throw new Exception("Error: " . $p->get_errmsg()); $p->begin_page_ext(0, 0, "width=a4.width height=a4.height " . "associatedfiles={" . $af . "}"); $p->fit_image($image, 10, 10, "boxsize={400 400} position={center} fitmethod=meet"); $p->close_image($image); $p->end_page_ext(""); /* ********************************************************** * Attach associated file to an imported PDF page */ $af = $p->load_asset("Attachment", $af_path, "mimetype=text/plain " . "description={Associated file for imported PDF page} " . "relationship=Data documentattachment=true " . "name={af3}"); if ($af == 0) throw new Exception("Error: " . $p->get_errmsg()); /* Open the input PDF */ $doc = $p->open_pdi_document($pdffilename, ""); if ($doc == 0) throw new Exception("Error: " . $p->get_errmsg()); $page = $p->open_pdi_page($doc, 1, "associatedfiles={" . $af . "}"); if ($page == 0) throw new Exception("Error: " . $p->get_errmsg()); $p->begin_page_ext(0, 0, "width=a4.width height=a4.height"); $p->fit_pdi_page($page, 0, 0, "adjustpage"); $p->close_pdi_page($page); $p->close_pdi_document($doc); $p->end_page_ext(""); /* ********************************************************** * Attach associated file to imported SVG graphics */ $p->begin_page_ext(0, 0, "width=a4.width height=a4.height"); /* Attach associated file at Form XObject level */ $af = $p->load_asset("Attachment", $af_path, "mimetype=text/plain " . "description={Associated file for SVG graphics} " . "relationship=Data documentattachment=true " . "name={af4}"); if ($af == 0) throw new Exception("Error: " + $p->get_errmsg()); /* Load the graphics */ $graphics = $p->load_graphics("auto", $graphicsfile, "templateoptions={associatedfiles={" . $af . "}}"); if ($graphics == -1) throw new Exception("Error: " . $p->get_errmsg()); $optlist = "boxsize={400 400} position={center} fitmethod=meet"; $p->fit_graphics($graphics, $x, $y, $optlist); $p->close_graphics($graphics); $p->end_page_ext(""); /* ********************************************************** * Attach associated file to the document */ $af = $p->load_asset("Attachment", $af_path, "mimetype=text/plain " . "description={Associated file for document} " . "relationship=Data documentattachment=true " . "name={af5}"); if ($af == -1) throw new Exception("Error: " . $p->get_errmsg()); $p->end_document("associatedfiles={" . $af . "}"); $buf = $p->get_buffer(); $len = strlen($buf); header("Content-type: application/pdf"); header("Content-Length: $len"); header("Content-Disposition: inline; filename=associated_files.pdf"); print $buf; } catch (PDFlibException $e) { echo("PDFlib exception occurred in associated_files sample:\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; ?>