*/
if (!defined('__CLASS_FPDF_FORMULAIRE__'))
{
define('__CLASS_FPDF_FORMULAIRE__', true);
require_once(dirname(__FILE__).'/01_fpdf_bookmark.class.php');
class FPDF_Formulaire extends FPDF_BookMark
{
var $javascript = ''; //javascript code
var $n_js; //numéro de l'objet javascript
var $n_cata; //numéro de l'objet catalogue
var $ur; //
function FPDF_Formulaire($orientation='P',$unit='mm',$format='A4')
{
$this->FPDF_BookMark($orientation,$unit,$format);
$this->PDFVersion='1.6';
$this->ur = false;
}
function _putuserrights()
{
if (!$this->ur) return;
$this->_out('/Perms<<');
$this->_out('/UR3<<');
$this->_out('/Reference[<<');
$this->_out('/Type /SigRef');
$this->_out('/TransformMethod /UR3');
$this->_out('/TransformParams<<');
$this->_out('/Type /TransformParams');
$this->_out('/Annots[ /Create /Delete /Modify /Copy /Import /Export ]');
$this->_out('/Document [ /FullSave ]');
$this->_out('/Form[ /Add /FillIn /Delete /SubmitStandalone ]');
$this->_out('/Signature[ /Modify ]');
$this->_out('/V /2.2');
$this->_out('>>');
$this->_out('>>]');
$this->_out('>>');
$this->_out('>>');
}
function _putresources()
{
parent::_putresources();
$this->_putjavascript();
}
function _putcatalog()
{
$this->n_cata = $this->n;
parent::_putcatalog();
if (!empty($this->javascript)) $this->_out('/Names <n_js).' 0 R>>');
$this->_putuserrights();
}
/*
* Create a javascript PDF string.
* @access protected
* @author Johannes Güntert, Nicola Asuni
*/
function _putjavascript()
{
if (empty($this->javascript)) return;
// the following two lines are used to avoid form fields duplication after saving
if ($this->ur)
{
$js1 = "if(!this.getField('pdfoldsaved')) this.addField('pdfoldsaved','text',0, [0, 1, 0, 1]);";
$js2 = "getField('pdfoldsaved').value = 'saved';";
}
else
{
$js1 = '';
$js2 = '';
}
$this->_newobj();
$this->n_js = $this->n;
$this->_out('<<');
$this->_out('/Names [(EmbeddedJS) '.($this->n + 1).' 0 R ]');
$this->_out('>>');
$this->_out('endobj');
$this->_newobj();
$this->_out('<<');
$this->_out('/S /JavaScript');
$this->_out('/JS '.$this->_textstring($js1."\n".$this->javascript."\n".$js2));
$this->_out('>>');
$this->_out('endobj');
}
/*
* Convert color to javascript color.
* @param string $color color name or #RRGGBB
* @access protected
* @author Denis Van Nuffelen, Nicola Asuni
*/
function _JScolor($color)
{
static $aColors = array('transparent', 'black', 'white', 'red', 'green', 'blue', 'cyan', 'magenta', 'yellow', 'dkGray', 'gray', 'ltGray');
if (substr($color,0,1) == '#')
{
return sprintf("['RGB',%.3f,%.3f,%.3f]", hexdec(substr($color,1,2))/255, hexdec(substr($color,3,2))/255, hexdec(substr($color,5,2))/255);
}
if (!in_array($color,$aColors))
{
$this->Error('Invalid color: '.$color);
}
return 'color.'.$color;
}
/*
* Adds a javascript form field.
* @param string $type field type
* @param string $name field name
* @param int $x horizontal position
* @param int $y vertical position
* @param int $w width
* @param int $h height
* @param array $prop array of properties. Possible values are (http://www.adobe.com/devnet/acrobat/pdfs/js_developer_guide.pdf):
- rect: Position and size of field on page.
- borderStyle: Rectangle border appearance.
- strokeColor: Color of bounding rectangle.
- lineWidth: Width of the edge of the surrounding rectangle.
- rotation: Rotation of field in 90-degree increments.
- fillColor: Background color of field (gray, transparent, RGB, or CMYK).
- userName: Short description of field that appears on mouse-over.
- readonly: Whether the user may change the field contents.
- doNotScroll: Whether text fields may scroll.
- display: Whether visible or hidden on screen or in print.
- textFont: Text font.
- textColor: Text color.
- textSize: Text size.
- richText: Rich text.
- richValue: Text.
- comb: Text comb format.
- multiline: Text multiline.
- charLimit: Text limit to number of characters.
- fileSelect: Text file selection format.
- password: Text password format.
- alignment: Text layout in text fields.
- buttonAlignX: X alignment of icon on button face.
- buttonAlignY: Y alignment of icon on button face.
- buttonFitBounds: Relative scaling of an icon to fit inside a button face.
- buttonScaleHow: Relative scaling of an icon to fit inside a button face.
- buttonScaleWhen: Relative scaling of an icon to fit inside a button face.
- highlight: Appearance of a button when pushed.
- style: Glyph style for checkbox and radio buttons.
- numItems: Number of items in a combo box or list box.
- editable: Whether the user can type in a combo box.
- multipleSelection: Whether multiple list box items may be selected.
* @access protected
* @author Denis Van Nuffelen, Nicola Asuni
*/
function _addfield($type, $name, $x, $y, $w, $h, $prop, $js_after = '')
{
if (!isset($prop['textSize'])) $prop['textSize'] = $this->FontSizePt;
if (!isset($prop['strokeColor'])) $prop['strokeColor'] = 'ltGray';
if (isset($prop['value'])) $prop['value'] = str_replace('"', '', $prop['value']);
$this->SetFillColor(240);
if ($w>0 && $h>0)
{
$d = 1/$this->k;
$r = 0.1;
$this->Rect($x+$d*0.5+$r, $y-$d*0.5+$r, $w-$d-2*$r, $h-$d-2*$r, 'F');
}
// javascript inclus
$this->ur = true;
// the followind avoid fields duplication after saving the document
$this->javascript .= "if(this.getField('pdfoldsaved') && this.getField('pdfoldsaved').value != 'saved') {";
$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";
$this->javascript .= 'f'.$name.'.textSize='.$this->FontSizePt.";\n";
while (list($key, $val) = each($prop))
{
if (strcmp(substr($key, -5), 'Color') == 0)
$val = $this->_JScolor($val);
else
$val = '"'.$val.'"';
$this->javascript .= 'f'.$name.'.'.$key.'='.$val.";\n";
}
$this->javascript .= '}';
$this->javascript.= "\n".$js_after;
}
function IncludeJS($script)
{
$this->javascript .= $script;
}
function form_InputHidden($name, $value)
{
$prop = array('value' => $value);
$js_after = '';
$this->_addfield('checkbox', $name, 0, 0, 0.1, 0.1, $prop, $js_after);
}
function form_InputCheckBox($name, $x, $y, $w, $checked)
{
$prop = array();
$prop['value'] = ($checked ? 'Yes' : 'Off');
$js_after = '';
$this->_addfield('checkbox', $name, $x, $y, $w, $w, $prop, $js_after);
}
function form_InputRadio($name, $x, $y, $w)
{
$prop = array();
$js_after = '';
$this->_addfield('radiobutton', $name, $x, $y, $w, $w, $prop, $js_after);
}
function form_InputText($name, $x, $y, $w, $h, $prop)
{
$js_after = '';
$this->_addfield('text', $name, $x, $y, $w, $h, $prop, $js_after);
}
function form_InputButton($name, $x, $y, $w, $h, $caption, $action, $prop)
{
if (!isset($prop['borderStyle'])) $prop['borderStyle'] = 'beveled';
if (!isset($prop['fillColor'])) $prop['fillColor'] = 'ltGray';
if (!isset($prop['strokeColor'])) $prop['strokeColor'] = 'black';
$js_after = 'f'.$name.".buttonSetCaption('".addslashes($caption)."');\n";
$js_after.= 'f'.$name.".setAction('MouseUp','".addslashes($action)."');\n";
$js_after.= 'f'.$name.".highlight='push';\n";
$js_after.= 'f'.$name.".print=false;\n";
$this->_addfield('button', $name, $x, $y, $w, $h, $prop, $js_after);
}
function form_Select($name, $x, $y, $w, $h, $values, $multiligne, $prop)
{
$type = ($multiligne ? 'listbox' : 'combobox');
$s = ''; foreach ($values as $value) { $s .= ($s ? ',' : '')."'".addslashes($value)."'"; }
$js_after = 'f'.$name.'.setItems(['.$s."]);\n";
$this->_addfield($type, $name, $x, $y, $w, $h, $prop, $js_after);
}
}
}
?>