-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathLocalInputSource.php
More file actions
381 lines (358 loc) · 12.4 KB
/
LocalInputSource.php
File metadata and controls
381 lines (358 loc) · 12.4 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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
<?php
/**
* Local input handling.
*/
namespace Mindee\Input;
use CURLFile;
use Exception;
use Mindee\Error\ErrorCode;
use Mindee\Error\MindeeMimeTypeException;
use Mindee\Error\MindeePDFException;
use Mindee\Error\MindeeSourceException;
use Mindee\Image\ImageCompressor;
use Mindee\PDF\PDFCompressor;
use Mindee\PDF\PDFUtils;
use setasign\Fpdi\Fpdi;
use setasign\Fpdi\PdfParser\PdfParserException;
use setasign\Fpdi\PdfReader\PdfReaderException;
/**
* List of allowed mime types for document parsing.
*/
const ALLOWED_MIME_TYPES = [
'application/pdf',
'image/heic',
'image/png',
'image/jpg',
'image/jpeg',
'image/tiff',
'image/webp',
'application/octet-stream',
];
/**
* Base class for all input sources coming from the local machine.
*/
abstract class LocalInputSource extends InputSource
{
/**
* @var CURLFile File object, as a CURLFile for simplicity.
*/
public CURLFile $fileObject;
/**
* @var string Name of the file, mandatory for proper Mime type handling server-side.
*/
public string $fileName;
/**
* @var string File Mime type, as a string.
*/
public string $fileMimetype;
/**
* @var string|null Path of the file for files retrieved from a path.
*/
public ?string $filePath;
/**
* Checks if the file needs fixing.
* @return void
*/
public function checkNeedsFix(): void
{
if ($this->fileMimetype == 'application/octet-stream') {
trigger_error(
'File type application/octet-stream is probably incorrect. '
. 'Try to run fixPDF() on the file.',
E_USER_WARNING
);
}
}
/**
* Checks the mimetype integrity of a file.
*
* @return void
* @throws MindeeMimeTypeException Throws if the Mime type isn't allowed.
*/
private function checkMimeType()
{
if (!in_array($this->fileMimetype, ALLOWED_MIME_TYPES)) {
$fileTypes = implode(', ', ALLOWED_MIME_TYPES);
throw new MindeeMimeTypeException(
"File type " .
$this->fileMimetype .
" not allowed, must be one of $fileTypes.",
ErrorCode::USER_OPERATION_ERROR
);
}
}
/**
* Base constructor, mostly used for Mime type checking.
*/
public function __construct()
{
$this->checkMimeType();
}
/**
* Checks whether the file type is a PDF.
*
* @return boolean
*/
public function isPDF(): bool
{
$this->checkMimeType();
return $this->fileMimetype == 'application/pdf';
}
/**
* Counts the amount of pages in a PDF.
*
* @return integer
* @throws MindeePDFException Throws if the source pdf can't be properly processed.
* @throws MindeeSourceException Throws if the source isn't a pdf.
*/
public function getPageCount(): int
{
if (!$this->isPDF()) {
throw new MindeeSourceException(
"File is not a PDF.",
ErrorCode::USER_OPERATION_ERROR
);
}
$pdf = new FPDI();
try {
return $pdf->setSourceFile($this->fileObject->getFilename());
} catch (PdfParserException $e) {
throw new MindeePDFException(
"Failed to read PDF file.",
ErrorCode::PDF_CANT_PROCESS,
$e
);
}
}
/**
* @return integer
* @deprecated
*/
public function countDocPages(): int
{
return $this->getPageCount();
}
/**
* @param string $fileBytes Raw data as bytes.
* @return void
*/
private function saveBytesAsFile(string $fileBytes): void
{
$cutPdfTempFile = tempnam(sys_get_temp_dir(), 'mindee_cut_pdf_');
file_put_contents($cutPdfTempFile, $fileBytes);
$this->filePath = $cutPdfTempFile;
$this->fileObject = new CURLFile($cutPdfTempFile, $this->fileMimetype, $this->fileName);
}
/**
* Create a new PDF from pages and set it as the main file object.
* @param array $pageNumbers Array of page numbers to add to the newly created PDF.
* @return void
* @throws MindeePDFException Throws if the pdf file can't be processed.
*/
public function mergePDFPages(array $pageNumbers): void
{
try {
$pdf = new FPDI();
$pdf->setSourceFile($this->filePath);
foreach ($pageNumbers as $pageNumber) {
$pdf->AddPage();
$pdf->useTemplate($pdf->importPage($pageNumber + 1));
}
$this->saveBytesAsFile($pdf->Output($this->fileName, 'S'));
$pdf->Close();
} catch (PdfParserException | PdfReaderException $e) {
throw new MindeePDFException(
"Failed to read PDF file.",
ErrorCode::PDF_CANT_PROCESS,
$e
);
}
}
/**
* Checks whether the contents of a PDF are empty.
* @param integer $threshold Semi-arbitrary threshold of minimum bytes on the page for it to be considered empty.
*
* @return boolean
* @throws MindeePDFException Throws if the pdf file can't be processed.
*/
public function isPDFEmpty(int $threshold = 1024): bool
{
try {
$pdf = new FPDI();
$pageCount = $pdf->setSourceFile($this->fileObject->getFilename());
$pdf->Close();
for ($pageNumber = 0; $pageNumber < $pageCount; $pageNumber++) {
$pdfPage = new FPDI();
$pdfPage->setSourceFile($this->fileObject->getFilename());
$pdfPage->AddPage();
$pdfPage->useTemplate($pdfPage->importPage($pageNumber + 1));
if (strlen($pdfPage->Output('', 'S')) > $threshold) {
$pdfPage->Close();
return false;
}
$pdfPage->Close();
}
} catch (PdfParserException | PdfReaderException $e) {
throw new MindeePDFException(
"Failed to read PDF file.",
ErrorCode::PDF_CANT_PROCESS,
$e
);
}
return true;
}
/**
* Reads the contents of the file.
*
* @return array
*/
public function readContents(): array
{
$fileHandle = fopen($this->fileObject->getFilename(), 'rb');
$strContents = fread($fileHandle, filesize($this->fileObject->getFilename()));
fclose($fileHandle);
return [basename($this->fileObject->getFilename()), $strContents];
}
/**
* Attempts to fix a PDF file.
*
* @return void
* @throws MindeeSourceException Throws if the file couldn't be fixed.
*/
public function fixPDF(): void
{
if (str_starts_with($this->fileMimetype, "image/")) {
error_log("Input file is an image, skipping PDF fix.");
return;
}
$bytesContent = file_get_contents($this->fileObject->getFilename());
$pdfMarkerPosition = strrpos(strtoupper($bytesContent), '%PDF');
if ($pdfMarkerPosition !== false) {
$tempFile = tempnam(sys_get_temp_dir(), 'pdf_fix_');
rename($tempFile, $tempFile .= "." . pathinfo($this->fileName, PATHINFO_EXTENSION));
file_put_contents($tempFile, substr($bytesContent, $pdfMarkerPosition));
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$this->fileMimetype = finfo_file($finfo, $tempFile);
finfo_close($finfo);
$this->fileObject = new CURLFile($tempFile, $this->fileMimetype, $this->fileName);
return;
}
throw new MindeeSourceException(
"PDF file could not be fixed.",
ErrorCode::FILE_OPERATION_ERROR
);
}
/**
* @param integer $quality Quality of the output file.
* @param integer|null $maxWidth Maximum width (Ignored for PDFs).
* @param integer|null $maxHeight Maximum height (Ignored for PDFs).
* @param boolean $forceSourceTextCompression Whether to force the operation on PDFs with source text.
* This will attempt to re-render PDF text over the rasterized original.
* The script will attempt to re-write text, but might not support all fonts & encoding.
* If disabled, ignored the operation.
* WARNING: this operation is strongly discouraged.
* @param boolean $disableSourceText If the PDF has source text, whether to re-apply it to the
* original or not. Needs force_source_text to work.
* @return void
*/
public function compress(
int $quality = 85,
?int $maxWidth = null,
?int $maxHeight = null,
bool $forceSourceTextCompression = false,
bool $disableSourceText = true
): void {
if ($this->isPDF()) {
$this->fileObject = PDFCompressor::compress(
$this->fileObject,
$quality,
$forceSourceTextCompression,
$disableSourceText
);
$this->fileMimetype = 'application/pdf';
$pathInfo = pathinfo($this->filePath);
$this->filePath = $pathInfo['dirname'] . DIRECTORY_SEPARATOR . $pathInfo['filename'] . '.pdf';
} else {
$this->fileObject = ImageCompressor::compress(
$this->fileObject,
$quality,
$maxWidth,
$maxHeight
);
$this->fileMimetype = 'image/jpeg';
$pathInfo = pathinfo($this->filePath);
$this->filePath = $pathInfo['dirname'] . DIRECTORY_SEPARATOR . $pathInfo['filename'] . '.jpg';
}
}
/**
* Checks the source file for source text.
*
* @return boolean Returns false if none is found, or if the file isn't a PDF.
* @throws Exception Throws if an instance of pdf-parser can't be created.
*/
public function hasSourceText(): bool
{
if (!$this->isPDF()) {
return false;
}
return PDFUtils::hasSourceText($this->filePath);
}
/**
* Applies PDF-specific operations on the current file based on the specified PageOptions.
*
* @param PageOptions|null $pageOptions The options specifying which pages to modify or retain in the PDF file.
* @return void
* @throws MindeePDFException If a PDF processing error occurs during the operation.
*/
public function applyPageOptions(?PageOptions $pageOptions): void
{
if ($this->isPDFEmpty()) {
throw new MindeePDFException(
"Pages are empty in PDF file.",
ErrorCode::USER_INPUT_ERROR
);
}
if ($this->getPageCount() < $pageOptions->onMinPage) {
return;
}
$allPages = range(0, $this->getPageCount() - 1);
$pagesToKeep = [];
if ($pageOptions->operation == KEEP_ONLY) {
foreach ($pageOptions->pageIndexes as $pageId) {
if ($pageId < 0) {
$pageId = $this->getPageCount() + $pageId;
}
if (!in_array($pageId, $allPages)) {
error_log("Page index '" . $pageId . "' is not present in source document");
} else {
$pagesToKeep[] = $pageId;
}
}
} elseif ($pageOptions->operation == REMOVE) {
$pagesToRemove = [];
foreach ($pageOptions->pageIndexes as $pageId) {
if ($pageId < 0) {
$pageId = $this->getPageCount() + $pageId;
}
if (!in_array($pageId, $allPages)) {
error_log("Page index '" . $pageId . "' is not present in source document");
} else {
$pagesToRemove[] = $pageId;
}
}
$pagesToKeep = array_diff($allPages, $pagesToRemove);
} else {
throw new MindeePDFException(
"Unknown operation '" . $pageOptions->operation . "'.",
ErrorCode::USER_OPERATION_ERROR
);
}
if (count($pagesToKeep) < 1) {
throw new MindeePDFException(
"Resulting PDF would have no pages left.",
ErrorCode::USER_OPERATION_ERROR
);
}
$this->mergePDFPages($pagesToKeep);
}
}