1<?php
2// $Header: /cvsroot/html2ps/output.pdflib.1.6.class.php,v 1.2 2006/11/11 13:43:53 Konstantin Exp $
3
4require_once(HTML2PS_DIR.'output.pdflib.class.php');
5
6class PDFLIBForm {
7  var $_name;
8
9  function PDFLIBForm($name /*, $submit_action, $reset_action */) {
10    $this->_name          = $name;
11  }
12
13  function name() {
14    return $this->_name;
15  }
16}
17
18class OutputDriverPdflib16 extends OutputDriverPdflib {
19  function field_multiline_text($x, $y, $w, $h, $value, $name) {
20    $font = $this->_control_font();
21    pdf_create_field($this->pdf,
22                     $x, $y, $x + $w, $y - $h,
23                     $this->_fqn($name),
24                     "textfield",
25                     sprintf("currentvalue {%s} defaultvalue {%s} font {%s} fontsize {auto} multiline {true}",
26                             $value,
27                             $value,
28                             $font));
29  }
30
31  function field_text($x, $y, $w, $h, $value, $name) {
32    $font = $this->_control_font();
33    pdf_create_field($this->pdf,
34                     $x, $y, $x + $w, $y - $h,
35                     $this->_fqn($name),
36                     "textfield",
37                     sprintf("currentvalue {%s} defaultvalue {%s} font {%s} fontsize {auto}",
38                             $value,
39                             $value,
40                             $font));
41  }
42
43  function field_password($x, $y, $w, $h, $value, $name) {
44    $font = $this->_control_font();
45    pdf_create_field($this->pdf,
46                     $x, $y, $x + $w, $y - $h,
47                     $this->_fqn($name),
48                     "textfield",
49                     sprintf("currentvalue {%s} font {%s} fontsize {auto} password {true}", $value, $font));
50  }
51
52  function field_pushbutton($x, $y, $w, $h) {
53    $font = $this->_control_font();
54
55    pdf_create_field($this->pdf,
56                     $x, $y, $x + $w, $y - $h,
57                     $this->_fqn(sprintf("___Button%s",md5(time().rand()))),
58                     "pushbutton",
59                     sprintf("font {%s} fontsize {auto} caption {%s}",
60                             $font,
61                             " "));
62  }
63
64  function field_pushbuttonimage($x, $y, $w, $h, $field_name, $value, $actionURL) {
65    $font = $this->_control_font();
66
67    $action = pdf_create_action($this->pdf,
68                                "SubmitForm",
69                                sprintf("exportmethod {html} url=%s", $actionURL));
70
71    pdf_create_field($this->pdf,
72                     $x, $y, $x + $w, $y - $h,
73                     $this->_fqn($field_name),
74                     "pushbutton",
75                     sprintf("action {activate %s} font {%s} fontsize {auto} caption {%s}",
76                             $action,
77                             $font,
78                             " "));
79  }
80
81  function field_pushbuttonreset($x, $y, $w, $h) {
82    $font = $this->_control_font();
83
84    $action = pdf_create_action($this->pdf,
85                                "ResetForm",
86                                sprintf(""));
87
88    pdf_create_field($this->pdf,
89                     $x, $y, $x + $w, $y - $h,
90                     $this->_fqn(sprintf("___ResetButton%d",$action)),
91                     "pushbutton",
92                     sprintf("action {activate %s} font {%s} fontsize {auto} caption {%s}",
93                             $action,
94                             $font,
95                             " "));
96  }
97
98  function field_pushbuttonsubmit($x, $y, $w, $h, $field_name, $value, $actionURL) {
99    $font = $this->_control_font();
100
101    $action = pdf_create_action($this->pdf,
102                                "SubmitForm",
103                                sprintf("exportmethod {html} url=%s", $actionURL));
104
105    pdf_create_field($this->pdf,
106                     $x, $y, $x + $w, $y - $h,
107                     $this->_fqn($field_name),
108                     "pushbutton",
109                     sprintf("action {activate %s} font {%s} fontsize {auto} caption {%s}",
110                             $action,
111                             $font,
112                             " "));
113  }
114
115  function field_checkbox($x, $y, $w, $h, $name, $value, $checked) {
116    pdf_create_field($this->pdf,
117                     $x, $y, $x + $w, $y - $h,
118                     $this->_fqn($name),
119                     "checkbox",
120                     sprintf("buttonstyle {cross} currentvalue {%s} defaultvalue {%s} itemname {%s}",
121                             $checked ? $value : "Off",
122                             $checked ? $value : "Off",
123                             $value));
124  }
125
126  function field_radio($x, $y, $w, $h, $groupname, $value, $checked) {
127    $fqgn = $this->_fqn($groupname, true);
128
129    if (!isset($this->_radiogroups[$fqgn])) {
130      $this->_radiogroups[$fqgn] = pdf_create_fieldgroup($this->pdf, $fqgn, "fieldtype=radiobutton");
131    };
132
133    pdf_create_field($this->pdf,
134                     $x, $y, $x + $w, $y - $h,
135                     sprintf("%s.%s",$fqgn,$value),
136                     "radiobutton",
137                     sprintf("buttonstyle {circle} currentvalue {%s} defaultvalue {%s} itemname {%s}",
138                             $checked ? $value : "Off",
139                             $checked ? $value : "Off",
140                             $value));
141  }
142
143  function field_select($x, $y, $w, $h, $name, $value, $options) {
144    $items_str = "";
145    $text_str  = "";
146    foreach ($options as $option) {
147      $items_str .= sprintf("%s ",$option[0]);
148      $text_str  .= sprintf("%s ",$option[1]);
149    };
150
151    $font = $this->_control_font();
152    pdf_create_field($this->pdf,
153                     $x, $y, $x + $w, $y - $h,
154                     $this->_fqn($name),
155                     "combobox",
156                     sprintf("currentvalue {%s} defaultvalue {%s} font {%s} fontsize {auto} itemnamelist {%s} itemtextlist {%s}",
157                             $value,
158                             $value,
159                             $font,
160                             $items_str,
161                             $text_str));
162  }
163
164  function new_form($name) {
165    $this->_forms[] = new PDFLIBForm($name);
166
167    pdf_create_fieldgroup($this->pdf, $name, "fieldtype=mixed");
168  }
169
170  /* private routines */
171
172  function _control_font() {
173    return pdf_load_font($this->pdf, "Helvetica", "winansi", "embedding=true subsetting=false");
174  }
175
176  function _lastform() {
177    if (count($this->_forms) == 0) {
178      /**
179       * Handle invalid HTML; if we've met an input control outside the form,
180       * generate a new form with random name
181       */
182
183      $name = sprintf("AnonymousFormObject_%u", md5(rand().time()));
184
185      $this->_forms[] = new PDFLIBForm($name);
186      pdf_create_fieldgroup($this->pdf, $name, "fieldtype=mixed");
187
188      error_log(sprintf("Anonymous form generated with name %s; check your HTML for validity",
189                        $name));
190    };
191
192    return $this->_forms[count($this->_forms)-1];
193  }
194
195  function _valid_name($name) {
196    if (empty($name)) { return false; };
197
198    return true;
199  }
200
201  function _fqn($name, $allowexisting=false) {
202    if (!$this->_valid_name($name)) {
203      $name = uniqid("AnonymousFormFieldObject_");
204      error_log(sprintf("Anonymous field generated with name %s; check your HTML for validity",
205                        $name));
206    };
207
208    $lastform = $this->_lastform();
209    $fqn = sprintf("%s.%s",
210                   $lastform->name(),
211                   $name);
212
213    if (array_search($fqn, $this->_field_names) === FALSE) {
214      $this->_field_names[] = $fqn;
215    } elseif (!$allowexisting) {
216      error_log(sprintf("Interactive form '%s' already contains field named '%s'",
217                        $lastform->name(),
218                        $name));
219      $fqn .= md5(rand().time());
220    };
221
222    return $fqn;
223  }
224}
225?>