4
4
5
5
namespace KNPLabs \Snappy \Backend \WkHtmlToPdf ;
6
6
7
- use KNPLabs \Snappy \Core \ Backend \Adapter ;
7
+ use KNPLabs \Snappy \Backend \WkHtmlToPdf \ ExtraOption \ Orientation ;
8
8
use KNPLabs \Snappy \Core \Backend \Adapter \HtmlFileToPdf ;
9
9
use KNPLabs \Snappy \Core \Backend \Adapter \Reconfigurable ;
10
+ use KNPLabs \Snappy \Core \Backend \Adapter \UriToPdf ;
10
11
use KNPLabs \Snappy \Core \Backend \Options ;
12
+ use KNPLabs \Snappy \Core \Backend \Options \PageOrientation ;
13
+ use KNPLabs \Snappy \Core \Stream \FileStream ;
14
+ use Psr \Http \Message \StreamFactoryInterface ;
11
15
use Psr \Http \Message \StreamInterface ;
16
+ use Psr \Http \Message \UriFactoryInterface ;
17
+ use Psr \Http \Message \UriInterface ;
18
+ use Symfony \Component \Process \Process ;
12
19
use SplFileInfo ;
13
20
14
- final class WkHtmlToPdfAdapter implements HtmlFileToPdf
21
+ final class WkHtmlToPdfAdapter implements HtmlFileToPdf, UriToPdf
15
22
{
16
23
/**
17
24
* @use Reconfigurable<self>
@@ -26,14 +33,70 @@ public function __construct(
26
33
private string $ binary ,
27
34
private int $ timeout ,
28
35
WkHtmlToPdfFactory $ factory ,
29
- Options $ options
36
+ Options $ options ,
37
+ private readonly StreamFactoryInterface $ streamFactory ,
38
+ private readonly UriFactoryInterface $ uriFactory ,
30
39
) {
31
40
$ this ->factory = $ factory ;
41
+
42
+ foreach ($ options ->extraOptions as $ extraOption ) {
43
+ if (!$ extraOption instanceof ExtraOption) {
44
+ throw new \InvalidArgumentException ("Invalid option type " );
45
+ }
46
+ }
47
+
32
48
$ this ->options = $ options ;
33
49
}
34
50
35
51
public function generateFromHtmlFile (SplFileInfo $ file ): StreamInterface
36
52
{
37
- throw new \Exception ("Not implemented for {$ this ->binary } with timeout {$ this ->timeout }. " );
53
+ $ filepath = $ file ->getRealPath ();
54
+
55
+ if ($ filepath === false ) {
56
+ throw new \RuntimeException ("File not found: {$ file ->getPathname ()}. " );
57
+ }
58
+
59
+ return $ this ->generateFromUri (
60
+ $ this ->uriFactory ->createUri ($ filepath )->withScheme ('file ' )
61
+ );
62
+ }
63
+
64
+ public function generateFromUri (UriInterface $ uri ): StreamInterface
65
+ {
66
+ $ outputStream = FileStream::createTmpFile ($ this ->streamFactory );
67
+
68
+ $ process = new Process (
69
+ command: [
70
+ $ this ->binary ,
71
+ ...$ this ->compileOptions (),
72
+ $ uri ->toString (),
73
+ $ outputStream ->file ->getPathname (),
74
+ ],
75
+ timeout: $ this ->timeout ,
76
+ );
77
+
78
+ return $ outputStream ;
79
+ }
80
+
81
+ /**
82
+ * @return array<string|int|float>
83
+ */
84
+ private function compileOptions (): array
85
+ {
86
+ return array_reduce (
87
+ $ this ->options ->extraOptions ,
88
+ fn (array $ carry , ExtraOption $ extraOption ) =>
89
+ $ this ->options ->pageOrientation !== null && $ extraOption instanceof Orientation
90
+ ? [
91
+ ...$ carry ,
92
+ ...(new Orientation ($ this ->options ->pageOrientation ->value ))->compile (),
93
+ ]
94
+ : [
95
+ ...$ carry ,
96
+ ...$ extraOption ->compile (),
97
+ ]
98
+ ,
99
+ [],
100
+ );
38
101
}
39
102
}
0 commit comments