interchange/associated_files
Demonstrate associated files at various locations of the document (requires PDF 2.0 output).
Download PHP Code Switch to Java Code Show Output
<?php
/*
* Associated files:
* Attach associated files to various locations in the document:
* - document
* - pages
* - Form XObjects, e.g. imported PDF pages and SVG graphics
* - Image XObjects
*
* The following locations are also possible, but are not demonstrated here:
* - annotations
* - structure element hierarchy
* - structure elements
* - marked content
* - DPart (document part) node
* - XMP metadata
*
* Required software: PDFlib/PDFlib+PDI/PPS 9
* Required data: SVG graphics, image, PDF document,
* suitable associated files (we use the same dummy text file for all objects)
*/
/* 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 = "";
$graphicsfile = "PDFlib-logo.svg";
$imagefile = "nesrin.jpg";
$pdffilename = "PDFlib-datasheet.pdf";
/* Dummy text file; could contain arbitrary contents */
$af_path = "af1.txt";
$x = 10; $y = 500;
$exitcode = 0;
try {
$p = new PDFlib();
$p->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;
?>