set_option("searchpath={" . $searchpath . "}"); /* This means we must check return values of load_font() etc. */ $p->set_option("errorpolicy=return"); if ($p->begin_document($outfile, "") == 0){ echo("Error: " . $p->get_errmsg(p)); exit(1); } $p->set_info("Creator", "PDFlib Cookbook"); $p->set_info("Title", $title); /* Start a page */ $p->begin_page_ext(0, 0, "width=a4.width height=a4.height"); /* Load the image file with its integrated clipping path * Add the following option to retrieve a particular path by * name: "clippingpathname={Path 47}". * "infomode" means that the pixel data is ignored and * not loaded into the PDF to reduce output file size. */ $image = $p->load_image("auto", $imagefile, "infomode"); if ($image == -1) throw new Exception("Error: " . $p->get_errmsg()); /* * You can supply formatting options here so that the * path will be formatted the same way as an image. */ $formatting_options = "boxsize={300 300}"; /* Supply the formatting options when retrieving * the clipping path to ensure correct scaling and placement. */ $path = (int) $p->info_image($image, "clippingpath", $formatting_options); if ($path == 0) throw new Exception("Error: image '" . $imagefile . "' doesn't contain clipping path"); /* Mark the target location with a red circle for illustration */ $p->set_graphics_option("fillcolor=red"); $p->circle($target_x, $target_y, 5); $p->fill(); $p->save(); /* Optional: retrieve the bounding box of the placed image and * clip the imported path to this box. This may be useful if the * image contains irrelevant additional paths outside the image * area. However, if you need the additional paths you should * skip the bbox clipping. */ $bbox = (int) $p->info_image($image, "boundingbox", $formatting_options); $p->draw_path($bbox, $target_x, $target_y, "close clip"); /* Draw the path at the target location */ $pathoptlist = "close stroke strokecolor=red fill fillcolor=blue " . "linewidth=2 linejoin=round linecap=round "; $p->draw_path($path, $target_x, $target_y, $pathoptlist . $formatting_options); $p->restore(); // Delete the path objects $p->delete_path($path); $p->delete_path($bbox); /* Close the image */ $p->close_image($image); $p->end_page_ext(""); $p->end_document(""); $buf = $p->get_buffer(); $len = strlen($buf); header("Content-type: application/pdf"); header("Content-Length: $len"); header("Content-Disposition: inline; filename=import_path_from_image.pdf"); print $buf; } catch (PDFlibException $e) { echo("PDFlib exception occurred in import_path_from_image sample:\n" . "[" . $e->get_errnum() . "] " . $e->get_apiname() . ": " . $e->get_errmsg() . "\n"); exit(1); } catch (Throwable $e) { echo($e); exit(1); } $p = 0; ?>