Repository navigation
Expand file tree
/
Copy pathWebkitDriver.php
More file actions
99 lines (80 loc) · 2.28 KB
/
Copy pathWebkitDriver.php
File metadata and controls
99 lines (80 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
namespace AsyncPHP\Paper\Driver;
use AsyncPHP\Paper\Driver;
use AsyncPHP\Paper\Promise;
use AsyncPHP\Paper\Runner;
use StdClass;
final class WebkitDriver extends BaseDriver implements Driver
{
/**
* @var string
*/
private $binaryPath;
/**
* @var string
*/
private $tempPath;
/**
* @var array
*/
private $options;
/**
* @param string $binaryPath
* @param string $tempPath
* @param array $options
*/
public function __construct($binaryPath, $tempPath, array $options = [])
{
$this->binaryPath = $binaryPath;
$this->tempPath = $tempPath;
$this->options = $options;
}
/**
* @inheritdoc
*
* @param Runner $runner
*
* @return mixed
*/
public function render(Runner $runner)
{
$data = $this->data();
$hash = md5(spl_object_hash(new StdClass) . $data["html"]);
$tempPath = rtrim($this->tempPath, "/");
$binary = $this->binaryPath;
$input = "{$tempPath}/{$hash}-body.html";
$output = "{$tempPath}/{$hash}.pdf";
$custom = $this->options;
return $runner->run(function() use ($data, $binary, $input, $output, $custom) {
file_put_contents($input, $data["html"]);
$orientation = "Portrait";
if ($data["orientation"] === "landscape") {
$orientation = "Landscape";
}
$options = "";
foreach ($custom as $key => $value) {
if (is_string($key)) {
$options .= " {$key} {$value}";
} else {
$options .= " {$value}";
}
}
exec("
{$binary} \
--page-size {$data["size"]} \
--orientation {$orientation} \
--dpi {$data["dpi"]} \
--disable-smart-shrinking \
--load-error-handling 'ignore' \
--load-media-error-handling 'ignore' \
{$options} \
{$input} {$output} \
> /dev/null 2> /dev/null
");
$contents = file_get_contents($output);
unlink($input);
unlink($output);
return $contents;
});
}
}