diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:45:49 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:45:49 +0000 |
commit | 0ff39c83d38ce538a9f5dba53eca0fa9cb16d9e6 (patch) | |
tree | 84c735df2e97350a721273e9dd425729d43cc8a2 /vendor/iio/libmergepdf/src | |
parent | Initial commit. (diff) | |
download | icingaweb2-module-pdfexport-0ff39c83d38ce538a9f5dba53eca0fa9cb16d9e6.tar.xz icingaweb2-module-pdfexport-0ff39c83d38ce538a9f5dba53eca0fa9cb16d9e6.zip |
Adding upstream version 0.10.2+dfsg1.upstream/0.10.2+dfsg1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/iio/libmergepdf/src')
-rw-r--r-- | vendor/iio/libmergepdf/src/Driver/DefaultDriver.php | 25 | ||||
-rw-r--r-- | vendor/iio/libmergepdf/src/Driver/DriverInterface.php | 13 | ||||
-rw-r--r-- | vendor/iio/libmergepdf/src/Driver/Fpdi2Driver.php | 64 | ||||
-rw-r--r-- | vendor/iio/libmergepdf/src/Driver/TcpdiDriver.php | 52 | ||||
-rw-r--r-- | vendor/iio/libmergepdf/src/Exception.php | 7 | ||||
-rw-r--r-- | vendor/iio/libmergepdf/src/Merger.php | 79 | ||||
-rw-r--r-- | vendor/iio/libmergepdf/src/Pages.php | 67 | ||||
-rw-r--r-- | vendor/iio/libmergepdf/src/PagesInterface.php | 11 | ||||
-rw-r--r-- | vendor/iio/libmergepdf/src/Source/FileSource.php | 50 | ||||
-rw-r--r-- | vendor/iio/libmergepdf/src/Source/RawSource.php | 45 | ||||
-rw-r--r-- | vendor/iio/libmergepdf/src/Source/SourceInterface.php | 25 |
11 files changed, 438 insertions, 0 deletions
diff --git a/vendor/iio/libmergepdf/src/Driver/DefaultDriver.php b/vendor/iio/libmergepdf/src/Driver/DefaultDriver.php new file mode 100644 index 0000000..9ad6f73 --- /dev/null +++ b/vendor/iio/libmergepdf/src/Driver/DefaultDriver.php @@ -0,0 +1,25 @@ +<?php + +declare(strict_types = 1); + +namespace iio\libmergepdf\Driver; + +use iio\libmergepdf\Source\SourceInterface; + +final class DefaultDriver implements DriverInterface +{ + /** + * @var DriverInterface + */ + private $wrapped; + + public function __construct(DriverInterface $wrapped = null) + { + $this->wrapped = $wrapped ?: new Fpdi2Driver; + } + + public function merge(SourceInterface ...$sources): string + { + return $this->wrapped->merge(...$sources); + } +} diff --git a/vendor/iio/libmergepdf/src/Driver/DriverInterface.php b/vendor/iio/libmergepdf/src/Driver/DriverInterface.php new file mode 100644 index 0000000..39cca1e --- /dev/null +++ b/vendor/iio/libmergepdf/src/Driver/DriverInterface.php @@ -0,0 +1,13 @@ +<?php + +namespace iio\libmergepdf\Driver; + +use iio\libmergepdf\Source\SourceInterface; + +interface DriverInterface +{ + /** + * Merge multiple sources + */ + public function merge(SourceInterface ...$sources): string; +} diff --git a/vendor/iio/libmergepdf/src/Driver/Fpdi2Driver.php b/vendor/iio/libmergepdf/src/Driver/Fpdi2Driver.php new file mode 100644 index 0000000..e56d8b1 --- /dev/null +++ b/vendor/iio/libmergepdf/src/Driver/Fpdi2Driver.php @@ -0,0 +1,64 @@ +<?php + +declare(strict_types = 1); + +namespace iio\libmergepdf\Driver; + +use iio\libmergepdf\Exception; +use iio\libmergepdf\Source\SourceInterface; +use setasign\Fpdi\Fpdi as FpdiFpdf; +use setasign\Fpdi\Tcpdf\Fpdi as FpdiTcpdf; +use setasign\Fpdi\PdfParser\StreamReader; + +final class Fpdi2Driver implements DriverInterface +{ + /** + * @var FpdiFpdf|FpdiTcpdf + */ + private $fpdi; + + /** + * @param FpdiFpdf|FpdiTcpdf $fpdi + */ + public function __construct($fpdi = null) + { + // Tcpdf generates warnings due to argument ordering with php 8 + // suppressing errors is a dirty hack until tcpdf is patched + $this->fpdi = $fpdi ?: @new FpdiTcpdf; + + if (!($this->fpdi instanceof FpdiFpdf) && !($this->fpdi instanceof FpdiTcpdf)) { + throw new \InvalidArgumentException('Constructor argument must be an FPDI instance.'); + } + } + + public function merge(SourceInterface ...$sources): string + { + $sourceName = ''; + + try { + $fpdi = clone $this->fpdi; + + foreach ($sources as $source) { + $sourceName = $source->getName(); + $pageCount = $fpdi->setSourceFile(StreamReader::createByString($source->getContents())); + $pageNumbers = $source->getPages()->getPageNumbers() ?: range(1, $pageCount); + + foreach ($pageNumbers as $pageNr) { + $template = $fpdi->importPage($pageNr); + $size = $fpdi->getTemplateSize($template); + $fpdi->SetPrintHeader(false); + $fpdi->SetPrintFooter(false); + $fpdi->AddPage( + $size['width'] > $size['height'] ? 'L' : 'P', + [$size['width'], $size['height']] + ); + $fpdi->useTemplate($template); + } + } + + return $fpdi->Output('', 'S'); + } catch (\Exception $e) { + throw new Exception("'{$e->getMessage()}' in '$sourceName'", 0, $e); + } + } +} diff --git a/vendor/iio/libmergepdf/src/Driver/TcpdiDriver.php b/vendor/iio/libmergepdf/src/Driver/TcpdiDriver.php new file mode 100644 index 0000000..fa0ddc8 --- /dev/null +++ b/vendor/iio/libmergepdf/src/Driver/TcpdiDriver.php @@ -0,0 +1,52 @@ +<?php + +declare(strict_types = 1); + +namespace iio\libmergepdf\Driver; + +use iio\libmergepdf\Exception; +use iio\libmergepdf\Source\SourceInterface; + +final class TcpdiDriver implements DriverInterface +{ + /** + * @var \TCPDI + */ + private $tcpdi; + + public function __construct(\TCPDI $tcpdi = null) + { + $this->tcpdi = $tcpdi ?: new \TCPDI; + } + + public function merge(SourceInterface ...$sources): string + { + $sourceName = ''; + + try { + $tcpdi = clone $this->tcpdi; + + foreach ($sources as $source) { + $sourceName = $source->getName(); + $pageCount = $tcpdi->setSourceData($source->getContents()); + $pageNumbers = $source->getPages()->getPageNumbers() ?: range(1, $pageCount); + + foreach ($pageNumbers as $pageNr) { + $template = $tcpdi->importPage($pageNr); + $size = $tcpdi->getTemplateSize($template); + $tcpdi->SetPrintHeader(false); + $tcpdi->SetPrintFooter(false); + $tcpdi->AddPage( + $size['w'] > $size['h'] ? 'L' : 'P', + [$size['w'], $size['h']] + ); + $tcpdi->useTemplate($template); + } + } + + return $tcpdi->Output('', 'S'); + } catch (\Exception $e) { + throw new Exception("'{$e->getMessage()}' in '$sourceName'", 0, $e); + } + } +} diff --git a/vendor/iio/libmergepdf/src/Exception.php b/vendor/iio/libmergepdf/src/Exception.php new file mode 100644 index 0000000..69b3208 --- /dev/null +++ b/vendor/iio/libmergepdf/src/Exception.php @@ -0,0 +1,7 @@ +<?php + +namespace iio\libmergepdf; + +final class Exception extends \Exception +{ +} diff --git a/vendor/iio/libmergepdf/src/Merger.php b/vendor/iio/libmergepdf/src/Merger.php new file mode 100644 index 0000000..57725c9 --- /dev/null +++ b/vendor/iio/libmergepdf/src/Merger.php @@ -0,0 +1,79 @@ +<?php + +declare(strict_types = 1); + +namespace iio\libmergepdf; + +use iio\libmergepdf\Driver\DriverInterface; +use iio\libmergepdf\Driver\DefaultDriver; +use iio\libmergepdf\Source\SourceInterface; +use iio\libmergepdf\Source\FileSource; +use iio\libmergepdf\Source\RawSource; + +/** + * Merge existing pdfs into one + * + * Note that your PDFs are merged in the order that you add them + */ +final class Merger +{ + /** + * @var SourceInterface[] List of pdf sources to merge + */ + private $sources = []; + + /** + * @var DriverInterface + */ + private $driver; + + public function __construct(DriverInterface $driver = null) + { + $this->driver = $driver ?: new DefaultDriver; + } + + /** + * Add raw PDF from string + */ + public function addRaw(string $content, PagesInterface $pages = null): void + { + $this->sources[] = new RawSource($content, $pages); + } + + /** + * Add PDF from file + */ + public function addFile(string $filename, PagesInterface $pages = null): void + { + $this->sources[] = new FileSource($filename, $pages); + } + + /** + * Add files using iterator + * + * @param iterable<string> $iterator Set of filenames to add + * @param PagesInterface $pages Optional pages constraint used for every added pdf + */ + public function addIterator(iterable $iterator, PagesInterface $pages = null): void + { + foreach ($iterator as $filename) { + $this->addFile($filename, $pages); + } + } + + /** + * Merges loaded PDFs + */ + public function merge(): string + { + return $this->driver->merge(...$this->sources); + } + + /** + * Reset internal state + */ + public function reset(): void + { + $this->sources = []; + } +} diff --git a/vendor/iio/libmergepdf/src/Pages.php b/vendor/iio/libmergepdf/src/Pages.php new file mode 100644 index 0000000..7675315 --- /dev/null +++ b/vendor/iio/libmergepdf/src/Pages.php @@ -0,0 +1,67 @@ +<?php + +declare(strict_types = 1); + +namespace iio\libmergepdf; + +/** + * Parse page numbers from string + */ +final class Pages implements PagesInterface +{ + /** + * @var int[] Added integer page numbers + */ + private $pages = []; + + /** + * Parse page numbers from expression string + * + * Pages should be formatted as 1,3,6 or 12-16 or combined. Note that pages + * are merged in the order that you provide them. If you put pages 12-14 + * before 1-5 then 12-14 will be placed first. + */ + public function __construct(string $expressionString = '') + { + $expressions = explode( + ',', + str_replace(' ', '', $expressionString) + ); + + foreach ($expressions as $expr) { + if (empty($expr)) { + continue; + } + if (ctype_digit($expr)) { + $this->addPage((int)$expr); + continue; + } + if (preg_match("/^(\d+)-(\d+)/", $expr, $matches)) { + $this->addRange((int)$matches[1], (int)$matches[2]); + continue; + } + throw new Exception("Invalid page number(s) for expression '$expr'"); + } + } + + /** + * Add a single page + */ + public function addPage(int $page): void + { + $this->pages[] = $page; + } + + /** + * Add a range of pages + */ + public function addRange(int $start, int $end): void + { + $this->pages = array_merge($this->pages, range($start, $end)); + } + + public function getPageNumbers(): array + { + return $this->pages; + } +} diff --git a/vendor/iio/libmergepdf/src/PagesInterface.php b/vendor/iio/libmergepdf/src/PagesInterface.php new file mode 100644 index 0000000..e85d3c8 --- /dev/null +++ b/vendor/iio/libmergepdf/src/PagesInterface.php @@ -0,0 +1,11 @@ +<?php + +namespace iio\libmergepdf; + +interface PagesInterface +{ + /** + * @return int[] + */ + public function getPageNumbers(): array; +} diff --git a/vendor/iio/libmergepdf/src/Source/FileSource.php b/vendor/iio/libmergepdf/src/Source/FileSource.php new file mode 100644 index 0000000..2802d80 --- /dev/null +++ b/vendor/iio/libmergepdf/src/Source/FileSource.php @@ -0,0 +1,50 @@ +<?php + +declare(strict_types = 1); + +namespace iio\libmergepdf\Source; + +use iio\libmergepdf\PagesInterface; +use iio\libmergepdf\Pages; +use iio\libmergepdf\Exception; + +/** + * Pdf source from file + */ +final class FileSource implements SourceInterface +{ + /** + * @var string + */ + private $filename; + + /** + * @var PagesInterface + */ + private $pages; + + public function __construct(string $filename, PagesInterface $pages = null) + { + if (!is_file($filename) || !is_readable($filename)) { + throw new Exception("Invalid file '$filename'"); + } + + $this->filename = $filename; + $this->pages = $pages ?: new Pages; + } + + public function getName(): string + { + return $this->filename; + } + + public function getContents(): string + { + return (string)file_get_contents($this->filename); + } + + public function getPages(): PagesInterface + { + return $this->pages; + } +} diff --git a/vendor/iio/libmergepdf/src/Source/RawSource.php b/vendor/iio/libmergepdf/src/Source/RawSource.php new file mode 100644 index 0000000..7966918 --- /dev/null +++ b/vendor/iio/libmergepdf/src/Source/RawSource.php @@ -0,0 +1,45 @@ +<?php + +declare(strict_types = 1); + +namespace iio\libmergepdf\Source; + +use iio\libmergepdf\PagesInterface; +use iio\libmergepdf\Pages; + +/** + * Pdf source from raw string + */ +final class RawSource implements SourceInterface +{ + /** + * @var string + */ + private $contents; + + /** + * @var PagesInterface + */ + private $pages; + + public function __construct(string $contents, PagesInterface $pages = null) + { + $this->contents = $contents; + $this->pages = $pages ?: new Pages; + } + + public function getName(): string + { + return "raw-content"; + } + + public function getContents(): string + { + return $this->contents; + } + + public function getPages(): PagesInterface + { + return $this->pages; + } +} diff --git a/vendor/iio/libmergepdf/src/Source/SourceInterface.php b/vendor/iio/libmergepdf/src/Source/SourceInterface.php new file mode 100644 index 0000000..4ce9c3c --- /dev/null +++ b/vendor/iio/libmergepdf/src/Source/SourceInterface.php @@ -0,0 +1,25 @@ +<?php + +declare(strict_types = 1); + +namespace iio\libmergepdf\Source; + +use iio\libmergepdf\PagesInterface; + +interface SourceInterface +{ + /** + * Get name of file or source + */ + public function getName(): string; + + /** + * Get pdf content + */ + public function getContents(): string; + + /** + * Get pages to fetch from source + */ + public function getPages(): PagesInterface; +} |