[44] | 1 | <?php
|
---|
| 2 | /**
|
---|
| 3 | * Logiciel : HTML2PDF - classe FPDF_Formulaire
|
---|
| 4 | *
|
---|
| 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
|
---|
| 7 | *
|
---|
| 8 | * @author Laurent MINGUET <webmaster@spipu.net>
|
---|
| 9 | */
|
---|
| 10 |
|
---|
| 11 | if (!defined('__CLASS_FPDF_FORMULAIRE__'))
|
---|
| 12 | {
|
---|
| 13 | define('__CLASS_FPDF_FORMULAIRE__', true);
|
---|
| 14 |
|
---|
| 15 | require_once(dirname(__FILE__).'/01_fpdf_bookmark.class.php');
|
---|
| 16 |
|
---|
| 17 | class FPDF_Formulaire extends FPDF_BookMark
|
---|
| 18 | {
|
---|
| 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
|
---|
| 22 | var $ur; //
|
---|
| 23 |
|
---|
| 24 | function FPDF_Formulaire($orientation='P',$unit='mm',$format='A4')
|
---|
| 25 | {
|
---|
| 26 | $this->FPDF_BookMark($orientation,$unit,$format);
|
---|
| 27 | $this->PDFVersion='1.6';
|
---|
| 28 |
|
---|
| 29 | $this->ur = false;
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | function _putuserrights()
|
---|
| 33 | {
|
---|
| 34 | if (!$this->ur) return;
|
---|
| 35 | $this->_out('/Perms<<');
|
---|
| 36 |
|
---|
| 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');
|
---|
| 48 | $this->_out('>>');
|
---|
| 49 | $this->_out('>>]');
|
---|
| 50 | $this->_out('>>');
|
---|
| 51 | $this->_out('>>');
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | function _putresources()
|
---|
| 55 | {
|
---|
| 56 |
|
---|
| 57 | parent::_putresources();
|
---|
| 58 | $this->_putjavascript();
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | function _putcatalog()
|
---|
| 62 | {
|
---|
| 63 | $this->n_cata = $this->n;
|
---|
| 64 |
|
---|
| 65 | parent::_putcatalog();
|
---|
| 66 |
|
---|
| 67 | if (!empty($this->javascript)) $this->_out('/Names <</JavaScript '.($this->n_js).' 0 R>>');
|
---|
| 68 | $this->_putuserrights();
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | /*
|
---|
| 72 | * Create a javascript PDF string.
|
---|
| 73 | * @access protected
|
---|
| 74 | * @author Johannes Güntert, Nicola Asuni
|
---|
| 75 | */
|
---|
| 76 | function _putjavascript()
|
---|
| 77 | {
|
---|
| 78 | if (empty($this->javascript)) return;
|
---|
| 79 |
|
---|
| 80 | // the following two lines are used to avoid form fields duplication after saving
|
---|
| 81 | if ($this->ur)
|
---|
| 82 | {
|
---|
| 83 | $js1 = "if(!this.getField('pdfoldsaved')) this.addField('pdfoldsaved','text',0, [0, 1, 0, 1]);";
|
---|
| 84 | $js2 = "getField('pdfoldsaved').value = 'saved';";
|
---|
| 85 | }
|
---|
| 86 | else
|
---|
| 87 | {
|
---|
| 88 | $js1 = '';
|
---|
| 89 | $js2 = '';
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | $this->_newobj();
|
---|
| 93 | $this->n_js = $this->n;
|
---|
| 94 | $this->_out('<<');
|
---|
| 95 | $this->_out('/Names [(EmbeddedJS) '.($this->n + 1).' 0 R ]');
|
---|
| 96 | $this->_out('>>');
|
---|
| 97 | $this->_out('endobj');
|
---|
| 98 | $this->_newobj();
|
---|
| 99 | $this->_out('<<');
|
---|
| 100 | $this->_out('/S /JavaScript');
|
---|
| 101 | $this->_out('/JS '.$this->_textstring($js1."\n".$this->javascript."\n".$js2));
|
---|
| 102 | $this->_out('>>');
|
---|
| 103 | $this->_out('endobj');
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | /*
|
---|
| 107 | * Convert color to javascript color.
|
---|
| 108 | * @param string $color color name or #RRGGBB
|
---|
| 109 | * @access protected
|
---|
| 110 | * @author Denis Van Nuffelen, Nicola Asuni
|
---|
| 111 | */
|
---|
| 112 | function _JScolor($color)
|
---|
| 113 | {
|
---|
| 114 | static $aColors = array('transparent', 'black', 'white', 'red', 'green', 'blue', 'cyan', 'magenta', 'yellow', 'dkGray', 'gray', 'ltGray');
|
---|
| 115 | if (substr($color,0,1) == '#')
|
---|
| 116 | {
|
---|
| 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);
|
---|
| 118 | }
|
---|
| 119 | if (!in_array($color,$aColors))
|
---|
| 120 | {
|
---|
| 121 | $this->Error('Invalid color: '.$color);
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | return 'color.'.$color;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | /*
|
---|
| 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>
|
---|
| 136 | * @access protected
|
---|
| 137 | * @author Denis Van Nuffelen, Nicola Asuni
|
---|
| 138 | */
|
---|
| 139 | function _addfield($type, $name, $x, $y, $w, $h, $prop, $js_after = '')
|
---|
| 140 | {
|
---|
| 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 |
|
---|
| 145 | $this->SetFillColor(240);
|
---|
| 146 | if ($w>0 && $h>0)
|
---|
| 147 | {
|
---|
| 148 | $d = 1/$this->k;
|
---|
| 149 | $r = 0.1;
|
---|
| 150 | $this->Rect($x+$d*0.5+$r, $y-$d*0.5+$r, $w-$d-2*$r, $h-$d-2*$r, 'F');
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | // javascript inclus
|
---|
| 154 | $this->ur = true;
|
---|
| 155 |
|
---|
| 156 | // the followind avoid fields duplication after saving the document
|
---|
| 157 | $this->javascript .= "if(this.getField('pdfoldsaved') && this.getField('pdfoldsaved').value != 'saved') {";
|
---|
| 158 | $this->javascript .= sprintf("f".$name."=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";
|
---|
| 159 | $this->javascript .= 'f'.$name.'.textSize='.$this->FontSizePt.";\n";
|
---|
| 160 | while (list($key, $val) = each($prop))
|
---|
| 161 | {
|
---|
| 162 | if (strcmp(substr($key, -5), 'Color') == 0)
|
---|
| 163 | $val = $this->_JScolor($val);
|
---|
| 164 | else
|
---|
| 165 | $val = '"'.$val.'"';
|
---|
| 166 | $this->javascript .= 'f'.$name.'.'.$key.'='.$val.";\n";
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | $this->javascript .= '}';
|
---|
| 170 | $this->javascript.= "\n".$js_after;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | function IncludeJS($script)
|
---|
| 174 | {
|
---|
| 175 | $this->javascript .= $script;
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | function form_InputHidden($name, $value)
|
---|
| 179 | {
|
---|
| 180 | $prop = array('value' => $value);
|
---|
| 181 | $js_after = '';
|
---|
| 182 | $this->_addfield('checkbox', $name, 0, 0, 0.1, 0.1, $prop, $js_after);
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | function form_InputCheckBox($name, $x, $y, $w, $checked)
|
---|
| 186 | {
|
---|
| 187 | $prop = array();
|
---|
| 188 | $prop['value'] = ($checked ? 'Yes' : 'Off');
|
---|
| 189 | $js_after = '';
|
---|
| 190 | $this->_addfield('checkbox', $name, $x, $y, $w, $w, $prop, $js_after);
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | function form_InputRadio($name, $x, $y, $w)
|
---|
| 194 | {
|
---|
| 195 | $prop = array();
|
---|
| 196 | $js_after = '';
|
---|
| 197 | $this->_addfield('radiobutton', $name, $x, $y, $w, $w, $prop, $js_after);
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | function form_InputText($name, $x, $y, $w, $h, $prop)
|
---|
| 201 | {
|
---|
| 202 | $js_after = '';
|
---|
| 203 | $this->_addfield('text', $name, $x, $y, $w, $h, $prop, $js_after);
|
---|
| 204 | }
|
---|
| 205 |
|
---|
| 206 | function form_InputButton($name, $x, $y, $w, $h, $caption, $action, $prop)
|
---|
| 207 | {
|
---|
| 208 | if (!isset($prop['borderStyle'])) $prop['borderStyle'] = 'beveled';
|
---|
| 209 | if (!isset($prop['fillColor'])) $prop['fillColor'] = 'ltGray';
|
---|
| 210 | if (!isset($prop['strokeColor'])) $prop['strokeColor'] = 'black';
|
---|
| 211 |
|
---|
| 212 | $js_after = 'f'.$name.".buttonSetCaption('".addslashes($caption)."');\n";
|
---|
| 213 | $js_after.= 'f'.$name.".setAction('MouseUp','".addslashes($action)."');\n";
|
---|
| 214 | $js_after.= 'f'.$name.".highlight='push';\n";
|
---|
| 215 | $js_after.= 'f'.$name.".print=false;\n";
|
---|
| 216 | $this->_addfield('button', $name, $x, $y, $w, $h, $prop, $js_after);
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | function form_Select($name, $x, $y, $w, $h, $values, $multiligne, $prop)
|
---|
| 220 | {
|
---|
| 221 | $type = ($multiligne ? 'listbox' : 'combobox');
|
---|
| 222 | $s = ''; foreach ($values as $value) { $s .= ($s ? ',' : '')."'".addslashes($value)."'"; }
|
---|
| 223 | $js_after = 'f'.$name.'.setItems(['.$s."]);\n";
|
---|
| 224 | $this->_addfield($type, $name, $x, $y, $w, $h, $prop, $js_after);
|
---|
| 225 | }
|
---|
| 226 | }
|
---|
| 227 | }
|
---|
| 228 | ?> |
---|