-
Notifications
You must be signed in to change notification settings - Fork 148
/
Copy pathexample69_custom_services.php
90 lines (65 loc) · 2.49 KB
/
example69_custom_services.php
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
<?php
// require composer autoload
require_once __DIR__ . '/bootstrap.php';
use Mpdf\Http\Response;
use Mpdf\Http\Stream;
use Psr\Http\Message\RequestInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
class CustomHttpClient implements \Mpdf\Http\ClientInterface {
public function sendRequest(RequestInterface $request)
{
$response = new Response();
$stream = Stream::create(file_get_contents(__DIR__ . '/assets/tiger.jpg'));
$stream->rewind();
return $response
->withStatus(200)
->withBody($stream);
}
};
class CustomContentLoader implements \Mpdf\File\LocalContentLoaderInterface, \Psr\Log\LoggerAwareInterface
{
private $logger;
public function __construct(LoggerInterface $logger = null)
{
$this->logger = $logger ?: new NullLogger();
}
public function load($path)
{
if (!preg_match('/alpha[0-9]{2}/', $path)) {
$this->logger->info(sprintf('Custom local content loader ignoring path "%s"', $path));
return base64_decode('R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7');
}
return file_get_contents($path);
}
public function setLogger(LoggerInterface $logger)
{
$this->logger = $logger;
}
}
class StdErrLogger extends \Psr\Log\AbstractLogger {
public function log($level, $message, array $context = [])
{
// fwrite(STDERR, $level . ': ' . $message . "\n");
}
}
$client = new CustomHttpClient();
$loader = new CustomContentLoader();
$mpdf = new \Mpdf\Mpdf([], new \Mpdf\Container\SimpleContainer([
'httpClient' => $client,
'localContentLoader' => $loader,
]));
$mpdf->setLogger(new StdErrLogger());
$mpdf->WriteHtml('
<p>Custom implementation of internal HTTP client will return the same image of a tiger to all HTTP requests</p>
<img width="256" src="https://upload.wikimedia.org/wikipedia/commons/2/2f/Neisse_bei_skerbersdorf_640x480.jpg"><br>
<img width="256" src="https://upload.wikimedia.org/wikipedia/commons/thumb/9/9c/14-05-04-pramen-nysa-RalfR-DSC_1050-017.jpg/640px-14-05-04-pramen-nysa-RalfR-DSC_1050-017.jpg"><br>
<img width="256" src="https://upload.wikimedia.org/wikipedia/commons/e/e2/Nei%C3%9Fem%C3%BCndung.JPG">
<pagebreak>
<p>Custom implementation of local files loader will ignore (return an empty 1px GIF file) all files not matching regex pattern alpha[0-9]{2}</p>
<img width="256" height="256" src="assets/alpha.png"><br>
<img width="256" height="256" src="assets/alpha3.png"><br>
<img width="256" height="256" src="assets/alpha09.png"><br>
<img width="256" height="256" src="assets/alpha36.png">
');
$mpdf->Output();