Adicionando header quando o formato for pdf

laravel-11
jtfnetoo 3 years ago
parent 13e3e993c6
commit 15ae7ec4c1

@ -67,9 +67,16 @@ interface JasperServerRequestServiceContract
*/ */
public function setTransformerKey(string $transformerKey): JasperServerRequestService; public function setTransformerKey(string $transformerKey): JasperServerRequestService;
/**
* @param string $contentDisposition
* @return $this
*/
public function setContentDisposition(string $contentDisposition): JasperServerRequestService;
/** /**
* @param string $uri * @param string $uri
* @param string|null $pdfFilename
* @return string * @return string
*/ */
public function call(string $uri): string; public function call(string $uri, ?string $pdfFilename): string;
} }

@ -2,6 +2,7 @@
namespace Ae3\JasperServer\Laravel\Integrator\app\Services; namespace Ae3\JasperServer\Laravel\Integrator\app\Services;
use Ae3\JasperServer\Laravel\Integrator\app\Enums\ReportFormatEnum;
use Ae3\JasperServer\Laravel\Integrator\app\Services\Contracts\JasperServerRequestServiceContract; use Ae3\JasperServer\Laravel\Integrator\app\Services\Contracts\JasperServerRequestServiceContract;
use Jaspersoft\Client\Client; use Jaspersoft\Client\Client;
@ -15,7 +16,7 @@ class JasperServerRequestService implements JasperServerRequestServiceContract
/** /**
* @var string * @var string
*/ */
protected $format = 'html'; protected $format = null;
/** /**
* @var null * @var null
@ -62,6 +63,11 @@ class JasperServerRequestService implements JasperServerRequestServiceContract
*/ */
protected $requestTimeout = 60; protected $requestTimeout = 60;
/**
* @var string
*/
protected $contentDisposition = 'inline';
/** /**
* @param Client $client * @param Client $client
*/ */
@ -69,6 +75,7 @@ class JasperServerRequestService implements JasperServerRequestServiceContract
Client $client Client $client
) { ) {
$this->client = $client; $this->client = $client;
$this->format = ReportFormatEnum::HTML;
} }
/** /**
@ -175,13 +182,24 @@ class JasperServerRequestService implements JasperServerRequestServiceContract
return $this; return $this;
} }
/**
* @param string $contentDisposition
* @return $this
*/
public function setContentDisposition(string $contentDisposition): self
{
$this->contentDisposition = $contentDisposition;
return $this;
}
/** /**
* @param string $uri * @param string $uri
* @param string|null $pdfFilename
* @return string * @return string
*/ */
public function call(string $uri): string public function call(string $uri, ?string $pdfFilename = null): string
{ {
return $this->client->reportService()->runReport( $report = $this->client->reportService()->runReport(
$uri, $uri,
$this->format, $this->format,
$this->pages, $this->pages,
@ -193,5 +211,31 @@ class JasperServerRequestService implements JasperServerRequestServiceContract
$this->saveDataSnapshot, $this->saveDataSnapshot,
$this->transformerKey $this->transformerKey
); );
if ($this->format === 'pdf') {
$this->setPdfHeader($report, $pdfFilename);
}
return $report;
}
/**
* @param string $report
* @param string|null $pdfFilename
* @return void
*/
private function setPdfHeader(string $report, ?string $pdfFilename = null)
{
if (!$pdfFilename) {
$pdfFilename = uniqid() . '.pdf';
}
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Description: File Transfer');
header("Content-Disposition: $this->contentDisposition;filename=$pdfFilename");
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . strlen($report));
header('Content-Type: application/pdf');
} }
} }

Loading…
Cancel
Save