/
PdfResult.php
executable file
·130 lines (116 loc) · 4.06 KB
/
PdfResult.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
namespace Staempfli\Pdf\Model\View;
use Magento\Framework;
use Magento\Framework\App\Response\HttpInterface as HttpResponseInterface;
use Magento\Framework\App\ResponseInterface;
use Staempfli\Pdf\Api\Options;
use Staempfli\Pdf\Api\OptionsFactory;
use Staempfli\Pdf\Model\PdfFactory;
use Staempfli\Pdf\Model\PdfResponse;
use Staempfli\Pdf\Model\PdfResponseFactory;
/**
* To render a single layout (without cover page), you can return the result instance directly from the controller.
*
* For more advanced options, use renderSourceDocument() to get a source document you can pass into the Pdf service
*/
class PdfResult extends Framework\Controller\AbstractResult
{
const TYPE = 'pdf';
/** @var PdfResponseFactory */
private $pdfResponseFactory;
/** @var PdfFactory */
private $pdfFactory;
/** @var Options */
private $pdfGlobalOptions;
/** @var Options */
private $pdfPageOptions;
/** @var PageResultWithoutHttp */
private $pageResult;
/** @var string */
private $filename = null;
public function __construct(PdfResponseFactory $pdfResponseFactory, PdfFactory $pdfFactory,
OptionsFactory $pdfOptionsFactory, PageResultWithoutHttp $pageResult
) {
$this->pdfResponseFactory = $pdfResponseFactory;
$this->pdfFactory = $pdfFactory;
$this->pdfGlobalOptions = $pdfOptionsFactory->create();
$this->pdfPageOptions = $pdfOptionsFactory->create();
$this->pageResult = $pageResult;
}
/**
* Set filename for download. If null, Content-Disposition header is not sent,
* i.e. download will not be forced and PDF may be displayed in browser
*
* @param $filename
*/
public function setFilename($filename)
{
$this->filename = $filename;
}
public function addGlobalOptions(Options $options)
{
$this->pdfGlobalOptions = $this->pdfGlobalOptions->merge($options);
}
public function addPageOptions(Options $options)
{
$this->pdfPageOptions = $this->pdfPageOptions->merge($options);
}
/**
* Renders directly to HTTP response
*
* @param HttpResponseInterface $response
* @return $this
*/
protected function render(HttpResponseInterface $response)
{
$this->preparePdfResponse($response, $this->renderPdf());
return $this;
}
/**
* @return PdfResponse
*/
public function renderSourceDocument()
{
/** @var PdfResponse $pdfResponse */
$pdfResponse = $this->pdfResponseFactory->create([
PdfResponse::PARAM_OPTIONS => $this->pdfPageOptions
]);
/*
* As of Magento 2.1, addDefaultHandle() must be called after instantiating a layout result,
* see \Magento\Framework\Controller\ResultFactory::create()
*
* But since it is marked as a temporary solution, this might change in a later Magento release
*/
$this->pageResult->addDefaultHandle();
$this->pageResult->renderNonHttpResult($pdfResponse);
return $pdfResponse;
}
/**
* @return string
*/
protected function renderPdf()
{
$pdf = $this->pdfFactory->create();
$pdf->addOptions($this->pdfGlobalOptions);
$pdfResponse = $this->renderSourceDocument();
$pdf->appendContent($pdfResponse);
$body = $pdf->file()->toString();
return $body;
}
/**
* @param Framework\App\Response\Http $response
* @param $body
* @return void
*/
protected function preparePdfResponse(Framework\App\Response\Http $response, $body)
{
$response->setHeader('Content-type', 'application/pdf', true);
$response->setHeader('Content-Length', \strlen($body));
$response->setHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0', true);
$response->setHeader('Last-Modified', \date('r'), true);
if (null !== $this->filename) {
$response->setHeader('Content-Disposition', 'attachment; filename="' . $this->filename . '"', true);
}
$response->appendBody($body);
}
}