3 * Logiciel : HTML2PDF - classe FPDF_Formulaire
5 * permet la gestion de champs de formulaire dans un PDF
6 * Inspiré des sources de http://fpdf.org/fr/script/script36.php et http://fpdf.org/fr/script/script40.php
8 * @author Laurent MINGUET <webmaster@html2pdf.fr>
11 if (!defined('__CLASS_FPDF_FORMULAIRE__'))
13 define('__CLASS_FPDF_FORMULAIRE__', true);
15 require_once(dirname(__FILE__
).'/01_fpdf_bookmark.class.php');
17 class FPDF_Formulaire
extends FPDF_BookMark
19 var $javascript = ''; //javascript code
20 var $n_js; //numéro de l'objet javascript
21 var $n_cata; //numéro de l'objet catalogue
24 function FPDF_Formulaire($orientation='P',$unit='mm',$format='A4')
26 $this->FPDF_BookMark($orientation,$unit,$format);
27 $this->PDFVersion
='1.6';
32 function _putuserrights()
34 if (!$this->ur
) return;
35 $this->_out('/Perms<<');
37 $this->_out('/UR3<<');
38 $this->_out('/Reference[<<');
39 $this->_out('/Type /SigRef');
40 $this->_out('/TransformMethod /UR3');
41 $this->_out('/TransformParams<<');
42 $this->_out('/Type /TransformParams');
43 $this->_out('/Annots[ /Create /Delete /Modify /Copy /Import /Export ]');
44 $this->_out('/Document [ /FullSave ]');
45 $this->_out('/Form[ /Add /FillIn /Delete /SubmitStandalone ]');
46 $this->_out('/Signature[ /Modify ]');
47 $this->_out('/V /2.2');
54 function _putresources()
57 parent
::_putresources();
58 $this->_putjavascript();
61 function _putcatalog()
63 $this->n_cata
= $this->n
;
65 parent
::_putcatalog();
67 if (!empty($this->javascript
)) $this->_out('/Names <</JavaScript '.($this->n_js
).' 0 R>>');
68 $this->_putuserrights();
72 * Create a javascript PDF string.
74 * @author Johannes Güntert, Nicola Asuni
76 function _putjavascript()
78 if (empty($this->javascript
)) return;
80 // the following two lines are used to avoid form fields duplication after saving
83 $js1 = "if(!this.getField('pdfoldsaved')) this.addField('pdfoldsaved','text',0, [0, 1, 0, 1]);";
84 $js2 = "getField('pdfoldsaved').value = 'saved';";
93 $this->n_js
= $this->n
;
95 $this->_out('/Names [(EmbeddedJS) '.($this->n +
1).' 0 R ]');
97 $this->_out('endobj');
100 $this->_out('/S /JavaScript');
101 $this->_out('/JS '.$this->_textstring($js1."\n".$this->javascript
."\n".$js2));
103 $this->_out('endobj');
107 * Convert color to javascript color.
108 * @param string $color color name or #RRGGBB
110 * @author Denis Van Nuffelen, Nicola Asuni
112 function _JScolor($color)
114 static $aColors = array('transparent', 'black', 'white', 'red', 'green', 'blue', 'cyan', 'magenta', 'yellow', 'dkGray', 'gray', 'ltGray');
115 if (substr($color,0,1) == '#')
117 return sprintf("['RGB',%.3F,%.3F,%.3F]", hexdec(substr($color,1,2))/255, hexdec(substr($color,3,2))/255, hexdec(substr($color,5,2))/255);
119 if (!in_array($color,$aColors))
121 $this->Error('Invalid color: '.$color);
124 return 'color.'.$color;
128 * Adds a javascript form field.
129 * @param string $type field type
130 * @param string $name field name
131 * @param int $x horizontal position
132 * @param int $y vertical position
133 * @param int $w width
134 * @param int $h height
135 * @param array $prop array of properties. Possible values are (http://www.adobe.com/devnet/acrobat/pdfs/js_developer_guide.pdf): <ul><li>rect: Position and size of field on page.</li><li>borderStyle: Rectangle border appearance.</li><li>strokeColor: Color of bounding rectangle.</li><li>lineWidth: Width of the edge of the surrounding rectangle.</li><li>rotation: Rotation of field in 90-degree increments.</li><li>fillColor: Background color of field (gray, transparent, RGB, or CMYK).</li><li>userName: Short description of field that appears on mouse-over.</li><li>readonly: Whether the user may change the field contents.</li><li>doNotScroll: Whether text fields may scroll.</li><li>display: Whether visible or hidden on screen or in print.</li><li>textFont: Text font.</li><li>textColor: Text color.</li><li>textSize: Text size.</li><li>richText: Rich text.</li><li>richValue: Text.</li><li>comb: Text comb format.</li><li>multiline: Text multiline.</li><li>charLimit: Text limit to number of characters.</li><li>fileSelect: Text file selection format.</li><li>password: Text password format.</li><li>alignment: Text layout in text fields.</li><li>buttonAlignX: X alignment of icon on button face.</li><li>buttonAlignY: Y alignment of icon on button face.</li><li>buttonFitBounds: Relative scaling of an icon to fit inside a button face.</li><li>buttonScaleHow: Relative scaling of an icon to fit inside a button face.</li><li>buttonScaleWhen: Relative scaling of an icon to fit inside a button face.</li><li>highlight: Appearance of a button when pushed.</li><li>style: Glyph style for checkbox and radio buttons.</li><li>numItems: Number of items in a combo box or list box.</li><li>editable: Whether the user can type in a combo box.</li><li>multipleSelection: Whether multiple list box items may be selected.</li></ul>
137 * @author Denis Van Nuffelen, Nicola Asuni
139 function _addfield($type, $name, $x, $y, $w, $h, $prop, $js_after = '')
141 if (!isset($prop['textSize'])) $prop['textSize'] = $this->FontSizePt
;
142 if (!isset($prop['strokeColor'])) $prop['strokeColor'] = 'ltGray';
143 if (isset($prop['value'])) $prop['value'] = str_replace('"', '', $prop['value']);
144 $name_field = preg_replace('/[^a-zA-Z0-9_]/isU', '_', $name);
146 $this->SetFillColor(240);
151 $this->Rect($x+
$d*0.5+
$r, $y-$d*0.5+
$r, $w-$d-2*$r, $h-$d-2*$r, 'F');
157 // the followind avoid fields duplication after saving the document
158 $this->javascript
.= "if(this.getField('pdfoldsaved') && this.getField('pdfoldsaved').value != 'saved') {";
159 $this->javascript
.= sprintf("f".$name_field."=this.addField('%s','%s',%d,[%.2F,%.2F,%.2F,%.2F]);", $name, $type, $this->PageNo()-1, $x*$this->k
, ($this->h
-$y)*$this->k+
1, ($x+
$w)*$this->k
, ($this->h
-$y-$h)*$this->k+
1)."\n";
160 $this->javascript
.= 'f'.$name_field.'.textSize='.$this->FontSizePt
.";\n";
161 while (list($key, $val) = each($prop))
163 if (strcmp(substr($key, -5), 'Color') == 0)
164 $val = $this->_JScolor($val);
167 $this->javascript
.= 'f'.$name_field.'.'.$key.'='.$val.";\n";
170 $this->javascript
.= '}';
171 $this->javascript
.= "\n".$js_after;
174 function IncludeJS($script)
176 $this->javascript
.= $script;
179 function form_InputHidden($name, $value)
181 $name_field = preg_replace('/[^a-zA-Z0-9_]/isU', '_', $name);
182 $prop = array('value' => $value);
184 $this->_addfield('checkbox', $name, 0, 0, 0.1, 0.1, $prop, $js_after);
187 function form_InputCheckBox($name, $x, $y, $w, $checked)
189 $name_field = preg_replace('/[^a-zA-Z0-9_]/isU', '_', $name);
192 $prop['value'] = ($checked ?
'Yes' : 'Off');
194 $this->_addfield('checkbox', $name, $x, $y, $w, $w, $prop, $js_after);
197 function form_InputRadio($name, $x, $y, $w)
199 $name_field = preg_replace('/[^a-zA-Z0-9_]/isU', '_', $name);
203 $this->_addfield('radiobutton', $name, $x, $y, $w, $w, $prop, $js_after);
206 function form_InputText($name, $x, $y, $w, $h, $prop)
208 $name_field = preg_replace('/[^a-zA-Z0-9_]/isU', '_', $name);
211 $this->_addfield('text', $name, $x, $y, $w, $h, $prop, $js_after);
214 function form_InputButton($name, $x, $y, $w, $h, $caption, $action, $prop)
216 $name_field = preg_replace('/[^a-zA-Z0-9_]/isU', '_', $name);
218 if (!isset($prop['borderStyle'])) $prop['borderStyle'] = 'beveled';
219 if (!isset($prop['fillColor'])) $prop['fillColor'] = 'ltGray';
220 if (!isset($prop['strokeColor'])) $prop['strokeColor'] = 'black';
222 $js_after = 'f'.$name_field.".buttonSetCaption('".addslashes($caption)."');\n";
223 $js_after.= 'f'.$name_field.".setAction('MouseUp','".addslashes($action)."');\n";
224 $js_after.= 'f'.$name_field.".highlight='push';\n";
225 $js_after.= 'f'.$name_field.".print=false;\n";
226 $this->_addfield('button', $name, $x, $y, $w, $h, $prop, $js_after);
229 function form_Select($name, $x, $y, $w, $h, $values, $multiligne, $prop)
231 $name_field = preg_replace('/[^a-zA-Z0-9_]/isU', '_', $name);
233 $type = ($multiligne ?
'listbox' : 'combobox');
234 $s = ''; foreach ($values as $value) { $s .= ($s ?
',' : '')."'".addslashes($value)."'"; }
235 $js_after = 'f'.$name_field.'.setItems(['.$s."]);\n";
236 $this->_addfield($type, $name, $x, $y, $w, $h, $prop, $js_after);