Compare commits

..

10 Commits

@ -32,8 +32,9 @@ JASPER_SERVER_PASSWORD="<Password de acesso>"
```
### Fazendo requisições ao servidor
Para efetuar as requisições ao servidor jasper, utilize o serviço **JasperServerRequestServiceContract**. Este serviço, além de conter
o método **call(uri)** para efetuar a requisição, e os métodos auxiliares de configuração.
Para efetuar as requisições ao servidor jasper,
utilize o serviço [**JasperServerRequestServiceContract**](src/app/Services/Contracts/JasperServerRequestServiceContract.php). Este serviço, além de conter
o método **call(uri)** para efetuar a requisição e os métodos auxiliares de configuração.
Segue abaixo exemplo de requisição para um relatório chamado **relatorio_teste**, contendo um parâmetro chamado **parametro_um**:
@ -44,14 +45,52 @@ $report = $this->jasperServerRequestService
->addInputControl('parametro_um', 'valor_qualquer')
->setRequestTimeout(300)
->call($uri);
echo $report;
```
Por padrão os relatórios são retornados no formato **html**. Para especificar outro formato, basta utilizar o método
**setFormat** disponível no serviço:
**setFormat** disponível no [serviço](src/app/Services/JasperServerRequestService.php) e o enum [ReportFormatEnum](src/app/Enums/ReportFormatEnum.php) com os formatos disponíveis:
```shell
$this->jasperServerRequestService
->setFormat('pdf')
->setFormat(ReportFormatEnum::PDF)
...
```
#### Métodos inline() e download()
Caso queira forçar a renderização inline do relatório,
basta utilizar a função ```inline()``` após o método ```call()```. Veja o exemplo abaixo:
```php
$uri = '/relatorio_teste';
$this->jasperServerRequestService
->addInputControl('parametro_um', 'valor_qualquer')
->setRequestTimeout(300)
->call($uri)
->inline();
```
Apenas arquivos que o navegador consegue renderizar serão
exibidos. Caso contrário, o arquivo será baixado.
Caso queira forçar o download do arquivo, basta utilizar o método ```download('nome_do_arquivo')```, veja o exemplo abaixo:
```php
$uri = '/relatorio_teste';
$this->jasperServerRequestService
->addInputControl('parametro_um', 'valor_qualquer')
->setRequestTimeout(300)
->call($uri)
->download();
```
O nome do arquivo é opcional e, caso não seja informado,
terá um valor randômico atribuído.
Caso você necessite obter o valor binário do relatório, pode utilizar
o método getReport(). Exemplo:
```php
$uri = '/relatorio_teste';
$report = $this->jasperServerRequestService
->addInputControl('parametro_um', 'valor_qualquer')
->setRequestTimeout(300)
->call($uri)
->getReport();
```

@ -0,0 +1,20 @@
<?php
namespace Ae3\JasperServer\Laravel\Integrator\app\Enums;
class ContentDispositionEnum
{
public const INLINE = "inline";
public const ATTACHMENT = "attachment";
/**
* @return array
*/
public static function all(): array
{
return [
self::INLINE,
self::ATTACHMENT
];
}
}

@ -0,0 +1,73 @@
<?php
namespace Ae3\JasperServer\Laravel\Integrator\app\Enums;
class ReportFormatEnum
{
public const PDF = "pdf";
public const HTML = "html";
public const XML = "xml";
public const CSV = "csv";
public const DOCX = "docx";
public const ODT = "odt";
public const ODS = "ods";
public const XLSX = "xlsx";
/**
* @return array
*/
public static function all(): array
{
return [
self::PDF,
self::HTML,
self::XML,
self::CSV,
self::DOCX,
self::ODT,
self::ODS,
self::XLSX,
];
}
/**
* @return string[]
*/
public static function downloadable(): array
{
return [
self::PDF,
self::XML,
self::CSV,
self::DOCX,
self::ODT,
self::ODS,
self::XLSX,
];
}
/**
* @param string $format
* @return string
*/
public static function getMimeType(string $format): string
{
switch ($format) {
case self::PDF:
return 'application/pdf';
case self::XML:
return 'application/xml';
case self::CSV:
return 'text/csv';
case self::DOCX:
return 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
case self::ODT:
return 'application/vnd.oasis.opendocument.text';
case self::XLSX:
case self::ODS:
return 'application/vnd.oasis.opendocument.spreadsheet';
default:
return 'text/html';
}
}
}

