1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ottosmops\Pdfvalidate; |
4
|
|
|
|
5
|
|
|
use Ottosmops\Pdfvalidate\Exceptions\PdfNotValide; |
6
|
|
|
use Ottosmops\Pdfvalidate\Exceptions\FileNotFound; |
7
|
|
|
use Ottosmops\Pdfvalidate\Exceptions\BinaryNotFound; |
8
|
|
|
use Ottosmops\Pdfvalidate\Exceptions\MimeTypeIsNotPdf; |
9
|
|
|
use Symfony\Component\Process\Process; |
10
|
|
|
|
11
|
|
|
class Validator { |
12
|
|
|
|
13
|
|
|
public $file; |
14
|
|
|
|
15
|
|
|
public $error = ''; |
16
|
|
|
|
17
|
|
|
public $process = ''; |
18
|
|
|
|
19
|
|
|
public $output; |
20
|
|
|
|
21
|
|
|
public $executable; |
22
|
|
|
|
23
|
6 |
|
public function __construct($file, $executable = 'pdftocairo') |
24
|
|
|
{ |
25
|
6 |
|
$this->executable = $executable; |
26
|
6 |
|
$this->checkExecutable(); |
27
|
5 |
|
if (!is_file($file)) { |
28
|
1 |
|
throw new FileNotFound($file); |
29
|
|
|
} |
30
|
4 |
|
if (!self::is_pdf($file)) { |
31
|
1 |
|
throw new MimeTypeIsNotPdf($file); |
32
|
|
|
} |
33
|
3 |
|
$this->file = $file; |
34
|
3 |
|
} |
35
|
|
|
|
36
|
6 |
|
public function checkExecutable() |
37
|
|
|
{ |
38
|
6 |
|
$process = new Process('which ' . $this->executable); |
39
|
6 |
|
$process->run(); |
40
|
6 |
|
if (!$process->isSuccessful()) { |
41
|
1 |
|
throw new BinaryNotFound($process); |
42
|
|
|
} |
43
|
|
|
|
44
|
5 |
|
return true; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
|
48
|
4 |
|
public static function is_pdf($file) |
49
|
|
|
{ |
50
|
4 |
|
$finfo = finfo_open(FILEINFO_MIME_TYPE); |
51
|
4 |
|
return finfo_file($finfo, $file) == 'application/pdf'; |
52
|
|
|
} |
53
|
|
|
|
54
|
3 |
|
public function check() |
55
|
|
|
{ |
56
|
3 |
|
$command = $this->executable . ' -pdf '. $this->file .' - 2>&1 >/dev/null'; |
57
|
|
|
|
58
|
3 |
|
$process = new Process($command); |
59
|
3 |
|
$process->run(); |
60
|
|
|
|
61
|
3 |
|
$output = $process->getOutput(); |
62
|
|
|
|
63
|
3 |
|
if (!$output) { |
64
|
1 |
|
return true; |
65
|
|
|
}; |
66
|
|
|
|
67
|
2 |
|
if (mb_strpos($output, 'Error') !== false) { |
68
|
2 |
|
$this->error = "Could open the PDF, but the PDF seems to be corrupted."; |
69
|
|
|
} |
70
|
|
|
|
71
|
2 |
|
if (mb_strpos($output, 'Error opening') !== false) { |
72
|
1 |
|
$this->error = "Could not open the PDF."; |
73
|
|
|
} |
74
|
|
|
|
75
|
2 |
|
$this->output = $output; |
76
|
2 |
|
return false; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
} |
80
|
|
|
|