1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* Jasper report integration for PHP |
7
|
|
|
* |
8
|
|
|
* @link https://github.com/belgattitude/soluble-jasper |
9
|
|
|
* @author Vanvelthem Sébastien |
10
|
|
|
* @copyright Copyright (c) 2017 Vanvelthem Sébastien |
11
|
|
|
* @license MIT |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Soluble\Jasper\Exporter; |
15
|
|
|
|
16
|
|
|
use Psr\Http\Message\ResponseInterface; |
17
|
|
|
use Soluble\Japha\Bridge\Adapter as BridgeAdapter; |
18
|
|
|
use Soluble\Jasper\Exception\IOException; |
19
|
|
|
use Soluble\Jasper\Exception\IOPermissionException; |
20
|
|
|
use Soluble\Jasper\Proxy\Engine\Export\JRPdfExporter; |
21
|
|
|
use Soluble\Jasper\Proxy\Engine\JasperPrint; |
22
|
|
|
use Soluble\Jasper\Proxy\Export\SimplePdfExporterConfiguration; |
23
|
|
|
use Soluble\Jasper\Report; |
24
|
|
|
use Soluble\Jasper\Runner\BridgedReportRunner; |
25
|
|
|
use Zend\Diactoros\Response; |
26
|
|
|
use Zend\Diactoros\Stream; |
27
|
|
|
|
28
|
|
|
class PDFExporter |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var BridgedReportRunner |
32
|
|
|
*/ |
33
|
|
|
private $runner; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var Report |
37
|
|
|
*/ |
38
|
|
|
private $report; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var JasperPrint |
42
|
|
|
*/ |
43
|
|
|
private $jasperPrint; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var BridgeAdapter |
47
|
|
|
*/ |
48
|
|
|
private $ba; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var JRPdfExporter |
52
|
|
|
*/ |
53
|
|
|
private $exporter; |
54
|
|
|
|
55
|
1 |
View Code Duplication |
public function __construct(BridgedReportRunner $runner, Report $report) |
|
|
|
|
56
|
|
|
{ |
57
|
1 |
|
$this->runner = $runner; |
58
|
1 |
|
$this->report = $report; |
59
|
1 |
|
$this->ba = $runner->getBridgeAdapter(); |
60
|
1 |
|
$this->exporter = new JRPdfExporter($this->ba); |
61
|
1 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string $outputFile |
65
|
|
|
* @param string[] $pdfConfig |
66
|
|
|
*/ |
67
|
1 |
|
public function saveFile(string $outputFile, array $pdfConfig = null): void |
68
|
|
|
{ |
69
|
1 |
|
$jasperPrint = $this->getFilledReport(); |
70
|
1 |
|
$this->exporter->setExporterInput($jasperPrint->getJavaProxiedObject()); |
71
|
1 |
|
$this->exporter->setExporterOutput(new \SplFileInfo($outputFile)); |
72
|
1 |
|
if ($pdfConfig !== null) { |
73
|
|
|
$simplePdfConfig = $this->getPdfExporterConfiguration($pdfConfig); |
74
|
|
|
$this->exporter->setConfiguration($simplePdfConfig); |
75
|
|
|
} |
76
|
1 |
|
$this->exporter->exportReport(); |
77
|
1 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Return a new PSR-7 Response object filled with the PDF content. |
81
|
|
|
* |
82
|
|
|
* @param string[]|null $pdfConfig |
83
|
|
|
* |
84
|
|
|
* @return ResponseInterface |
85
|
|
|
*/ |
86
|
1 |
|
public function getPsr7Response(array $pdfConfig = null): ResponseInterface |
87
|
|
|
{ |
88
|
1 |
|
$tmpDir = sys_get_temp_dir(); |
89
|
1 |
|
$tmpFile = tempnam(sys_get_temp_dir(), 'soluble-jasper'); |
90
|
1 |
|
if ($tmpFile === false) { |
91
|
|
|
throw new IOException(sprintf( |
92
|
|
|
'Cannot create temporary file in %s', |
93
|
|
|
$tmpDir |
94
|
|
|
)); |
95
|
|
|
} |
96
|
1 |
|
if (chmod($tmpFile, 0666) === false) { |
97
|
|
|
throw new IOPermissionException(sprintf( |
98
|
|
|
'Cannot set file permission of file %s.', |
99
|
|
|
$tmpFile |
100
|
|
|
)); |
101
|
|
|
} |
102
|
|
|
|
103
|
1 |
|
$this->saveFile($tmpFile, $pdfConfig); |
104
|
|
|
|
105
|
1 |
|
$response = new Response(); |
106
|
1 |
|
$response = $response->withBody(new Stream($tmpFile)); |
107
|
1 |
|
$response = $response->withHeader('Content-type', 'application/pdf'); |
108
|
|
|
|
109
|
1 |
|
unlink($tmpFile); |
110
|
|
|
|
111
|
1 |
|
return $response; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param string[] $config |
116
|
|
|
*/ |
117
|
|
|
private function getPdfExporterConfiguration(array $config): SimplePdfExporterConfiguration |
|
|
|
|
118
|
|
|
{ |
119
|
|
|
$pdfConfig = new SimplePdfExporterConfiguration($this->ba); |
120
|
|
|
|
121
|
|
|
return $pdfConfig; |
122
|
|
|
} |
123
|
|
|
|
124
|
1 |
View Code Duplication |
private function getFilledReport(): JasperPrint |
|
|
|
|
125
|
|
|
{ |
126
|
1 |
|
if ($this->jasperPrint === null) { |
127
|
1 |
|
$jasperReport = $this->runner->compileReport($this->report); |
128
|
1 |
|
$this->jasperPrint = $this->runner->fillReport( |
129
|
1 |
|
$jasperReport, |
130
|
1 |
|
$this->report->getReportParams(), |
131
|
1 |
|
$this->report->getDataSource() |
132
|
|
|
); |
133
|
|
|
} |
134
|
|
|
|
135
|
1 |
|
return $this->jasperPrint; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.