Skip to content

Commit

Permalink
Merge pull request #5 from italolelis/master
Browse files Browse the repository at this point in the history
Pull request para modificações de streaming
  • Loading branch information
italolelis committed Feb 3, 2014
2 parents 5942617 + 794f850 commit e43c3c7
Showing 1 changed file with 52 additions and 25 deletions.
77 changes: 52 additions & 25 deletions src/Umbrella/SimpleReport/Renderer/WkPdfRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@

namespace Umbrella\SimpleReport\Renderer;

use DateTime;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Umbrella\SimpleReport\Api\IDatasource;
use Umbrella\SimpleReport\Api\ITemplate;
use Umbrella\SimpleReport\BaseRenderer;
use Umbrella\SimpleReport\Parser\TemplateParser;
use Symfony\Component\HttpFoundation\StreamedResponse;

/**
* Classe utilizada para gerar relatórios em PDF com o wkhtmltopdf
Expand All @@ -46,10 +48,20 @@ class WkPdfRenderer extends BaseRenderer
*/
private $parser;

/**
* @var boolean
*/
private $isStreaming = false;

/**
* @var int
*/
private $length = 0;

/**
* Inicializa uma nova instancia da classe WkPdfRenderer
* @param \Umbrella\SimpleReport\Api\IDatasource $datasource Uma instância de IDatasource
* @param \Umbrella\SimpleReport\Api\ITemplate $template Uma instância de ITemplate
* @param IDatasource $datasource Uma instância de IDatasource
* @param ITemplate $template Uma instância de ITemplate
*/
public function __construct(IDatasource $datasource, ITemplate $template)
{
Expand All @@ -73,16 +85,45 @@ public function setOutput($output)
return $this;
}

public function isStreaming()
{
return $this->isStreaming;
}

public function setStreaming($isStreaming)
{
$this->isStreaming = $isStreaming;
return $this;
}

public function render()
{
$htmlFile = $this->getHtmlPageContent();
$response = new StreamedResponse();
$response->setCallback(function () use($htmlFile) {
if ($this->isStreaming()) {
$response = new StreamedResponse();

ob_start();
$self = $this;

$response->setCallback(function () use($htmlFile, $self, &$lenght) {
$self->renderPdf($htmlFile);
ob_flush();
flush();
});
} else {
$response = new \Symfony\Component\HttpFoundation\Response();
$this->renderPdf($htmlFile);
ob_flush();
flush();
});
}

$d = $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_INLINE, md5($this->output));
$response->headers->set('Content-Disposition', $d);
$response->headers->set('Content-type', 'application/pdf');
$response->headers->set('Cache-Control', 'max-age=0, must-revalidate');
$response->headers->set('Pragma', 'public');
ini_set('zlib.output_compression', '0');

$response->send();

return $this;
}

Expand All @@ -99,7 +140,6 @@ protected function getHtmlPageContent()
), $this->template->getTags())
);
$content = $this->parser->parse();

file_put_contents($filename, $content);
ob_end_clean();

Expand All @@ -108,11 +148,11 @@ protected function getHtmlPageContent()

protected function createDate()
{
$date = new \DateTime();
$date = new DateTime();
return $date->format('d/m/Y');
}

protected function renderPdf($htmlFile)
public function renderPdf($htmlFile)
{
\wkhtmltox_convert('pdf', array(
'out' => $this->output,
Expand All @@ -125,6 +165,7 @@ protected function renderPdf($htmlFile)

$this->setPermissions($htmlFile, $this->output);
$this->unlinkFile($htmlFile);
$this->length = readfile($this->output);
}

/**
Expand All @@ -143,18 +184,4 @@ public function unlinkFile($file)
unlink($file);
}

/**
* Envia os headersde pdf para o browser
* @param boolean $attachment Se o pdf será exibido no browser ou baixado pelo usuário
*/
public function send($attachment = true)
{
$type = $attachment ? 'attachment' : 'inline';
header('Content-type: application/pdf');
header('Content-Disposition: ' . $type . '; filename="' . md5($this->output) . '.pdf"');
readfile($this->output);

$this->unlinkFile($this->output);
}

}

0 comments on commit e43c3c7

Please sign in to comment.