|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* PDFtk wrapper |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright 2014-2022 Institute of Legal Medicine, Medical University of Innsbruck |
|
6
|
|
|
* @author Andreas Erhard <[email protected]> |
|
7
|
|
|
* @license LGPL-3.0-only |
|
8
|
|
|
* @link http://www.gerichtsmedizin.at/ |
|
9
|
|
|
* |
|
10
|
|
|
* @package pdftk |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Gmi\Toolkit\Pdftk; |
|
14
|
|
|
|
|
15
|
|
|
use Gmi\Toolkit\Pdftk\Exception\SplitException; |
|
16
|
|
|
|
|
17
|
|
|
use Exception; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Splits PDF files. |
|
21
|
|
|
*/ |
|
22
|
|
|
class Splitter |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var PdftkWrapper |
|
26
|
|
|
*/ |
|
27
|
|
|
private $wrapper; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Constructor. |
|
31
|
|
|
* |
|
32
|
|
|
* @param PdftkWrapper $wrapper |
|
33
|
|
|
*/ |
|
34
|
9 |
|
public function __construct(PdftkWrapper $wrapper = null) |
|
35
|
|
|
{ |
|
36
|
9 |
|
$this->wrapper = $wrapper ?: new PdftkWrapper(); |
|
37
|
9 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Splits a PDF according to the provided filename => pages mapping. |
|
41
|
|
|
* |
|
42
|
|
|
* @param string $inputFile Filename of the input file which should be split |
|
43
|
|
|
* @param array $mapping Mapping of output filename to page numbers |
|
44
|
|
|
* @param string $outputFolder Folder where the output files should be stored (without trailing slash). |
|
45
|
|
|
* If the output folder is null, the filenames of the mapping |
|
46
|
|
|
* (which can contain a path) are used as they are. |
|
47
|
|
|
* |
|
48
|
|
|
* @throws SplitException if the PDF split fails |
|
49
|
|
|
*/ |
|
50
|
4 |
|
public function split($inputFile, $mapping, $outputFolder = null) |
|
51
|
|
|
{ |
|
52
|
4 |
|
$commandLines = $this->buildCommandLines($inputFile, $mapping, $outputFolder); |
|
53
|
|
|
|
|
54
|
4 |
|
foreach ($commandLines as $commandLine) { |
|
55
|
4 |
|
$process = $this->wrapper->createProcess($commandLine); |
|
56
|
|
|
|
|
57
|
|
|
try { |
|
58
|
4 |
|
$process->mustRun(); |
|
59
|
2 |
|
} catch (Exception $e) { |
|
60
|
2 |
|
throw new SplitException( |
|
61
|
2 |
|
sprintf('Failed to split PDF "%s"! Error: %s', $inputFile, $e->getMessage()), |
|
62
|
2 |
|
0, |
|
63
|
2 |
|
$e, |
|
64
|
2 |
|
$process->getErrorOutput(), |
|
65
|
4 |
|
$process->getOutput() |
|
66
|
|
|
); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
2 |
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Builds the pdftk command lines for splitting. |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $inputFile |
|
75
|
|
|
* @param array $mapping |
|
76
|
|
|
* @param string $outputFolder |
|
77
|
|
|
* |
|
78
|
|
|
* @return string[] |
|
79
|
|
|
*/ |
|
80
|
4 |
|
private function buildCommandLines($inputFile, $mapping, $outputFolder = null) |
|
81
|
|
|
{ |
|
82
|
4 |
|
$commandLines = []; |
|
83
|
|
|
|
|
84
|
4 |
|
foreach ($mapping as $filename => $pages) { |
|
85
|
4 |
|
if ($outputFolder) { |
|
86
|
3 |
|
$target = sprintf('%s/%s', $outputFolder, $filename); |
|
87
|
|
|
} else { |
|
88
|
1 |
|
$target = $filename; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
4 |
|
$commandLines[] = sprintf( |
|
92
|
4 |
|
'%s %s cat %s output %s', |
|
93
|
4 |
|
$this->wrapper->getBinary(), |
|
94
|
4 |
|
escapeshellarg($inputFile), |
|
95
|
4 |
|
implode(' ', $pages), |
|
96
|
4 |
|
escapeshellarg($target) |
|
97
|
|
|
); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
4 |
|
return $commandLines; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|