-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathforge_fdf.php
More file actions
146 lines (124 loc) · 4.08 KB
/
forge_fdf.php
File metadata and controls
146 lines (124 loc) · 4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
/* forge_fdf, by Sid Steward
version 1.0
visit: www.pdfhacks.com/forge_fdf/
For text fields, combo boxes and list boxes, add
field values as a name => value pair to $fdf_data_strings.
For check boxes and radio buttons, add field values
as a name => value pair to $fdf_data_names. Typically,
true and false correspond to the (case sensitive)
names "Yes" and "Off".
Any field added to the $fields_hidden or $fields_readonly
array must also be a key in $fdf_data_strings or
$fdf_data_names; this might be changed in the future
Any field listed in $fdf_data_strings or $fdf_data_names
that you want hidden or read-only must have its field
name added to $fields_hidden or $fields_readonly; do this
even if your form has these bits set already
PDF can be particular about CR and LF characters, so I
spelled them out in hex: CR == \x0d : LF == \x0a
*/
function escape_pdf_string( $ss )
{
$ss_esc= '';
$ss_len= strlen( $ss );
for( $ii= 0; $ii< $ss_len; ++$ii ) {
if( ord($ss[$ii])== 0x28 || // open paren
ord($ss[$ii])== 0x29 || // close paren
ord($ss[$ii])== 0x5c ) // backslash
{
$ss_esc.= chr(0x5c).$ss[$ii]; // escape the character w/ backslash
}
else if( ord($ss[$ii]) < 32 || 126 < ord($ss[$ii]) ) {
$ss_esc.= sprintf( "\\%03o", ord($ss[$ii]) ); // use an octal code
}
else {
$ss_esc.= $ss[$ii];
}
}
return $ss_esc;
}
/**
$key = addcslashes($key, "\n\r\t\\()");
$val = addcslashes($val, "\n\r\t\\()");
**/
function escape_pdf_name( $ss )
{
$ss_esc= '';
$ss_len= strlen( $ss );
for( $ii= 0; $ii< $ss_len; ++$ii ) {
if( ord($ss[$ii]) < 33 || 126 < ord($ss[$ii]) ||
ord($ss[$ii])== 0x23 ) // hash mark
{
$ss_esc.= sprintf( "#%02x", ord($ss[$ii]) ); // use a hex code
}
else {
$ss_esc.= $ss[$ii];
}
}
return $ss_esc;
}
/**
* Generates the fdf code
*
*@param String $pdf_form_url: a string containing a URL path to a PDF file on the
* server. This PDF MUST exist and contain fields with
* the names referenced by $pdf_data for this function
* to work.
*@param Array $fdf_data_strings: an array of any fields in $pdf_form_url that you want to
* populate, of the form key=>val; where the field
* name is the key, and the field's value is in val.
*@return String
**/
function forge_fdf( $pdf_form_url,
$fdf_data_strings,
$fdf_data_names,
$fields_hidden,
$fields_readonly )
{
$fdf = "%FDF-1.2\x0d%\xe2\xe3\xcf\xd3\x0d\x0a"; // header
$fdf.= "1 0 obj\x0d<< "; // open the Root dictionary
$fdf.= "\x0d/FDF << "; // open the FDF dictionary
$fdf.= "/Fields [ "; // open the form Fields array
// string data, used for text fields, combo boxes and list boxes
foreach( $fdf_data_strings as $key => $value ) {
$fdf.= "<< /V (".escape_pdf_string($value).")".
"/T (".escape_pdf_string($key).") ";
if( in_array( $key, $fields_hidden ) )
$fdf.= "/SetF 2 ";
else
$fdf.= "/ClrF 2 ";
if( in_array( $key, $fields_readonly ) )
$fdf.= "/SetFf 1 ";
else
$fdf.= "/ClrFf 1 ";
$fdf.= ">> \x0d";
}
// name data, used for checkboxes and radio buttons
// (e.g., /Yes and /Off for true and false)
foreach( $fdf_data_names as $key => $value ) {
$fdf.= "<< /V /".escape_pdf_name($value).
" /T (".escape_pdf_string($key).") ";
if( in_array( $key, $fields_hidden ) )
$fdf.= "/SetF 2 ";
else
$fdf.= "/ClrF 2 ";
if( in_array( $key, $fields_readonly ) )
$fdf.= "/SetFf 1 ";
else
$fdf.= "/ClrFf 1 ";
$fdf.= ">> \x0d";
}
$fdf.= "] \x0d"; // close the Fields array
// the PDF form filename or URL, if given
if( $pdf_form_url ) {
$fdf.= "/F (".escape_pdf_string($pdf_form_url).") \x0d";
}
$fdf.= ">> \x0d"; // close the FDF dictionary
$fdf.= ">> \x0dendobj\x0d"; // close the Root dictionary
// trailer; note the "1 0 R" reference to "1 0 obj" above
$fdf.= "trailer\x0d<<\x0d/Root 1 0 R \x0d\x0d>>\x0d";
$fdf.= "%%EOF\x0d\x0a";
return $fdf;
}
?>