-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathphpPDF.php
More file actions
executable file
·256 lines (187 loc) · 6.51 KB
/
phpPDF.php
File metadata and controls
executable file
·256 lines (187 loc) · 6.51 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
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
<?
ini_set('max_execution_time', 300);
require_once("ParametrizedPDF.php");
function getRequiredParam($paramName, $item, $itemIdx) {
if(!array_key_exists($paramName, $item)) {
showError("'$paramName' property needs to be specified for item at position $itemIdx.");
}
return $item[$paramName];
}
function getOptionalParam ($paramName, $item, $defaultValue) {
$result = $defaultValue;
if(array_key_exists($paramName, $item)) {
$result = $item[$paramName];
}
return $result;
}
function convertPDFFileToImage($filePath) {
$img = new imagick();
$img->setResolution(210, 210);
$img->readImage($filePath);
$img->resetIterator();
// If the PDF has several pages, we merge all of them in one image.
$img = $img->appendImages(true);
$img->setImageFormat("png");
$img->adaptiveBlurImage(1, 1);
$img->setImageUnits(imagick::RESOLUTION_PIXELSPERINCH);
$data = $img->getImageBlob();
$img = imagecreatefromstring($data);
return $img;
}
function showError($errMesg) {
error_log("phpPDF: ".$errMesg);
$response = array(
"error" => "phpPDF: ".$errMesg);
header("Content-Type: application/json");
echo stripslashes(json_encode($response));
exit(1);
}
$params = null;
if(array_key_exists("params", $_GET)) {
$params =$_GET["params"];
} else if(array_key_exists("params",$_POST)) {
$params = $_POST["params"];
}
if($params) {
// We decode the params into an associative array
$decodedParams = json_decode($params,true);
if(!$decodedParams) {
showError("Params parameter isn't valid JSON!: ".$params);
}
$params = $decodedParams;
} else {
showError("Params parameter is required!");
}
// We copy the uploaded files here as we seem to lose them if try to handle them
// inside ParametrizedPDF... :(
foreach($_FILES as $inputName => $uploadInfo) {
if($uploadInfo["size"]!==0) {
$filePath = $uploadInfo["tmp_name"];
if(!file_exists($filePath)) {
showError("The uploaded file $filePath wasn't found");
}
$uploadPath = sys_get_temp_dir()."/".$uploadInfo["name"];
if(!move_uploaded_file($filePath, $uploadPath)){
showError("The uploaded file $filePath couldn't be moved to $uploadPath");
}
}
}
$outputFormat = strtoupper(getOptionalParam("outputFormat", $params, "PDF"));
if(!in_array($outputFormat, array("PDF","PNG"))) {
showError("Output format must be one of: 'PDF','PNG'");
}
$outputFile = getOptionalParam("outputFile",$params, false);
$downloadFile = getOptionalParam("downloadFile", $params, false);
if($downloadFile) {
// The user wants to download a previously generated file.
$mimeType = $outputFormat=="PDF"?"application/pdf":"image/png";
if(!$outputFile) {
$outputFile = "$downloadFile.". strtolower($outputFormat);
}
header("Content-type: $mimeType");
header("Content-disposition: attachment; filename=$outputFile");
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
// Send Headers: Prevent Caching of File
header('Cache-Control: private');
header('Pragma: private');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
$filePath = sys_get_temp_dir()."/".$downloadFile;
echo file_get_contents($filePath);
unlink($filePath);
exit(0);
}
if(!$outputFile) {
$outputFile = $outputFormat==="PDF"?"doc.pdf":"doc.png";
}
$paperSize = getOptionalParam("size",$params,"A4");
$margin = getOptionalParam("margin", $params, 30);
if(is_array($margin)) {
$marginTop = getOptionalParam("top", $margin,30);
$marginBottom= getOptionalParam("bottom", $margin,30);
$marginRight = getOptionalParam("right", $margin,30);
$marginLeft = getOptionalParam("left", $margin,30);
} else {
$marginTop = $margin;
$marginBottom = $margin;
$marginLeft = $margin;
$marginRight = $margin;
}
$pageOrientation = getOptionalParam("orientation", $params, "P");
$items = array();
if(array_key_exists("items",$params)) {
$items = $params["items"];
} else {
showError("At least one item must be defined! ");
}
$header = getOptionalParam("header", $params, false);
$footer = getOptionalParam("footer", $params, false);
$pdf = new ParametrizedPDF($pageOrientation,"mm",$paperSize);
// We set the file's metadata. It won't be preserved if output is 'PNG'.
$pdf->SetTitle(getOptionalParam("title",$params,""));
$pdf->SetSubject(getOptionalParam("subject",$params,""));
$pdf->SetCreator(getOptionalParam("creator", $params,"Created with phpPDF and TCPDF!"));
$pdf->SetAuthor(getOptionalParam("author", $params,""));
$pdf->SetKeywords(getOptionalParam("keywords",$params,""));
$pdf->SetMargins($marginLeft, $marginTop, $marginRight,true);
$pdf->SetAutoPageBreak(true, $marginBottom);
if($header) {
$pdf->setCustomHeader($header);
}
if($footer) {
$pdf->setCustomFooter($footer);
}
$pdf->AddPage();
$columns = getOptionalParam("columns", $params, 1);
if($columns > 1) {
$pdf->setEqualColumns($columns, ($pdf->getPageWidth())/$columns -5);
}
$pdf->SetFontSize(12);
// We add items to the pdf!
$pdf->addItems($items);
$keepFile = getOptionalParam("keepFile", $params, false);
// We remove the moved uploaded files.
foreach($_FILES as $inputName => $fileInfo) {
unlink(sys_get_temp_dir()."/".$fileInfo["name"]);
}
if(!$keepFile && $outputFormat==="PDF") {
$pdf->Output($outputFile,"D");
} else if(!$keepFile) {
$tmpPdfOut = tempnam(sys_get_temp_dir(),"pdfOut");
// We write the file to a tmporal file.
$pdf->Output($tmpPdfOut,"F");
$pngImageOut = convertPDFFileToImage($tmpPdfOut);
// We remove the temporal file, now that we have the image.
unlink($tmpPdfOut);
header("Content-type: image/png");
header("Content-disposition: attachment; filename=$outputFile");
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
// Send Headers: Prevent Caching of File
header('Cache-Control: private');
header('Pragma: private');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
imagealphablending($pngImageOut, true);
//imagesavealpha($pngImageOut, true);
imagepng($pngImageOut, null, 9);
} else {
$tmpPdfOut = tempnam(sys_get_temp_dir(), $outputFile."_");
// We write the file to a tmporal file.
$pdf->Output($tmpPdfOut,"F");
if($outputFormat==="PNG") {
$pngImageOut = convertPDFFileToImage($tmpPdfOut);
imagealphablending($pngImageOut, true);
//imagesavealpha($pngImageOut, true);
imagepng($pngImageOut, $tmpPdfOut, 9);
}
// We return a json object with the download url.
header("Content-Type: text/html");
header('Cache-Control: private');
header('Pragma: private');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
$response = array(
"downloadableFile" => str_replace(sys_get_temp_dir()."/","",$tmpPdfOut)
);
// echo out the JSON
echo stripslashes(json_encode($response));
}