-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgenerate_pdf.php
192 lines (155 loc) · 6.57 KB
/
generate_pdf.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
<?php
// (C) copyleft AGPL license, http://itextpdf.com/terms-of-use/agpl.php, Nikolay Kitsul.
// Note: Use proper escaping functions: pdfff_escape($str), pdfff_checkbox($value).
$error_level = error_reporting();
error_reporting($error_level ^ E_NOTICE);
$error = 0;
// Do not pass parameters from $_REQUEST to proc_open() without validation!
$allowed_files = array('pdf_file_1', 'pdf_file_2');
if (in_array($_REQUEST['file'], $allowed_files))
$file = $_REQUEST['file'] . '.pdf';
else {
echo 'Unknown file.';
exit;
}
// Process parameters.
if ($_REQUEST['flatten'])
$flatten = true;
else
$flatten = false;
if ($_REQUEST['pff_op'] == 'list')
$operation = "list";
else if ($_REQUEST['pff_op'] == 'env_vars')
$operation = "env_vars";
else if ($_REQUEST['pff_op'] == 'dump_fields')
$operation = "dump_fields";
else
$operation = null;
switch ($_REQUEST['file']) {
case 'pdf_file_1':
$fill_func = 'fill_pdf_file_1';
break;
case 'history_2':
$fill_func = 'fill_pdf_file_2';
break;
}
pdfff_fill_pdffile_and_dump_to_http($file, $fill_func, $operation, $flatten);
error_reporting($error_level);
/**
* Fills a pdf file <var>$file</var> with the function provided <var>$fill_func</var> and dumps it to http for the browser to download.
*
* @param string $file pdf filename to fill.
* @param string $fill_func - function name of a function that will fills pdf file by writing to a pipe.
* @param string $operation 'fill' - DEFAULT. Fill pdf file <var>$file</var> with what is in function <var>$fill_func</var>. <br>
* 'list' - List all fields that are available in pdf file $file. <br>
* 'env_vars' - List shell enviroment variables. Useful if returned error code is 127 (when paths are wrong).<br>
* 'dump_fields' - Dump fields, submitted to be submitted to phpformfiller, to browser. Useful if Unix Shell encoding is wrong.<br>
*
* @param boolean $flatten - produce "flat" pdf, that is its forms will not be editable any more.
*/
function pdfff_fill_pdffile_and_dump_to_http($file, $fill_func, $operation = 'fill', $flatten = false) {
$DO_DUMP = false;
if ($flatten)
$flatten = " -flatten";
else
$flatten = "";
//putenv('LANG=en_US.UTF-8');
$env = array('LANG' => 'en_US.UTF-8');
$path = null;
// On FreeBSD open_proc() is not run in a shell and
// thus a PATH with 'usr/local/bin' and CWD are not set.
// $env['PATH'] = '/usr/bin:/bin:/usr/local/bin';
// $path = '/var/www/linux.org/site/htroot/doc';
if ($operation == 'list') {
// List all fields that are available in pdf file $file.
$cmd = 'java -jar pdfformfiller.jar ' . $file . ' -l 2>&1';
$DO_DUMP = true; // send error as text to browser (not as a file to download via http).
} else if ($operation == 'env_vars') {
$cmd = 'set';
$DO_DUMP = true;
} else if ($operation == 'dump_fields') {
$cmd = 'cat';
$DO_DUMP = true;
} else
$cmd = 'java -jar pdfformfiller.jar ' . $file . ' -font "fonts/arial.ttf"' .
$flatten . ' 2>&1';
$descriptorspec = array(
0 => array("pipe", "r"), // stdin is a pipe that the child will read from
1 => array("pipe", "w"), // stdout is a pipe that the child will write to
// 2 => array("file", "error-output.txt", "a") // stderr is a file to write to
2 => array("pipe", "w") // Actually, stderr is sent to stdin " 2>&1"
// in $cmd above, as select() in php is not reliable.
);
$f = proc_open($cmd, $descriptorspec, $pipes, $path, $env /* [, array $other_options ] */);
$error |= $f === false;
if (!$operation || $operation == 'fill')
$error |= $fill_func($pipes[0]);
$error |= false === fclose($pipes[0]);
$result = stream_get_contents($pipes[1]);
$error |= false === $result;
$error |= false === fclose($pipes[1]);
// It is important that you close any pipes before calling
// proc_close in order to avoid a deadlock
$return_value = proc_close($f);
$error |= $return_value != 0;
if (!$error && !$DO_DUMP) {
// Disable cache.
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
// We'll be outputting a PDF
header('Content-type: application/pdf');
header('Content-Transfer-Encoding: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $file . '"');
} else {
header("Content-Type: text/plain");
if ($error) {
echo "ERROR:\n";
if ($return_value == -1)
echo "Error accesing pdfformfiller.\n";
else
echo "pdfformfiller returned error code: $return_value\n\n";
}
}
echo $result;
}
function fill_pdf_file_1($pipe) {
$error = 0;
$error |= false === fwrite($pipe, 'fill_7' . ' ' . pdfff_escape($_POST['ADDRESS']) . "\n");
$error |= false === fwrite($pipe, 'fill_9' . ' ' . pdfff_escape($_POST['PHONE'] . ', ' . $_POST['EMAIL']) . "\n");
$error |= false === fwrite($pipe, 'change_identification' . ' ' . pdfff_checkbox($_POST['IDENTIFICATION']) . "\n");
$error |= false === fwrite($pipe, 'change_adress' . ' ' . pdfff_checkbox($_POST['CHANGE_ADRESS']) . "\n");
return $error;
}
function fill_pdf_file_2($pipe) {
$error = 0;
$error |= false === fwrite($pipe, 'FAMILY' . ' ' . pdfff_escape($_POST['FAMILY']) . "\n");
$error |= false === fwrite($pipe, 'NAME' . ' ' . pdfff_escape($_POST['NAME']) . "\n");
$error |= false === fwrite($pipe, 'SECOND_NAME' . ' ' . pdfff_escape($_POST['SECOND_NAME']) . "\n");
return $error;
}
function pdfff_escape($str) {
//$str = str_replace("\\", "\\\\", $str);
//$str = str_replace("\n", "\\n", $str);
mb_internal_encoding('UTF-8');
mb_regex_encoding('UTF-8');
if (($s = mb_ereg_replace("\\", "\\\\", $str, "p")) !== false)
$str = $s;
// U+2028 utf-8 E280A8 : LINE SEPARATOR LS
if (($s = mb_ereg_replace("\xE2\x80\xA8", "\\n", $str, "p")) !== false)
$str = $s;
//U+2029 utf-8 E280A9 : PARAGRAPH SEPARATOR PS
if (($s = mb_ereg_replace("\xE2\x80\xA8", "\\p", $str, "p")) !== false)
$str = $s;
// DOS newline
if (($s = mb_ereg_replace("\r\n", "\\n", $str, "p")) !== false)
$str = $s;
if (($s = mb_ereg_replace("\n", "\\n", $str, "p")) !== false)
$str = $s;
return $str;
}
function pdfff_checkbox($value) {
if ($value)
return 'Yes';
return 'Off';
}
?>