Add-On Dynamic form with JavaScript
Informations
Author:Denis Van NuffelenLicense: FPDF
Description
This script allows to generate forms dynamically when the document opens by the means of some JavaScript code (it requires the JavaScript support extension).Caution: the generated PDF works only with Acrobat Reader 5.1.
It is possible to create text fields, combo boxes, check boxes and buttons. Fields are created at the current position and are given a name. This name allows to manipulate them via JavaScript in order to perform some validation for instance.
Upon field creation, an associative array can be passed to set a number of properties, among which:
TextColor (black by default)
FillColor (transparent by default)
BorderColor (transparent by default)
BorderStyle (solid, dashed, beveled, inset or underline; solid by default)
Colors can be chosen in the following list (case sensitive):
black white red green blue cyan magenta yellow dkGray gray ltGray
or be in the form #RRGGBB.
Methods to create fields are the following:
TextField(string name, float w, float h [, array prop])
name: field name.
w: width.
h: height.
prop: properties. The value property allows to set the initial value. The multiline property allows to define the field as multiline.
ComboBox(string name, float w, float h, array values [, array prop])
name: combobox name.
w: width.
h: height.
values: array containing the list of values.
prop: properties.
CheckBox(string name, float w [, boolean checked [, array prop]])
name: checkbox name.
w: width.
checked: boolean defining the initial state (false by default).
prop: properties.
Button(string name, float w, float h, string caption, string action [, array prop])
name: button name.
w: width.
h: height.
caption: caption.
action: action triggered by the button (JavaScript code).
prop: properties.
The JavaScript is being generated and added to the script property of the objet each time you call one of these methods.
Source
<?php |
Example
Here is an example which creates a form with the different possible field types. When one clicks on the button, the mandatory fields are checked, and, if validation passes, the page is printed.
<?php |
View the result here.