Skip to content

Commit

Permalink
Make RenderPdfTrait work without container (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-schranz authored and wachterjohannes committed May 13, 2019
1 parent bf948c6 commit 79e2b62
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
30 changes: 25 additions & 5 deletions Controller/RenderPdfTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,44 @@
namespace Massive\Bundle\PdfBundle\Controller;

use Massive\Bundle\PdfBundle\Pdf\PdfFactory;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Bundle\FrameworkBundle\Tests\Controller\ContainerAwareController;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\HttpFoundation\Response;

trait RenderPdfTrait
{
protected function renderPdf($template, $parameters = [], $requestFormat = 'pdf', Response $response = null)
/**
* @var PdfFactory
*/
protected $pdfFactory;

protected function getPdfFactory()
{
/** @var PdfFactory $pdfFactory */
$pdfFactory = $this->get('massive_pdf.pdf_factory');
if (!$this->pdfFactory) {
if (!$this instanceof Controller) {
throw new \RuntimeException('The pdfFactory service need to be set to be used.');
}

$this->pdfFactory = $this->get('massive_pdf.pdf_factory');
}

return $this->pdfFactory;
}

protected function renderPdf($template, $parameters = [], $requestFormat = 'pdf', Response $response = null)
{
if (!$response) {
$response = new Response();
}

$parameters['request_format'] = $requestFormat;

if ('pdf' === $requestFormat) {
$content = $pdfFactory->create($template, array_merge($parameters, ['request_format' => 'pdf']));
$content = $this->pdfFactory->create($template, $parameters);
$response->headers->set('Content-Type', 'application/pdf');
} else {
$content = $this->renderView($template, array_merge($parameters, ['request_format' => 'html']));
$content = $this->pdfFactory->generateHtml($template, $parameters);
}

$response->setContent($content);
Expand Down
2 changes: 1 addition & 1 deletion Pdf/PdfFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function create($template, $parameters = [], $options = [])
*
* @return string
*/
protected function generateHtml($template, $parameters = [])
public function generateHtml($template, $parameters = [])
{
return $this->templating->render(
$template,
Expand Down

0 comments on commit 79e2b62

Please sign in to comment.