"Fossies" - the Fresh Open Source Software Archive

Member "PDFlib-Lite-7.0.5p3/bind/pdflib/php/invoice.php" (6 Jun 2012, 5317 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 "invoice.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 /* $Id: invoice.php,v 1.23 2006/10/01 20:33:35 rjs Exp $
    3  *
    4  * PDFlib client: invoice example in PHP
    5  */
    6 
    7 $left = 55;
    8 $right = 530;
    9 $fontsize = 12;
   10 $pagewidth = 595;
   11 $pageheight = 842;
   12 $fontsize = 12;
   13 $infile  =  "stationery.pdf";
   14 $baseopt =  "ruler        {   30 45     275   375    475} " .
   15         "tabalignment { right left right right right} " .
   16         "hortabmethod ruler fontsize 12 ";
   17 
   18 
   19 /* This is where font/image/PDF input files live. Adjust as necessary. */
   20 $searchpath = "../data";
   21 $closingtext =
   22     "Terms of payment: <fillcolor={rgb 1 0 0}>30 days net. " .
   23     "<fillcolor={gray 0}>90 days warranty starting at the day of sale. " .
   24     "This warranty covers defects in workmanship only. " .
   25     "<fontname=Helvetica-BoldOblique encoding=host>Kraxi Systems, Inc. " .
   26     "<resetfont>will, at its option, repair or replace the " .
   27     "product under the warranty. This warranty is not transferable. " .
   28     "No returns or exchanges will be accepted for wet products.";
   29 
   30 $data = array(  array("name"=>"Super Kite",     "price"=>20,    "quantity"=>2),
   31         array("name"=>"Turbo Flyer",    "price"=>40,    "quantity"=>5),
   32         array("name"=>"Giga Trasch",    "price"=>180,   "quantity"=>1),
   33         array("name"=>"Bare Bone Kit",  "price"=>50,    "quantity"=>3),
   34         array("name"=>"Nitty Gritty",   "price"=>20,    "quantity"=>10),
   35         array("name"=>"Pretty Dark Flyer","price"=>75,  "quantity"=>1),
   36         array("name"=>"Free Gift",  "price"=>0,     "quantity"=>1)
   37         );
   38 
   39 $months = array( "January", "February", "March", "April", "May", "June",
   40         "July", "August", "September", "October", "November", "December");
   41 
   42 $p = PDF_new();
   43 
   44 # This means we must check return values of load_font() etc.
   45 PDF_set_parameter($p, "errorpolicy", "return");
   46 
   47 PDF_set_parameter($p, "SearchPath", $searchpath);
   48 
   49 /*  open new PDF file; insert a file name to create the PDF on disk */
   50 if (PDF_begin_document($p, "", "") == 0) {
   51     die("Error: " . PDF_get_errmsg($p));
   52 }
   53 
   54 /* This line is required to avoid problems on Japanese systems */
   55 PDF_set_parameter($p, "hypertextencoding", "winansi");
   56 
   57 PDF_set_info($p, "Creator", "invoice.php");
   58 PDF_set_info($p, "Author", "Thomas Merz");
   59 PDF_set_info($p, "Title", "PDFlib invoice generation demo (PHP)");
   60 
   61 $stationery = PDF_open_pdi($p, $infile, "", 0);
   62 if ($stationery == 0) {
   63     die("Error: " . PDF_get_errmsg($p));
   64 }
   65 
   66 $page = PDF_open_pdi_page($p, $stationery, 1, "");
   67 if ($page == 0) {
   68     die("Error: " . PDF_get_errmsg($p));
   69 }
   70 
   71 
   72 $boldfont = PDF_load_font($p, "Helvetica-Bold", "winansi", "");
   73 if ($boldfont == 0) {
   74     die("Error: " . PDF_get_errmsg($p));
   75 }
   76 $regularfont = PDF_load_font($p, "Helvetica", "winansi", "");
   77 if ($regularfont == 0) {
   78     die("Error: " . PDF_get_errmsg($p));
   79 }
   80 $leading = $fontsize + 2;
   81 
   82 /* Establish coordinates with the origin in the upper left corner. */
   83 PDF_begin_page_ext($p, $pagewidth, $pageheight, "topdown");
   84 
   85 PDF_fit_pdi_page($p, $page, 0, $pageheight, "");
   86 PDF_close_pdi_page($p, $page);
   87 
   88 PDF_setfont($p, $regularfont, $fontsize);
   89 
   90 /* print the address */
   91 $y = 170;
   92 PDF_set_value($p, "leading", $leading);
   93 
   94 PDF_show_xy($p, "John Q. Doe", $left, $y);
   95 PDF_continue_text($p, "255 Customer Lane");
   96 PDF_continue_text($p, "Suite B");
   97 PDF_continue_text($p, "12345 User Town");
   98 PDF_continue_text($p, "Everland");
   99 
  100 /* print the header and date */
  101 
  102 PDF_setfont($p, $boldfont, $fontsize);
  103 $y = 300;
  104 PDF_show_xy($p, "INVOICE", $left, $y);
  105 $time = localtime();
  106 $buf = sprintf("%s %d, %d", $months[$time[4]], $time[3], $time[5]+1900);
  107 PDF_fit_textline($p, $buf, $right, $y, "position {100 0}");
  108 
  109 
  110 /* print the invoice header line */
  111 $y = 370;
  112 $buf = sprintf("\tITEM\tDESCRIPTION\tQUANTITY\tPRICE\tAMOUNT");
  113 $optlist = sprintf("%s font %d ", $baseopt, $boldfont);
  114 $textflow = PDF_create_textflow($p, $buf, $optlist);
  115 
  116 if ($textflow == 0){
  117     die("Error: " . PDF_get_errmsg($p));
  118 }
  119 
  120 PDF_fit_textflow($p, $textflow, $left, $y-$leading, $right, $y, "");
  121 PDF_delete_textflow($p, $textflow);
  122 
  123 $y += 2*$leading;
  124 $total = 0;
  125 $optlist = sprintf("%s font %d ", $baseopt, $regularfont);
  126 
  127 for ($i = 0; $i < count($data); $i++) {
  128     $sum = $data[$i]{"price"}*$data[$i]{"quantity"};
  129     $buf = sprintf("\t%d\t%s\t%d\t%.2f\t%.2f", $i+1, $data[$i]{"name"},
  130         $data[$i]{"quantity"}, $data[$i]{"price"}, $sum);
  131 
  132     $textflow = PDF_create_textflow($p, $buf, $optlist);
  133 
  134     if ($textflow == 0) {
  135     die("Error: " . PDF_get_errmsg($p));
  136     }
  137 
  138     PDF_fit_textflow($p, $textflow, $left, $y-$leading, $right, $y, "");
  139     PDF_delete_textflow($p, $textflow);
  140 
  141     $y += $leading;
  142     $total +=$sum;
  143 }
  144 
  145 $y += $leading;
  146 
  147 PDF_setfont($p, $boldfont, $fontsize);
  148 PDF_fit_textline($p,sprintf("%.2f",$total), $right, $y, "position {100 0}");
  149 
  150 /* Print the closing text */
  151 
  152 $y +=5*$leading;
  153 $optlist  = "alignment=justify leading=120% ";
  154 $optlist .= "fontname=Helvetica fontsize=12 encoding=winansi";
  155 
  156 $textflow = PDF_create_textflow($p, $closingtext, $optlist);
  157 
  158 if ($textflow == 0) {
  159     die("Error: " . PDF_get_errmsg($p));
  160 }
  161 
  162 PDF_fit_textflow($p, $textflow, $left, $y+6*$leading, $right, $y, "");
  163 PDF_delete_textflow($p, $textflow);
  164 
  165 PDF_end_page_ext($p, "");
  166 PDF_end_document($p, "");
  167 PDF_close_pdi($p, $stationery);
  168 
  169 $buf = PDF_get_buffer($p);
  170 $len = strlen($buf);
  171 
  172 header("Content-type: application/pdf");
  173 header("Content-Length: $len");
  174 header("Content-Disposition: inline; filename=hello.pdf");
  175 print $buf;
  176 
  177 PDF_delete($p);
  178 ?>