@ -74,9 +74,24 @@ interface JasperServerRequestServiceContract
public function setContentDisposition(string $contentDisposition): JasperServerRequestService;
/**
* @param string $uri
* @param string|null $pdfFilename
* @return string
*/
public function call(string $uri, ?string $pdfFilename): string;
public function getReport(): string;
/**
* @param string $uri
* @return $this
*/
public function call(string $uri): JasperServerRequestService;
/**
* @return void
*/
public function inline(): void;
/**
* @param string|null $filename
* @return void
*/
public function download(?string $filename = null): void;
}

@ -2,6 +2,7 @@
namespace Ae3\JasperServer\Laravel\Integrator\app\Services;
use Ae3\JasperServer\Laravel\Integrator\app\Enums\ContentDispositionEnum;
use Ae3\JasperServer\Laravel\Integrator\app\Enums\ReportFormatEnum;
use Ae3\JasperServer\Laravel\Integrator\app\Services\Contracts\JasperServerRequestServiceContract;
use Jaspersoft\Client\Client;
@ -66,7 +67,12 @@ class JasperServerRequestService implements JasperServerRequestServiceContract
/**
* @var string
*/
protected $contentDisposition = 'inline';
protected $contentDisposition;
/**
* @var string
*/
protected $report;
/**
* @param Client $client
@ -75,7 +81,8 @@ class JasperServerRequestService implements JasperServerRequestServiceContract
Client $client
) {
$this->client = $client;
$this->format = ReportFormatEnum::HTML;
$this->setFormat(ReportFormatEnum::HTML);
$this->setContentDisposition(ContentDispositionEnum::INLINE);
}
/**
@ -192,14 +199,21 @@ class JasperServerRequestService implements JasperServerRequestServiceContract
return $this;
}
/**
* @return string Binary data of report
*/
public function getReport(): string
{
return $this->report;
}
/**
* @param string $uri
* @param string|null $pdfFilename
* @return string
* @return $this
*/
public function call(string $uri, ?string $pdfFilename = null): string
public function call(string $uri): self
{
$report = $this->client->reportService()->runReport(
$this->report = $this->client->reportService()->runReport(
$uri,
$this->format,
$this->pages,
@ -212,30 +226,61 @@ class JasperServerRequestService implements JasperServerRequestServiceContract
$this->transformerKey
);
if ($this->format === 'pdf') {
$this->setPdfHeader($report, $pdfFilename);
return $this;
}
/**
* @return void
*/
public function inline(): void
{
$this->setContentDisposition(ContentDispositionEnum::INLINE);
$this->setHeader($this->report);
echo $this->getReport();
}
/**
* @param string|null $filename
* @return void
*/
public function download(?string $filename = null): void
{
if (in_array($this->format, ReportFormatEnum::downloadable())) {
$this->setContentDisposition(ContentDispositionEnum::ATTACHMENT);
$this->setHeader($this->report, $filename);
}
return $report;
echo $this->getReport();
}
/**
* @param string $report
* @param string|null $pdfFilename
* @param string|null $filename
* @return void
*/
private function setPdfHeader(string $report, ?string $pdfFilename = null)
private function setHeader(string $report, ?string $filename = null)
{
if (!$pdfFilename) {
$pdfFilename = uniqid() . '.pdf';
if (!$filename) {
$filename = uniqid();
}
$filename = $this->appendFormatExtension($filename);
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Description: File Transfer');
header("Content-Disposition: $this->contentDisposition;filename=$pdfFilename");
header("Content-Disposition: $this->contentDisposition;filename=$filename");
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . strlen($report));
header('Content-Type: application/pdf');
header('Content-Type: ' . ReportFormatEnum::getMimeType($this->format));
}
/**
* @param string $filename
* @return string
*/
private function appendFormatExtension(string $filename): string
{
return $filename . '.' . $this->format;
}
}

Loading…
Cancel
Save