From 9f7adcf977c94cfeebd308a852f9d4404f2b99cc Mon Sep 17 00:00:00 2001 From: alexpozzi Date: Wed, 16 Oct 2024 14:39:25 +0200 Subject: [PATCH] feat: implement wkhtmltopdf backend --- src/Backend/WkHtmlToPdf/ExtraOption.php | 10 +++ .../WkHtmlToPdf/ExtraOption/CollateOption.php | 17 +++++ .../ExtraOption/CookieJarOption.php | 17 +++++ .../WkHtmlToPdf/ExtraOption/CopiesOption.php | 17 +++++ .../WkHtmlToPdf/ExtraOption/DpiOption.php | 17 +++++ .../ExtraOption/GrayscaleOption.php | 15 ++++ .../ExtraOption/ImageDpiOption.php | 10 +++ .../ExtraOption/NoCollateOption.php | 10 +++ .../WkHtmlToPdf/ExtraOption/Orientation.php | 10 +++ .../WkHtmlToPdf/WkHtmlToPdfAdapter.php | 71 +++++++++++++++++-- 10 files changed, 190 insertions(+), 4 deletions(-) create mode 100644 src/Backend/WkHtmlToPdf/ExtraOption.php create mode 100644 src/Backend/WkHtmlToPdf/ExtraOption/CollateOption.php create mode 100644 src/Backend/WkHtmlToPdf/ExtraOption/CookieJarOption.php create mode 100644 src/Backend/WkHtmlToPdf/ExtraOption/CopiesOption.php create mode 100644 src/Backend/WkHtmlToPdf/ExtraOption/DpiOption.php create mode 100644 src/Backend/WkHtmlToPdf/ExtraOption/GrayscaleOption.php create mode 100644 src/Backend/WkHtmlToPdf/ExtraOption/ImageDpiOption.php create mode 100644 src/Backend/WkHtmlToPdf/ExtraOption/NoCollateOption.php create mode 100644 src/Backend/WkHtmlToPdf/ExtraOption/Orientation.php diff --git a/src/Backend/WkHtmlToPdf/ExtraOption.php b/src/Backend/WkHtmlToPdf/ExtraOption.php new file mode 100644 index 00000000..6e0f4931 --- /dev/null +++ b/src/Backend/WkHtmlToPdf/ExtraOption.php @@ -0,0 +1,10 @@ + */ + public function compile(): array; +} diff --git a/src/Backend/WkHtmlToPdf/ExtraOption/CollateOption.php b/src/Backend/WkHtmlToPdf/ExtraOption/CollateOption.php new file mode 100644 index 00000000..0b3f70c3 --- /dev/null +++ b/src/Backend/WkHtmlToPdf/ExtraOption/CollateOption.php @@ -0,0 +1,17 @@ +number]; + } +} diff --git a/src/Backend/WkHtmlToPdf/ExtraOption/DpiOption.php b/src/Backend/WkHtmlToPdf/ExtraOption/DpiOption.php new file mode 100644 index 00000000..65502d9f --- /dev/null +++ b/src/Backend/WkHtmlToPdf/ExtraOption/DpiOption.php @@ -0,0 +1,17 @@ +dpi]; + } +} diff --git a/src/Backend/WkHtmlToPdf/ExtraOption/GrayscaleOption.php b/src/Backend/WkHtmlToPdf/ExtraOption/GrayscaleOption.php new file mode 100644 index 00000000..21ed0645 --- /dev/null +++ b/src/Backend/WkHtmlToPdf/ExtraOption/GrayscaleOption.php @@ -0,0 +1,15 @@ + @@ -26,14 +33,70 @@ public function __construct( private string $binary, private int $timeout, WkHtmlToPdfFactory $factory, - Options $options + Options $options, + private readonly StreamFactoryInterface $streamFactory, + private readonly UriFactoryInterface $uriFactory, ) { $this->factory = $factory; + + foreach ($options->extraOptions as $extraOption) { + if (!$extraOption instanceof ExtraOption) { + throw new \InvalidArgumentException("Invalid option type"); + } + } + $this->options = $options; } public function generateFromHtmlFile(SplFileInfo $file): StreamInterface { - throw new \Exception("Not implemented for {$this->binary} with timeout {$this->timeout}."); + $filepath = $file->getRealPath(); + + if ($filepath === false) { + throw new \RuntimeException("File not found: {$file->getPathname()}."); + } + + return $this->generateFromUri( + $this->uriFactory->createUri($filepath)->withScheme('file') + ); + } + + public function generateFromUri(UriInterface $uri): StreamInterface + { + $outputStream = FileStream::createTmpFile($this->streamFactory); + + $process = new Process( + command: [ + $this->binary, + ...$this->compileOptions(), + $uri->toString(), + $outputStream->file->getPathname(), + ], + timeout: $this->timeout, + ); + + return $outputStream; + } + + /** + * @return array + */ + private function compileOptions(): array + { + return array_reduce( + $this->options->extraOptions, + fn (array $carry, ExtraOption $extraOption) => + $this->options->pageOrientation !== null && $extraOption instanceof Orientation + ? [ + ...$carry, + ...(new Orientation($this->options->pageOrientation->value))->compile(), + ] + : [ + ...$carry, + ...$extraOption->compile(), + ] + , + [], + ); } }