milardovich

Symfony Generar PDF al vuelo

May 12th, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.22 KB | None | 0 0
  1.         // Usar este bundle: https://packagist.org/packages/spraed/pdf-generator-bundle/SpraedPDFGeneratorBundle
  2.         // Más info y repo: https://github.com/stedekay
  3.  
  4.  
  5.         /*
  6.          * Ejemplo de cómo generar un PDF en el vuelo (sin guardar el archivo).
  7.          * Ver la annotation con @Pdf() antes del método. Todo esto va dentro del controller.
  8.          */
  9.  
  10.         /**
  11.          * @Pdf()
  12.          * @Route("/informe/pdf/{codInforme}",name="presupuesto/pdf")
  13.          */
  14.         public function pdfAction($codInforme){
  15.             // Acá se le pasan las variables que necesites manipular con twig, le mandé codigo, items y total como ejemplo. Obviamente no va a funcionar porque $total e $items no están declaradas, es sólo a modo ilustrativo.
  16.             $content = $this->renderView('TuBundle:Carpeta:informe.pdf.twig', ['codigo' => $codInforme, 'items' => $items, 'total' => $total]);
  17.         $pdfGenerator = $this->get('spraed.pdf.generator');
  18.  
  19.         // Nótese el tipo de respuesta, que sale de $pdfGenerator
  20.         return new Response($pdfGenerator->generatePDF($content),
  21.             200,
  22.             array(
  23.                 'Content-Type' => 'application/pdf',
  24.                 'Content-Disposition' => 'inline; filename="informe.pdf"'
  25.             )
  26.         );
  27.     }
Advertisement
Add Comment
Please, Sign In to add comment