forked from andrewheiss/pdftk-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdftk-php.php
315 lines (264 loc) · 10 KB
/
pdftk-php.php
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<?php
##################################################################################################################
#
# pdftk-php Class
# http://code.google.com/p/pdftk-php/
# http://www.pdfhacks.com/forge_fdf/
#
# License: Released under New BSD license - http://www.opensource.org/licenses/bsd-license.php
#
# Purpose: Contains functions used to inject data from MySQL into an empty PDF form
#
# Authors: Andrew Heiss (www.andrewheiss.com), Sid Steward (http://www.oreillynet.com/pub/au/1754)
#
# History:
# 8/26/08 - Initial programming
#
# Usage:
# $pdfmaker = new pdftk_php;
#
# $fdf_data_strings = array();
# $fdf_data_names = array();
# $fields_hidden = array();
# $fields_readonly = array();
# $pdf_original = "string"; = filename of the original, empty pdf form
# $pdf_filename = "string"; = filename to be used for the output pdf
#
# $pdfmaker->make_pdf($fdf_data_strings, $fdf_data_names, $fields_hidden, $fields_readonly, $pdf_filename);
#
##################################################################################################################
class pdftk_php {
#############################################################################
#
# Function name: makePDF
#
# Purpose: Generate an FDF file from db data, inject the FDF in an empty PDF form
#
# Incoming parameters:
# $fetched_array - one row of fetched MySQL data saved as a variable
#
# Returns: Downloaded PDF file
#
# Notes:
# * For text fields, combo boxes and list boxes, add field values as a name => value pair to $fdf_data_strings. An example of $fdf_data_strings is given in /example/download.php
# * 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
#
#############################################################################
public function make_pdf($fdf_data_strings, $fdf_data_names, $fields_hidden, $fields_readonly, $pdf_original, $pdf_filename) {
// Create the fdf file
$fdf = $this->forge_fdf('', $fdf_data_strings, $fdf_data_names, $fields_hidden, $fields_readonly);
// Save the fdf file temporarily - make sure the server has write permissions in the folder you specify in tempnam()
$fdf_fn = tempnam(".", "fdf");
$fp = fopen($fdf_fn, 'w');
if($fp) {
fwrite($fp, $fdf);
fclose($fp);
// Send a force download header to the browser with a file MIME type
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=\"$pdf_filename\"");
// Actually make the PDF by running pdftk - make sure the path to pdftk is correct
// The PDF will be output directly to the browser - apart from the original PDF file, no actual PDF wil be saved on the server.
passthru("/usr/local/bin/pdftk $pdf_original fill_form $fdf_fn output - flatten");
// delete temporary fdf file
unlink( $fdf_fn );
}
else { // error
echo 'Error: unable to write temp fdf file: '. $fdf_fn;
}
} // end of make_pdf()
protected function forge_fdf($pdf_form_url, &$fdf_data_strings, &$fdf_data_names, &$fields_hidden, &$fields_readonly) {
/* forge_fdf, by Sid Steward
version 1.1
visit: www.pdfhacks.com/forge_fdf/
PDF can be particular about CR and LF characters, so I spelled them out in hex: CR == \x0d : LF == \x0a
*/
$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
$fdf_data_strings = $this->burst_dots_into_arrays( $fdf_data_strings );
$this->forge_fdf_fields_strings( $fdf,
$fdf_data_strings,
$fields_hidden,
$fields_readonly );
$fdf_data_names= $this->burst_dots_into_arrays( $fdf_data_names );
$this->forge_fdf_fields_names( $fdf,
$fdf_data_names,
$fields_hidden,
$fields_readonly );
$fdf.= "] \x0d"; // close the Fields array
// the PDF form filename or URL, if given
if( $pdf_form_url ) {
$fdf.= "/F (".$this->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;
}
public function escape_pdf_string( $ss ) {
$backslash= chr(0x5c);
$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.= $backslash.$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;
}
protected 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;
}
// In PDF, partial form field names are combined using periods to
// yield the full form field name; we'll take these dot-delimited
// names and then expand them into nested arrays, here; takes
// an array that uses dot-delimited names and returns a tree of arrays;
//
protected function burst_dots_into_arrays( &$fdf_data_old ) {
$fdf_data_new= array();
foreach( $fdf_data_old as $key => $value ) {
$key_split= explode( '.', (string)$key, 2 );
if( count($key_split)== 2 ) { // handle dot
if( !array_key_exists( (string)($key_split[0]), $fdf_data_new ) ) {
$fdf_data_new[ (string)($key_split[0]) ]= array();
}
if( gettype( $fdf_data_new[ (string)($key_split[0]) ] )!= 'array' ) {
// this new key collides with an existing name; this shouldn't happen;
// associate string value with the special empty key in array, anyhow;
$fdf_data_new[ (string)($key_split[0]) ]=
array( '' => $fdf_data_new[ (string)($key_split[0]) ] );
}
$fdf_data_new[ (string)($key_split[0]) ][ (string)($key_split[1]) ]= $value;
}
else { // no dot
if( array_key_exists( (string)($key_split[0]), $fdf_data_new ) &&
gettype( $fdf_data_new[ (string)($key_split[0]) ] )== 'array' )
{ // this key collides with an existing array; this shouldn't happen;
// associate string value with the special empty key in array, anyhow;
$fdf_data_new[ (string)$key ]['']= $value;
}
else { // simply copy
$fdf_data_new[ (string)$key ]= $value;
}
}
}
foreach( $fdf_data_new as $key => $value ) {
if( gettype($value)== 'array' ) {
$fdf_data_new[ (string)$key ]= $this->burst_dots_into_arrays( $value ); // recurse
}
}
return $fdf_data_new;
}
protected function forge_fdf_fields_flags( &$fdf,
$field_name,
&$fields_hidden,
&$fields_readonly ) {
if( in_array( $field_name, $fields_hidden ) )
$fdf.= "/SetF 2 "; // set
else
$fdf.= "/ClrF 2 "; // clear
if( in_array( $field_name, $fields_readonly ) )
$fdf.= "/SetFf 1 "; // set
else
$fdf.= "/ClrFf 1 "; // clear
}
protected function forge_fdf_fields( &$fdf,
&$fdf_data,
&$fields_hidden,
&$fields_readonly,
$accumulated_name,
$strings_b ) // true <==> $fdf_data contains string data
//
// string data is used for text fields, combo boxes and list boxes;
// name data is used for checkboxes and radio buttons, and
// /Yes and /Off are commonly used for true and false
{
if( 0< strlen( $accumulated_name ) ) {
$accumulated_name.= '.'; // append period seperator
}
foreach( $fdf_data as $key => $value ) {
// we use string casts to prevent numeric strings from being silently converted to numbers
$fdf.= "<< "; // open dictionary
if( gettype($value)== 'array' ) { // parent; recurse
$fdf.= "/T (".$this->escape_pdf_string( (string)$key ).") "; // partial field name
$fdf.= "/Kids [ "; // open Kids array
// recurse
$this->forge_fdf_fields( $fdf,
$value,
$fields_hidden,
$fields_readonly,
$accumulated_name. (string)$key,
$strings_b );
$fdf.= "] "; // close Kids array
}
else {
// field name
$fdf.= "/T (".$this->escape_pdf_string( (string)$key ).") ";
// field value
if( $strings_b ) { // string
$fdf.= "/V (".$this->escape_pdf_string( (string)$value ).") ";
}
else { // name
$fdf.= "/V /".$this->escape_pdf_name( (string)$value ). " ";
}
// field flags
$this->forge_fdf_fields_flags( $fdf,
$accumulated_name. (string)$key,
$fields_hidden,
$fields_readonly );
}
$fdf.= ">> \x0d"; // close dictionary
}
}
protected function forge_fdf_fields_strings( &$fdf,
&$fdf_data_strings,
&$fields_hidden,
&$fields_readonly ) {
return
$this->forge_fdf_fields( $fdf,
$fdf_data_strings,
$fields_hidden,
$fields_readonly,
'',
true ); // true => strings data
}
protected function forge_fdf_fields_names( &$fdf,
&$fdf_data_names,
&$fields_hidden,
&$fields_readonly ) {
return
$this->forge_fdf_fields( $fdf,
$fdf_data_names,
$fields_hidden,
$fields_readonly,
'',
false ); // false => names data
}
}
?>