"Fossies" - the Fresh Open Source Software Archive

Member "PDFlib-Lite-7.0.5p3/bind/pdflib/ruby/invoice.rb" (6 Jun 2012, 4931 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) Ruby 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.

    1 #!/usr/bin/env ruby
    2 #
    3 # $Id: invoice.rb,v 1.4.2.5 2008/02/28 14:49:59 rjs Exp $
    4 #
    5 # PDFlib client: invoice example in ruby
    6 #
    7 
    8 require 'PDFlib'
    9 
   10 infile = "stationery.pdf"
   11 # This is where font/image/PDF input files live. Adjust as necessary.
   12 searchpath = "../data"
   13 left = 55
   14 right = 530
   15 fontsize = 12
   16 pagewidth = 595
   17 pageheight = 842
   18 baseopt =
   19   "ruler        {   30 45     275   375   475} " +
   20   "tabalignment {right left right right right} " +
   21   "hortabmethod ruler fontsize 12 "
   22 
   23 
   24 now = Time.now
   25 fulldate = now.strftime("%b %d, %Y")
   26 
   27 String      closingtext =
   28   "Terms of payment: <fillcolor={rgb 1 0 0}>30 days net. " +
   29   "<fillcolor={gray 0}>90 days warranty starting at the day of sale. " +
   30   "This warranty covers defects in workmanship only. " +
   31   "<fontname=Helvetica-BoldOblique encoding=winansi>" +
   32   "Kraxi Systems, Inc. " +
   33   "<resetfont>will, at its option, repair or replace the " +
   34   "product under the warranty. This warranty is not transferable. " +
   35   "No returns or exchanges will be accepted for wet products."
   36 
   37 data = [
   38   { "name" => "Super Kite",         "price" => 20,  "quantity" => 2},
   39   { "name" => "Turbo Flyer",        "price" => 40,  "quantity" => 5},
   40   { "name" => "Giga Trash",         "price" => 180, "quantity" => 1},
   41   { "name" => "Bare Bone Kit",      "price" => 50,  "quantity" => 3},
   42   { "name" => "Nitty Gritty",       "price" => 20,  "quantity" => 10},
   43   { "name" => "Pretty Dark Flyer",  "price" => 75,  "quantity" => 1},
   44   { "name" => "Free Gift",          "price" => 0,   "quantity" => 1}
   45 ]
   46 
   47 months = [
   48   "January", "February", "March", "April", "May", "June",
   49   "July", "August", "September", "October", "November", "December"
   50 ]
   51 
   52 begin
   53   p = PDFlib.new
   54 
   55   # This means we must check return values of load_font() etc.
   56   p.set_parameter("errorpolicy", "return")
   57 
   58   p.set_parameter("SearchPath", searchpath)
   59 
   60   if (p.begin_document("invoice.pdf", "") == -1)
   61     raise "Error: " + p.get_errmsg
   62   end
   63 
   64   p.set_info("Creator", "invoice.rb")
   65   p.set_info("Author", "Thomas Merz")
   66   p.set_info("Title", "PDFlib invoice generation demo (Ruby)")
   67 
   68   stationery = p.open_pdi_document(infile, "")
   69   if (stationery == -1)
   70     raise "Error: " + p.get_errmsg
   71   end
   72 
   73   page = p.open_pdi_page(stationery, 1, "")
   74   if (page == -1)
   75     raise "Error: " + p.get_errmsg
   76   end
   77 
   78   boldfont = p.load_font("Helvetica-Bold", "winansi", "")
   79   if boldfont == -1
   80     raise "Error: " + p.get_errmsg + "\n"
   81   end
   82 
   83   regularfont = p.load_font("Helvetica", "winansi", "")
   84   if regularfont == -1
   85     raise "Error: " + p.get_errmsg + "\n"
   86   end
   87 
   88   leading = fontsize + 2
   89 
   90   # Establish coordinates with the origin in the upper left corner.
   91   p.begin_page_ext(pagewidth, pageheight, "topdown")
   92 
   93   p.fit_pdi_page(page, 0, pageheight, "")
   94   p.close_pdi_page(page)
   95 
   96   p.setfont(regularfont, fontsize)
   97 
   98   # Print the address
   99   y = 170
  100   p.set_value("leading", leading)
  101 
  102   p.show_xy("John Q. Doe", left, y)
  103   p.continue_text("255 Customer Lane")
  104   p.continue_text("Suite B")
  105   p.continue_text("12345 User Town")
  106   p.continue_text("Everland")
  107 
  108   # Print the header and date
  109 
  110   p.setfont(boldfont, fontsize)
  111   y = 300
  112   p.show_xy("INVOICE", left, y)
  113 
  114   p.fit_textline(fulldate, right, y, "position {100 0}")
  115 
  116   # Print the invoice header line
  117 
  118   y = 370
  119   buf = "\tITEM\tDESCRIPTION\tQUANTITY\tPRICE\tAMOUNT"
  120   optlist = baseopt + " font " + boldfont.to_s
  121 
  122   textflow = p.create_textflow(buf, optlist)
  123   if (textflow == -1)
  124     raise "Error: " + p.get_errmsg
  125   end
  126 
  127   p.fit_textflow(textflow, left, y-leading, right, y, "")
  128   p.delete_textflow(textflow)
  129 
  130   # Print the article list
  131 
  132   i = 1
  133   y += 2*leading
  134   total = 0
  135 
  136   optlist = baseopt + " font " + regularfont.to_s
  137 
  138   data.each { |row|
  139     sum = row["price"] * row["quantity"]
  140     buf = sprintf("\t%d\t%s\t%d\t%.2f\t%.2f",
  141     i, row["name"], row["quantity"], row["price"], sum);
  142 
  143     textflow = p.create_textflow(buf, optlist)
  144     if (textflow == -1)
  145       raise "Error: " + p.get_errmsg
  146     end
  147 
  148     p.fit_textflow(textflow, left, y-leading, right, y, "")
  149     p.delete_textflow(textflow)
  150 
  151     i += 1
  152     y += leading
  153     total += sum
  154   }
  155 
  156   y += leading
  157   p.setfont(boldfont, fontsize)
  158   p.fit_textline(total.to_s, right, y, "position {100 0}")
  159 
  160   # Print the closing text
  161 
  162   y += 5*leading
  163 
  164   optlist = "alignment=justify leading=120% " \
  165       "fontname=Helvetica fontsize=12 encoding=winansi "
  166 
  167   textflow = p.create_textflow(closingtext, optlist)
  168   if (textflow == -1)
  169     raise "Error: " + p.get_errmsg
  170   end
  171 
  172   p.fit_textflow(textflow, left, y+6*leading, right, y, "")
  173   p.delete_textflow(textflow)
  174 
  175   p.end_page_ext("")
  176   p.end_document("")
  177   p.close_pdi_document(stationery)
  178 
  179 rescue  PDFlibException => pe
  180   print "PDFlib exception occurred in invoice sample:\n"
  181   print "[" + pe.get_errnum.to_s + "] " + pe.get_apiname + \
  182           ": " + pe.get_errmsg + "\n"
  183 rescue  Exception => e
  184   print e.backtrace.join("\n") + "\n" + e.to_s + "\n"
  185 ensure
  186   p.delete() if p
  187 end
  188 
  189 # vim: sw=2