Skip to content

Commit bb1559d

Browse files
authored
v2 unstable wkhtmltopdf options refactoring (#522)
* test: remove duplicated test case * refactor: redesign whhtmltopdf extra options and add tests
1 parent d3a8659 commit bb1559d

File tree

103 files changed

+1640
-1203
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

103 files changed

+1640
-1203
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
runs:
3+
using: 'composite'
4+
steps:
5+
- run: sudo apt-get update
6+
shell: bash
7+
- run: sudo apt-get install ghostscript --yes
8+
shell: bash
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
runs:
3+
using: 'composite'
4+
steps:
5+
- run: sudo apt-get update
6+
shell: bash
7+
- run: sudo apt-get install xvfb libfontconfig wkhtmltopdf --yes
8+
shell: bash

.github/workflows/tests.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ jobs:
6666
# with:
6767
# sparse-checkout: |
6868
# ${{ matrix.path }}
69-
- run: sudo apt-get update
70-
- run: sudo apt-get install ghostscript --yes
69+
- uses: ./.github/actions/install-ghostscript
70+
- uses: ./.github/actions/install-wkhtmltopdf
7171
- uses: shivammathur/setup-php@v2
7272
- run: composer install
7373
- run: vendor/bin/phpunit

src/Backend/Dompdf/Tests/DompdfAdapterTest.php

Lines changed: 0 additions & 150 deletions
This file was deleted.

src/Backend/WkHtmlToPdf/ExtraOption.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,26 @@
44

55
namespace KNPLabs\Snappy\Backend\WkHtmlToPdf;
66

7-
interface ExtraOption {
8-
public function isRepeatable(): bool;
7+
/**
8+
* @see https://wkhtmltopdf.org/usage/wkhtmltopdf.txt
9+
*/
10+
abstract class ExtraOption
11+
{
12+
/**
13+
* @param non-empty-array<string> $command
14+
*/
15+
public function __construct(private bool $repeatable, private array $command) {}
916

10-
/** @return array<string|int|float> */
11-
public function compile(): array;
17+
final public function isRepeatable(): bool
18+
{
19+
return $this->repeatable;
20+
}
21+
22+
/**
23+
* @return non-empty-array<string>
24+
*/
25+
final public function getCommand(): array
26+
{
27+
return $this->command;
28+
}
1229
}

src/Backend/WkHtmlToPdf/ExtraOption/Allow.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@
66

77
use KNPLabs\Snappy\Backend\WkHtmlToPdf\ExtraOption;
88

9-
class Allow implements ExtraOption
9+
/**
10+
* Allow the file or files from the specified folder to be loaded.
11+
*/
12+
final class Allow extends ExtraOption
1013
{
11-
public function __construct(private readonly string $path)
14+
/**
15+
* @param non-empty-string $path
16+
*/
17+
public function __construct(string $path)
1218
{
19+
parent::__construct(
20+
repeatable: true,
21+
command: ['--allow', $path]
22+
);
1323
}
14-
15-
public function isRepeatable(): bool
16-
{
17-
return true;
18-
}
19-
20-
public function compile(): array
21-
{
22-
return ['--allow', $this->path];
23-
}
24-
}
24+
}

src/Backend/WkHtmlToPdf/ExtraOption/BypassProxyFor.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@
66

77
use KNPLabs\Snappy\Backend\WkHtmlToPdf\ExtraOption;
88

9-
class BypassProxyFor implements ExtraOption
9+
/**
10+
* Bypass proxy for host.
11+
*/
12+
final class BypassProxyFor extends ExtraOption
1013
{
11-
public function __construct(private readonly string $value)
14+
/**
15+
* @param non-empty-string $value
16+
*/
17+
public function __construct(string $value)
1218
{
19+
parent::__construct(
20+
repeatable: true,
21+
command: ['--bypass-proxy-for', $value],
22+
);
1323
}
14-
15-
public function isRepeatable(): bool
16-
{
17-
return true;
18-
}
19-
20-
public function compile(): array
21-
{
22-
return ['--bypass-proxy-for', $this->value];
23-
}
24-
}
24+
}

src/Backend/WkHtmlToPdf/ExtraOption/CacheDir.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@
66

77
use KNPLabs\Snappy\Backend\WkHtmlToPdf\ExtraOption;
88

9-
class CacheDir implements ExtraOption
9+
/**
10+
* Web cache directory.
11+
*/
12+
final class CacheDir extends ExtraOption
1013
{
11-
public function __construct(private readonly string $path)
14+
/**
15+
* @param non-empty-string $path
16+
*/
17+
public function __construct(string $path)
1218
{
19+
parent::__construct(
20+
repeatable: false,
21+
command: ['--cache-dir', $path],
22+
);
1323
}
14-
15-
public function isRepeatable(): bool
16-
{
17-
return false;
18-
}
19-
20-
public function compile(): array
21-
{
22-
return ['--cache-dir', $this->path];
23-
}
24-
}
24+
}

src/Backend/WkHtmlToPdf/ExtraOption/CheckBoxSvg.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@
66

77
use KNPLabs\Snappy\Backend\WkHtmlToPdf\ExtraOption;
88

9-
class CheckBoxSvg implements ExtraOption
9+
/**
10+
* Use this SVG file when rendering unchecked checkboxes.
11+
*/
12+
final class CheckBoxSvg extends ExtraOption
1013
{
11-
public function __construct(private readonly string $path)
14+
/**
15+
* @param non-empty-string $path
16+
*/
17+
public function __construct(string $path)
1218
{
19+
parent::__construct(
20+
repeatable: false,
21+
command: ['--checkbox-svg', $path],
22+
);
1323
}
14-
15-
public function isRepeatable(): bool
16-
{
17-
return false;
18-
}
19-
20-
public function compile(): array
21-
{
22-
return ['--checkbox-svg', $this->path];
23-
}
24-
}
24+
}

src/Backend/WkHtmlToPdf/ExtraOption/CheckboxCheckedSvg.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@
66

77
use KNPLabs\Snappy\Backend\WkHtmlToPdf\ExtraOption;
88

9-
class CheckboxCheckedSvg implements ExtraOption
9+
/**
10+
* Use this SVG file when rendering checked checkboxes.
11+
*/
12+
final class CheckboxCheckedSvg extends ExtraOption
1013
{
11-
public function __construct(private readonly string $path)
14+
/**
15+
* @param non-empty-string $path
16+
*/
17+
public function __construct(string $path)
1218
{
19+
parent::__construct(
20+
repeatable: false,
21+
command: ['--checkbox-checked-svg', $path],
22+
);
1323
}
14-
15-
public function isRepeatable(): bool
16-
{
17-
return false;
18-
}
19-
20-
public function compile(): array
21-
{
22-
return ['--checkbox-checked-svg', $this->path];
23-
}
24-
}
24+
}

0 commit comments

Comments
 (0)