|
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-2019 Vanvelthem Sébastien |
|
11
|
|
|
* @license MIT |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Soluble\Jasper\Exporter; |
|
15
|
|
|
|
|
16
|
|
|
use Soluble\Japha\Bridge\Adapter as BridgeAdapter; |
|
17
|
|
|
use Soluble\Jasper\Exception; |
|
18
|
|
|
use Soluble\Jasper\Io\Exception\InvalidDirectoryException; |
|
19
|
|
|
use Soluble\Jasper\Io\JvmFileUtils; |
|
20
|
|
|
use Soluble\Jasper\Proxy\Engine\JasperExportManager; |
|
21
|
|
|
use Soluble\Jasper\Proxy\Engine\JasperPrint; |
|
22
|
|
|
use Soluble\Jasper\Report; |
|
23
|
|
|
use Soluble\Jasper\Runner\BridgedReportRunner; |
|
24
|
|
|
|
|
25
|
|
|
class BridgedExportManager implements ExportManagerInterface |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var BridgedReportRunner |
|
29
|
|
|
*/ |
|
30
|
|
|
private $runner; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var Report |
|
34
|
|
|
*/ |
|
35
|
|
|
private $report; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var JasperPrint |
|
39
|
|
|
*/ |
|
40
|
|
|
private $jasperPrint; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var BridgeAdapter |
|
44
|
|
|
*/ |
|
45
|
|
|
private $ba; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var JasperExportManager |
|
49
|
|
|
*/ |
|
50
|
|
|
private $exportManager; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var JvmFileUtils |
|
54
|
|
|
*/ |
|
55
|
|
|
private $jvmFileUtils; |
|
56
|
|
|
|
|
57
|
9 |
|
public function __construct(BridgedReportRunner $runner, Report $report) |
|
58
|
|
|
{ |
|
59
|
9 |
|
$this->runner = $runner; |
|
60
|
9 |
|
$this->report = $report; |
|
61
|
9 |
|
$this->ba = $runner->getBridgeAdapter(); |
|
62
|
9 |
|
$this->exportManager = new JasperExportManager($this->ba); |
|
63
|
9 |
|
$this->jvmFileUtils = new JvmFileUtils($this->ba); |
|
64
|
9 |
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @throws Exception\BrokenXMLReportFileException When cannot parse the xml content or invalid xml file |
|
68
|
|
|
* @throws Exception\ReportFileNotFoundException When the report file cannot be located (both php and java sides) |
|
69
|
|
|
* @throws Exception\ReportCompileException When there's an error compiling/evaluating the report |
|
70
|
|
|
* @throws Exception\JavaProxiedException When the compileReport has encountered a Java error |
|
71
|
|
|
* @throws Exception\JavaSaveProxiedException When the report directory cannot be written from the JVM side |
|
72
|
|
|
*/ |
|
73
|
9 |
|
public function savePdf(string $outputFile): void |
|
74
|
|
|
{ |
|
75
|
|
|
// Is writable ? |
|
76
|
|
|
|
|
77
|
9 |
|
$outputDirectory = dirname($outputFile); |
|
78
|
|
|
|
|
79
|
|
|
try { |
|
80
|
9 |
|
$canWrite = $this->jvmFileUtils->isDirectoryWritable($outputDirectory); |
|
81
|
|
|
} catch (InvalidDirectoryException $e) { |
|
82
|
|
|
throw new Exception\InvalidArgumentException($e->getMessage()); |
|
83
|
|
|
} catch (\Throwable $e) { |
|
84
|
|
|
// Others |
|
85
|
|
|
throw $e; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
9 |
|
if (!$canWrite) { |
|
89
|
|
|
throw new Exception\IOJavaPermissionException( |
|
90
|
|
|
sprintf("Cannot write into directory '%s', permissions error", $outputDirectory) |
|
91
|
|
|
); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
9 |
|
$this->exportManager->exportReportToPdfFile($this->getFilledReport()->getJavaProxiedObject(), $outputFile); |
|
95
|
9 |
|
} |
|
96
|
|
|
|
|
97
|
9 |
|
private function getFilledReport(): JasperPrint |
|
98
|
|
|
{ |
|
99
|
9 |
|
if ($this->jasperPrint === null) { |
|
100
|
9 |
|
$jasperReport = $this->runner->compileReport($this->report); |
|
101
|
9 |
|
$this->jasperPrint = $this->runner->fillReport( |
|
102
|
9 |
|
$jasperReport, |
|
103
|
9 |
|
$this->report->getReportParams(), |
|
104
|
9 |
|
$this->report->getDataSource() |
|
105
|
|
|
); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
9 |
|
return $this->jasperPrint; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|