1#!/bin/sh
2# $Id: invoice.tcl 14574 2005-10-29 16:27:43Z bonefish $
3#
4# PDFlib client: invoice generation demo
5#
6
7# Hide the exec to Tcl but not to the shell by appending a backslash\
8exec tclsh "$0" ${1+"$@"}
9
10# The lappend line is unnecessary if PDFlib has been installed
11# in the Tcl package directory
12set auto_path [linsert $auto_path 0 .libs .]
13
14package require pdflib 5.0
15
16set infile "stationery.pdf"
17set searchpath  "../data"
18set col1 55.0
19set col2 100.0
20set col3 330.0
21set col4 430.0
22set col5 530.0
23set fontsize 12.0
24set pagewidth 595.0
25set pageheight 842.0
26
27set closingtext  \
28"30 days warranty starting at the day of sale. \
29This warranty covers defects in workmanship only. \
30Kraxi Systems, Inc. will, at its option, repair or replace the \
31product under the warranty. This warranty is not transferable. \
32No returns or exchanges will be accepted for wet products."
33
34global articleName articlePrice articleQuantity
35
36proc article_add {id name price quantity} {
37    global articleName articlePrice articleQuantity
38    set articleName($id) $name
39    set articlePrice($id) $price
40    set articleQuantity($id) $quantity
41}
42
43article_add 0 "Super Kite"		20	2
44article_add 1 "Turbo Flyer"		40	5
45article_add 2 "Giga Trash"		180	1
46article_add 3 "Bare Bone Kit"		50	3
47article_add 4 "Nitty Gritty"		20	10
48article_add 5 "Pretty Dark Flyer"	75	1
49article_add 6 "Free Gift"		0	1
50
51set ARTICLECOUNT 7
52
53# create a new PDFlib object
54set p [PDF_new]
55
56# open new PDF file
57if {[PDF_open_file $p "invoice.pdf"] == -1} {
58    puts stderr "Error: [PDF_get_errmsg $p]"
59    exit
60}
61
62PDF_set_parameter $p "SearchPath" $searchpath
63
64PDF_set_info $p "Creator" "invoice.c"
65PDF_set_info $p "Author" "Thomas Merz"
66PDF_set_info $p "Title" "PDFlib invoice generation demo (TCL)"
67
68set form [PDF_open_pdi $p $infile "" 0]
69if {$form == -1} {
70    puts stderr "Error: [PDF_get_errmsg $p]"
71    exit
72}
73
74set page [PDF_open_pdi_page $p $form 1 ""]
75if {$page == -1} {
76    puts stderr "Error: [PDF_get_errmsg $p]"
77    exit
78}
79
80set boldfont [PDF_load_font $p "Helvetica-Bold" "winansi" ""]
81set regularfont [PDF_load_font $p "Helvetica" "winansi" ""]
82set leading [expr $fontsize + 2]
83
84# Establish coordinates with the origin in the upper left corner.
85PDF_set_parameter $p "topdown" "true"
86
87# A4 page
88PDF_begin_page $p $pagewidth $pageheight
89
90PDF_fit_pdi_page $p $page 0 $pageheight ""
91PDF_close_pdi_page $p $page
92
93PDF_setfont $p $regularfont $fontsize
94
95# Print the address
96set y 170
97PDF_set_value $p "leading" $leading
98
99PDF_show_xy $p "John Q. Doe" $col1 $y
100PDF_continue_text $p "255 Customer Lane"
101PDF_continue_text $p "Suite B"
102PDF_continue_text $p "12345 User Town"
103PDF_continue_text $p "Everland"
104
105# Print the header and date
106
107PDF_setfont $p $boldfont $fontsize
108set y 300
109PDF_show_xy $p "INVOICE" $col1 $y
110
111set buf [clock format [clock seconds] -format "%B %d, %Y"]
112PDF_fit_textline $p $buf $col5 $y "position {100 0}"
113
114# Print the invoice header line
115PDF_setfont $p $boldfont $fontsize
116
117# "position {0 0}" is left-aligned, "position {100 0}" right-aligned
118set y 370
119PDF_fit_textline $p "ITEM"		$col1 $y "position {0 0}"
120PDF_fit_textline $p "DESCRIPTION"	$col2 $y "position {0 0}"
121PDF_fit_textline $p "QUANTITY"		$col3 $y "position {100 0}"
122PDF_fit_textline $p "PRICE"		$col4 $y "position {100 0}"
123PDF_fit_textline $p "AMOUNT"		$col5 $y "position {100 0}"
124
125# Print the article list
126
127PDF_setfont  $p $regularfont $fontsize
128set y [expr $y + 2*$leading]
129set total 0
130
131for {set i 0} {$i < $ARTICLECOUNT} {set i [expr $i + 1]} {
132    PDF_show_xy $p [expr $i + 1] $col1 $y
133
134    PDF_show_xy $p $articleName($i) $col2 $y
135
136    PDF_fit_textline $p $articleName($i) $col3 $y "position {100 0}"
137
138    set buf [format "%.2f" $articlePrice($i)]
139    PDF_fit_textline $p $buf $col4 $y "position {100 0}"
140
141    set sum [ expr $articlePrice($i) * $articleQuantity($i)]
142    set buf [format "%.2f" $sum]
143    PDF_fit_textline $p $buf $col5 $y "position {100 0}"
144
145    set y [expr $y + $leading]
146    set total [expr $total + $sum]
147}
148
149set y [expr $y + $leading]
150PDF_setfont $p $boldfont $fontsize
151set buf [format "%.2f" $total]
152PDF_fit_textline $p $buf $col5 $y "position {100 0}"
153
154# Print the closing text
155
156set y [expr $y + 5*$leading]
157PDF_setfont $p $regularfont $fontsize
158PDF_set_value $p "leading" $leading
159PDF_show_boxed $p $closingtext \
160    $col1 [expr $y + 4*$leading] [expr $col5-$col1] [expr 4*$leading]\
161    "justify" ""
162
163PDF_end_page $p
164PDF_close $p
165PDF_close_pdi $p $form
166
167PDF_delete $p
168