diff --git a/Controller/RenderPdfTrait.php b/Controller/RenderPdfTrait.php index 5680e6e..572ff18 100644 --- a/Controller/RenderPdfTrait.php +++ b/Controller/RenderPdfTrait.php @@ -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); diff --git a/Pdf/PdfFactory.php b/Pdf/PdfFactory.php index 52291fa..05db875 100644 --- a/Pdf/PdfFactory.php +++ b/Pdf/PdfFactory.php @@ -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,