path_objects/svg_path
Create a path object from a description in SVG path syntax.
Download PHP Code Switch to Java Code Show Output
<?php
/*
* Create a path object from a description in SVG path syntax
*
* Required software: PDFlib/PDFlib+PDI/PPS 9
* Required data: none
*
*/
$outfile = "";
$exitcode = 0;
$target_x = 200; $target_y = 500; // target location
try {
$p = new PDFlib();
$title = "SVG path";
/* 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 an A4 page */
$p->begin_page_ext(0, 0, "width=a4.width height=a4.height");
/* Path description in SVG syntax as required by the "d" attribute
* of the "path" element in SVG
* (see https://www.w3.org/TR/SVG11/paths.html#PathData)
*/
$svgPath =
"M238.458,42.761c0-26.02-17.289-39.467-48.722-39.467 " .
"c-12.922,0-24.797,0.349-35.625,0.873v117.877h32.481V85.72c2.27,0.175,4.191,0.175,4.889,0.175" .
"C223.09,85.895,238.458,65.637,238.458,42.761z M204.928,43.809c0,9.779-7.509,15.019-15.892,15.019" .
"c-0.523,0-1.222,0-2.444-0.175V29.838c1.397-0.174,2.619-0.174,3.143-0.174C200.912,29.664,204.928,36.125,204.928,43.809z";
/* Create a path object from the SVG path description above */
$path = $p->add_path_point(0, 0, 0, "addpath", "svgpath = {" . $svgPath . "} close");
/* Retrieve coordinates of the lower left corner of the path's bounding box */
$mirroropt = "scale={1 -1}";
/* Retrieve the coordinates of the desired corner of the path's
* bounding box, e.g. (x1, y1) = lower left corner
*/
$corner_x = $p->info_path($path, "x1", $mirroropt);
$corner_y = $p->info_path($path, "y1", $mirroropt);
/* 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();
/* ----------------------------------------------------------------
* Draw the path at the selected target location and stroke it
* If the path directly stems from an SVG file it uses top-down
* coordinates which must be adjusted:
* - supply "scale={1 -1}" for unmirroring the path
* - draw the path at (target_x-corner_x, target_y-corner_y)
* to place the selected corner of the path's bounding box
* at the desired target location
* - if you use the "topdown" page option draw the path at
* (target_x-corner_x, target_y+corner_y) instead
* ----------------------------------------------------------------
*/
$p->draw_path($path, $target_x-$corner_x, $target_y-$corner_y,
$mirroropt .
" stroke linewidth=3 fill fillcolor=Turquoise linecap=round");
$p->delete_path($path);
$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=svg_path.pdf");
print $buf;
}
catch (PDFlibException $e) {
echo("PDFlib exception occurred in svg_path sample:\n" .
"[" . $e->get_errnum() . "] " . $e->get_apiname() . ": " .
$e->get_errmsg() . "\n");
exit(1);
}
catch (Throwable $e) {
echo($e);
exit(1);
}
$p = 0;
?>