Skip to content

Commit

Permalink
feat: implement wkhtmltopdf backend
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpozzi committed Oct 16, 2024
1 parent 3ed9af8 commit 9f7adcf
Show file tree
Hide file tree
Showing 10 changed files with 190 additions and 4 deletions.
10 changes: 10 additions & 0 deletions src/Backend/WkHtmlToPdf/ExtraOption.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace KNPLabs\Snappy\Backend\WkHtmlToPdf;

interface ExtraOption {
/** @return array<string|int|float> */
public function compile(): array;
}
17 changes: 17 additions & 0 deletions src/Backend/WkHtmlToPdf/ExtraOption/CollateOption.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace KNPLabs\Snappy\Backend\WkHtmlToPdf\ExtraOption;

use KNPLabs\Snappy\Backend\WkHtmlToPdf\ExtraOption;

final class CollateOption implements ExtraOption
{
public function __construct() {}

public function compile(): array
{
return ['--collate'];
}
}
17 changes: 17 additions & 0 deletions src/Backend/WkHtmlToPdf/ExtraOption/CookieJarOption.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace KNPLabs\Snappy\Backend\WkHtmlToPdf\ExtraOption;

use KNPLabs\Snappy\Backend\WkHtmlToPdf\ExtraOption;

final class CookieJarOption implements ExtraOption
{
public function __construct(public readonly string $path) {}

public function compile(): array
{
return ['--no-collate'];
}
}
17 changes: 17 additions & 0 deletions src/Backend/WkHtmlToPdf/ExtraOption/CopiesOption.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace KNPLabs\Snappy\Backend\WkHtmlToPdf\ExtraOption;

use KNPLabs\Snappy\Backend\WkHtmlToPdf\ExtraOption;

final class CopiesOption implements ExtraOption
{
public function __construct(private readonly int $number) {}

public function compile(): array
{
return ['--copies', $this->number];
}
}
17 changes: 17 additions & 0 deletions src/Backend/WkHtmlToPdf/ExtraOption/DpiOption.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace KNPLabs\Snappy\Backend\WkHtmlToPdf\ExtraOption;

use KNPLabs\Snappy\Backend\WkHtmlToPdf\ExtraOption;

final class DpiOptions implements ExtraOption
{
public function __construct(private readonly int $dpi) {}

public function compile(): array
{
return ['--dpi', $this->dpi];
}
}
15 changes: 15 additions & 0 deletions src/Backend/WkHtmlToPdf/ExtraOption/GrayscaleOption.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace KNPLabs\Snappy\Backend\WkHtmlToPdf\ExtraOption;

final class GrayscaleOption
{
public function __construct() {}

public function compile(): array
{
return ['--grayscale'];
}
}
10 changes: 10 additions & 0 deletions src/Backend/WkHtmlToPdf/ExtraOption/ImageDpiOption.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace KNPLabs\Snappy\Backend\WkHtmlToPdf\ExtraOption;

final class ImageDpiOption
{
public function __construct(public readonly int $dpi) {}
}
10 changes: 10 additions & 0 deletions src/Backend/WkHtmlToPdf/ExtraOption/NoCollateOption.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace KNPLabs\Snappy\Backend\WkHtmlToPdf\ExtraOption;

final class NoCollateOption
{
public function __construct() {}
}
10 changes: 10 additions & 0 deletions src/Backend/WkHtmlToPdf/ExtraOption/Orientation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace KNPLabs\Snappy\Backend\WkHtmlToPdf\ExtraOption;

final class Orientation
{
public function __construct() {}
}
71 changes: 67 additions & 4 deletions src/Backend/WkHtmlToPdf/WkHtmlToPdfAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,21 @@

namespace KNPLabs\Snappy\Backend\WkHtmlToPdf;

use KNPLabs\Snappy\Core\Backend\Adapter;
use KNPLabs\Snappy\Backend\WkHtmlToPdf\ExtraOption\Orientation;
use KNPLabs\Snappy\Core\Backend\Adapter\HtmlFileToPdf;
use KNPLabs\Snappy\Core\Backend\Adapter\Reconfigurable;
use KNPLabs\Snappy\Core\Backend\Adapter\UriToPdf;
use KNPLabs\Snappy\Core\Backend\Options;
use KNPLabs\Snappy\Core\Backend\Options\PageOrientation;
use KNPLabs\Snappy\Core\Stream\FileStream;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UriFactoryInterface;
use Psr\Http\Message\UriInterface;
use Symfony\Component\Process\Process;
use SplFileInfo;

final class WkHtmlToPdfAdapter implements HtmlFileToPdf
final class WkHtmlToPdfAdapter implements HtmlFileToPdf, UriToPdf
{
/**
* @use Reconfigurable<self>
Expand All @@ -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<string|int|float>
*/
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(),
]
,
[],
);
}
}

0 comments on commit 9f7adcf

Please sign in to comment.