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(), + ] + , + [], + ); } }