"Fossies" - the Fresh Open Source Software Archive

Member "PDFlib-Lite-7.0.5p3/bind/pdflib/php/starter_graphics.php" (6 Jun 2012, 7533 Bytes) of package /linux/misc/old/PDFlib-Lite-7.0.5p3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "starter_graphics.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 #!/usr/bin/perl
    3 # $Id: starter_graphics.php,v 1.1.2.1 2007/12/29 23:26:05 rjs Exp $
    4 # Starter Graphics:
    5 # Create some basic examples of vector graphics
    6 #
    7 # Stroke a line, curve, circle, arc, and rectangle using the current line width
    8 # and stroke color. Stroke and fill a rectangle.
    9 # Draw an arc segment by drawing a line and an arc, closing the path and
   10 # filling and stroking it.
   11 # Draw a rectangle and use it as the clipping a path. Draw and fill a circle
   12 # using the clipping path defined.
   13 #
   14 # Required software: PDFlib Lite/PDFlib/PDFlib+PDI/PPS 7
   15 # Required data: none
   16 
   17 # This is where the data files are. Adjust as necessary.
   18 $searchpath = "../data";
   19 $title = "Starter Graphics";
   20 
   21 # create a new PDFlib object
   22 $p = PDF_new();
   23 
   24 $xt=20;
   25 $x = 210;
   26 $y=770;
   27 $dy=90;
   28 
   29     PDF_set_parameter($p, "SearchPath", $searchpath);
   30 
   31     # This means we must check return values of load_font() etc.
   32     PDF_set_parameter($p, "errorpolicy", "return");
   33 
   34     if (PDF_begin_document($p, "", "") == 0) {
   35     die("Error: " .  PDF_get_errmsg($p));
   36     }
   37 
   38     PDF_set_info($p, "Creator", "PDFlib Cookbook");
   39     $buf = $title . '  $Revision: 1.1.2.1 $';
   40     PDF_set_info($p, "Title", $buf);
   41 
   42     # Load the font; for PDFlib Lite: change "unicode" to "winansi"
   43     $font = PDF_load_font($p, "Helvetica", "winansi", "");
   44     if ($font == 0) {
   45     die("Error: " .  PDF_get_errmsg($p));
   46     }
   47 
   48     # Start an A4 page
   49     PDF_begin_page_ext($p, 0, 0, "width=a4.width height=a4.height");
   50 
   51     # Set the font
   52     PDF_setfont($p, $font, 14);
   53 
   54     # Set the line width
   55     PDF_setlinewidth($p, 2.0);
   56 
   57     # Set the stroke color
   58     PDF_setcolor($p, "stroke", "rgb", 0.0, 0.5, 0.5, 0.0);
   59 
   60     # Set the fill color
   61     PDF_setcolor($p, "fill", "rgb", 0.0, 0.85, 0.85, 0.0);
   62 
   63 
   64     # -------------
   65     # Stroke a line
   66     # -------------
   67     
   68 
   69     # Set the current point for graphics output
   70     PDF_moveto($p, $x, $y);
   71 
   72     # Draw a line from the current point to the supplied point
   73     PDF_lineto($p, $x+300, $y+50);
   74 
   75     # Stroke the path using the current line width and stroke color, and
   76     # clear it
   77     
   78     PDF_stroke($p);
   79 
   80     # Output some descriptive black text
   81     PDF_fit_textline($p, "lineto() and stroke()", $xt, $y,
   82     "fillcolor={gray 0}");
   83 
   84 
   85     # --------------
   86     # Stroke a curve
   87     # --------------
   88     
   89 
   90     # Set the current point for graphics output
   91     PDF_moveto($p, $x, $y-=$dy);
   92 
   93     # Draw a Bézier curve from the current point to (x3, y3), using three
   94     # control points
   95     
   96     PDF_curveto($p, $x+50, $y+40, $x+200, $y+80, $x+300, $y+30);
   97 
   98     # Stroke the path using the current line width and stroke color, and
   99     # clear it
  100     
  101     PDF_stroke($p);
  102 
  103     # Output some descriptive black text
  104     PDF_fit_textline($p, "curveto() and stroke()", $xt, $y, 
  105     "fillcolor={gray 0}");
  106 
  107 
  108     # ---------------
  109     # Stroke a circle
  110     # ---------------
  111     
  112 
  113     # Draw a circle at position (x, y) with a radius of 40
  114     PDF_circle($p, $x, $y-=$dy, 40);
  115 
  116     # Stroke the path using the current line width and stroke color, and
  117     # clear it
  118     
  119     PDF_stroke($p);
  120 
  121     # Output some descriptive black text
  122     PDF_fit_textline($p, "circle() and stroke()", $xt, $y,
  123     "fillcolor={gray 0}");
  124 
  125 
  126     # ---------------------
  127     # Stroke an arc segment
  128     # ---------------------
  129     
  130 
  131     # Draw an arc segment counterclockwise at position (x, y) with a radius
  132     # of 40 starting at an angle of 90 degrees and ending at 180 degrees
  133     
  134     PDF_arc($p, $x, $y-=$dy+20, 40, 90, 180);
  135 
  136     # Stroke the path using the current line width and stroke color, and
  137     # clear it
  138     
  139     PDF_stroke($p);
  140 
  141     # Output some descriptive black text
  142     PDF_fit_textline($p, "arc() and stroke()", $xt, $y,
  143     "fillcolor={gray 0}");
  144 
  145 
  146     # ------------------
  147     # Stroke a rectangle
  148     # ------------------
  149     
  150 
  151     # Draw a rectangle at position (x, y) with a width of 200 and a height
  152     # of 50
  153     
  154     PDF_rect($p, $x, $y-=$dy, 200, 50);
  155 
  156     # Stroke the path using the current line width and stroke color, and
  157     # clear it
  158     
  159     PDF_stroke($p);
  160 
  161     # Output some descriptive black text
  162     PDF_fit_textline($p, "rect() and stroke()", $xt, $y,
  163     "fillcolor={gray 0}");
  164 
  165 
  166     # ----------------
  167     # Fill a rectangle
  168     # ----------------
  169     
  170 
  171     # Draw a rectangle at position (x, y) with a width of 200 and a height
  172     # of 50
  173     
  174     PDF_rect($p, $x, $y-=$dy, 200, 50);
  175 
  176     # Fill the path using current fill color, and clear it
  177     PDF_fill($p);
  178 
  179     # Output some descriptive black text
  180     PDF_fit_textline($p, "rect() and fill()", $xt, $y,
  181     "fillcolor={gray 0}");
  182 
  183 
  184     # ---------------------------
  185     # Fill and stroke a rectangle
  186     # ---------------------------
  187     
  188 
  189     # Draw a rectangle at position (x, y) with a width of 200 and a height
  190     # of 50
  191     
  192     PDF_rect($p, $x, $y-=$dy, 200, 50);
  193 
  194     # Fill and stroke the path using the current line width, fill color,
  195     # and stroke color, and clear it
  196     
  197     PDF_fill_stroke($p);
  198 
  199     # Output some descriptive black text
  200     PDF_fit_textline($p, "rect() and fill_stroke()", $xt, $y,
  201     "fillcolor={gray 0}");
  202 
  203 
  204     # -------------------------------------------------------------
  205     # Draw a line and an arc, close the path and fill and stroke it
  206     # -------------------------------------------------------------
  207     
  208 
  209     # Set the current point for graphics output
  210     PDF_moveto($p, $x-40, $y-=$dy);
  211 
  212     # Draw a line from the current point to the supplied point
  213     PDF_lineto($p, $x, $y);
  214 
  215     # Draw an arc segment counterclockwise at position (x, y) with a radius
  216     # of 40 starting at an angle of 90 degrees and ending at 180 degrees
  217     
  218     PDF_arc($p, $x, $y, 40, 90, 180);
  219 
  220     # Close the path and stroke and fill it, i.e. close the current subpath
  221     # (add a straight line segment from the current point to the starting
  222     # point of the path), and stroke and fill the complete current path
  223     
  224     PDF_closepath_fill_stroke($p);
  225 
  226     # Output some descriptive black text
  227     PDF_fit_textline($p, "lineto(), arc(), and", $xt, $y+20,
  228     "fillcolor={gray 0}");
  229     PDF_fit_textline($p, "closepath_fill_stroke()", $xt, $y,
  230     "fillcolor={gray 0}");
  231 
  232 
  233     # -----------------------------------------------------------------
  234     # Draw a rectangle and use it as the clipping a path. Draw and fill
  235     # a circle and clip it according to the clipping path defined.
  236     # -----------------------------------------------------------------
  237     
  238 
  239     # Save the current graphics state including the current clipping
  240     # path which is set to the entire page by default
  241     
  242     PDF_save($p);
  243 
  244     # Draw a rectangle at position (x, y) with a width of 200 and a height
  245     # of 50
  246     
  247     PDF_rect($p, $x, $y-=$dy, 200, 50);
  248 
  249     # Use the current path as the clipping path for subsequent operations
  250     PDF_clip($p);
  251 
  252     # Draw a circle at position (x, y) with a radius of 100
  253     PDF_circle($p, $x, $y, 80);
  254 
  255     # Fill the path with the current fill color and clear it
  256     PDF_fill($p);
  257 
  258     # Restore the graphics state which has been saved above
  259     PDF_restore($p);
  260 
  261     # Output some descriptive black text
  262     PDF_fit_textline($p, "rect(), clip(),", $xt, $y+20,
  263     "fillcolor={gray 0}");
  264     PDF_fit_textline($p, "circle(), and fill()", $xt, $y,
  265     "fillcolor={gray 0}");
  266 
  267     PDF_end_page_ext($p, "");
  268 
  269     PDF_end_document($p, "");
  270 
  271 $buf = PDF_get_buffer($p);
  272 $len = strlen($buf);
  273 
  274 header("Content-type: application/pdf");
  275 header("Content-Length: $len");
  276 header("Content-Disposition: inline; filename=hello.pdf");
  277 print $buf;
  278 
  279 ?>