From 0ff39c83d38ce538a9f5dba53eca0fa9cb16d9e6 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 28 Apr 2024 14:45:49 +0200 Subject: Adding upstream version 0.10.2+dfsg1. Signed-off-by: Daniel Baumann --- .../iio/libmergepdf/src/Driver/DefaultDriver.php | 25 +++++++ .../iio/libmergepdf/src/Driver/DriverInterface.php | 13 ++++ vendor/iio/libmergepdf/src/Driver/Fpdi2Driver.php | 64 ++++++++++++++++++ vendor/iio/libmergepdf/src/Driver/TcpdiDriver.php | 52 ++++++++++++++ vendor/iio/libmergepdf/src/Exception.php | 7 ++ vendor/iio/libmergepdf/src/Merger.php | 79 ++++++++++++++++++++++ vendor/iio/libmergepdf/src/Pages.php | 67 ++++++++++++++++++ vendor/iio/libmergepdf/src/PagesInterface.php | 11 +++ vendor/iio/libmergepdf/src/Source/FileSource.php | 50 ++++++++++++++ vendor/iio/libmergepdf/src/Source/RawSource.php | 45 ++++++++++++ .../iio/libmergepdf/src/Source/SourceInterface.php | 25 +++++++ 11 files changed, 438 insertions(+) create mode 100644 vendor/iio/libmergepdf/src/Driver/DefaultDriver.php create mode 100644 vendor/iio/libmergepdf/src/Driver/DriverInterface.php create mode 100644 vendor/iio/libmergepdf/src/Driver/Fpdi2Driver.php create mode 100644 vendor/iio/libmergepdf/src/Driver/TcpdiDriver.php create mode 100644 vendor/iio/libmergepdf/src/Exception.php create mode 100644 vendor/iio/libmergepdf/src/Merger.php create mode 100644 vendor/iio/libmergepdf/src/Pages.php create mode 100644 vendor/iio/libmergepdf/src/PagesInterface.php create mode 100644 vendor/iio/libmergepdf/src/Source/FileSource.php create mode 100644 vendor/iio/libmergepdf/src/Source/RawSource.php create mode 100644 vendor/iio/libmergepdf/src/Source/SourceInterface.php (limited to 'vendor/iio/libmergepdf/src') 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 @@ +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 @@ +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 @@ +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 @@ +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 $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 @@ +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 @@ +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 @@ +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 @@ +