|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +/** |
| 5 | + * @section LICENSE |
| 6 | + * This file is created by vianetz <[email protected]>. |
| 7 | + * The code is distributed under the GPL license. |
| 8 | + * |
| 9 | + * If you did not receive a copy of the license and are unable to |
| 10 | + * obtain it through the world-wide-web, please send an email |
| 11 | + * to [email protected] so we can send you a copy immediately. |
| 12 | + * |
| 13 | + * @package Vianetz\Pdf |
| 14 | + * @author Christoph Massmann, <[email protected]> |
| 15 | + * @link https://www.vianetz.com |
| 16 | + * @copyright Copyright (c) since 2006 vianetz - Dipl.-Ing. C. Massmann (https://www.vianetz.com) |
| 17 | + * @license http://www.gnu.org/licenses/gpl-3.0.txt GNU GENERAL PUBLIC LICENSE |
| 18 | + */ |
| 19 | + |
| 20 | +namespace Vianetz\Pdf\Model\Merger; |
| 21 | + |
| 22 | +use setasign\Fpdi\Fpdi as FpdfFpdi; |
| 23 | +use setasign\Fpdi\PdfParser\StreamReader; |
| 24 | +use Vianetz\Pdf\Model\Config; |
| 25 | + |
| 26 | +class Fpdf extends AbstractMerger |
| 27 | +{ |
| 28 | + private const OUTPUT_MODE_STRING = 'S'; |
| 29 | + private const OUTPUT_FORMAT_LANDSCAPE = 'L'; |
| 30 | + private const OUTPUT_FORMAT_PORTRAIT = 'P'; |
| 31 | + protected FpdfFpdi $fpdiModel; |
| 32 | + private string $orientation = self::OUTPUT_FORMAT_PORTRAIT; |
| 33 | + private string $paper; |
| 34 | + |
| 35 | + public function __construct(?Config $config = null, ?FpdfFpdi $fpdiModel = null) |
| 36 | + { |
| 37 | + $config ??= new Config(); |
| 38 | + |
| 39 | + if ($config->getPdfOrientation() === Config::PAPER_ORIENTATION_PORTRAIT) { |
| 40 | + $this->orientation = self::OUTPUT_FORMAT_PORTRAIT; |
| 41 | + } elseif ($config->getPdfOrientation() === Config::PAPER_ORIENTATION_LANDSCAPE) { |
| 42 | + $this->orientation = self::OUTPUT_FORMAT_LANDSCAPE; |
| 43 | + } |
| 44 | + |
| 45 | + $this->paper = $config->getPdfSize(); |
| 46 | + |
| 47 | + $this->fpdiModel = $fpdiModel ?? new FpdfFpdi($this->orientation, 'mm', $this->paper); |
| 48 | + |
| 49 | + $this->fpdiModel->SetAuthor($config->getPdfAuthor()); |
| 50 | + $this->fpdiModel->SetTitle($config->getPdfTitle()); |
| 51 | + $this->fpdiModel->setCreator('https://github.com/vianetz/pdf-generator'); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Import the specified page number from the given file into the current pdf model. |
| 56 | + * |
| 57 | + * @param string|resource|StreamReader $file |
| 58 | + * |
| 59 | + * @throws \setasign\Fpdi\PdfParser\PdfParserException|\setasign\Fpdi\PdfReader\PdfReaderException |
| 60 | + */ |
| 61 | + public function importPageFromFile($file, int $pageNumber): void |
| 62 | + { |
| 63 | + $this->fpdiModel->setSourceFile($file); |
| 64 | + $pageId = $this->fpdiModel->importPage($pageNumber); |
| 65 | + $this->fpdiModel->useTemplate($pageId); |
| 66 | + } |
| 67 | + |
| 68 | + public function importPageFromPdfString(string $pdfString, int $pageNumber): void |
| 69 | + { |
| 70 | + $this->importPageFromFile($this->createPdfStream($pdfString), $pageNumber); |
| 71 | + } |
| 72 | + |
| 73 | + public function toPdf(): string |
| 74 | + { |
| 75 | + return $this->fpdiModel->Output('', self::OUTPUT_MODE_STRING); |
| 76 | + } |
| 77 | + |
| 78 | + public function countPages(string $pdfString): int |
| 79 | + { |
| 80 | + return $this->fpdiModel->setSourceFile($this->createPdfStream($pdfString)); |
| 81 | + } |
| 82 | + |
| 83 | + public function addPage(): self |
| 84 | + { |
| 85 | + $this->fpdiModel->addPage($this->orientation, $this->paper); |
| 86 | + |
| 87 | + return $this; |
| 88 | + } |
| 89 | + |
| 90 | + public function addAttachment(string $fileName): self |
| 91 | + { |
| 92 | + throw new \RuntimeException('not implemented'); |
| 93 | + } |
| 94 | + |
| 95 | + private function createPdfStream(string $pdfString): StreamReader |
| 96 | + { |
| 97 | + return StreamReader::createByString($pdfString); |
| 98 | + } |
| 99 | +} |
0 commit comments