1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace iio\libmergepdf; |
6
|
|
|
|
7
|
|
|
use iio\libmergepdf\Driver\DriverInterface; |
8
|
|
|
use iio\libmergepdf\Driver\DefaultDriver; |
9
|
|
|
use iio\libmergepdf\Source\SourceInterface; |
10
|
|
|
use iio\libmergepdf\Source\FileSource; |
11
|
|
|
use iio\libmergepdf\Source\RawSource; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Merge existing pdfs into one |
15
|
|
|
* |
16
|
|
|
* Note that your PDFs are merged in the order that you add them |
17
|
|
|
*/ |
18
|
|
|
final class Merger |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var SourceInterface[] List of pdf sources to merge |
22
|
|
|
*/ |
23
|
|
|
private $sources = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var DriverInterface |
27
|
|
|
*/ |
28
|
|
|
private $driver; |
29
|
|
|
|
30
|
|
|
public function __construct(DriverInterface $driver = null) |
31
|
|
|
{ |
32
|
|
|
$this->driver = $driver ?: new DefaultDriver; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Add raw PDF from string |
37
|
|
|
*/ |
38
|
|
|
public function addRaw(string $content, PagesInterface $pages = null): void |
39
|
|
|
{ |
40
|
|
|
$this->sources[] = new RawSource($content, $pages); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Add PDF from file |
45
|
|
|
*/ |
46
|
|
|
public function addFile(string $filename, PagesInterface $pages = null): void |
47
|
|
|
{ |
48
|
|
|
$this->sources[] = new FileSource($filename, $pages); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Add files using iterator |
53
|
|
|
* |
54
|
|
|
* Note that optional pages constraint is used for every added pdf |
55
|
|
|
*/ |
56
|
|
|
public function addIterator(iterable $iterator, PagesInterface $pages = null): void |
57
|
|
|
{ |
58
|
|
|
foreach ($iterator as $filename) { |
59
|
|
|
$this->addFile($filename, $pages); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Merges loaded PDFs |
65
|
|
|
*/ |
66
|
|
|
public function merge(): string |
67
|
|
|
{ |
68
|
|
|
return $this->driver->merge(...$this->sources); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Reset internal state |
73
|
|
|
*/ |
74
|
|
|
public function reset(): void |
75
|
|
|
{ |
76
|
|
|
$this->sources = []; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|