daily pastebin goal
57%
SHARE
TWEET

rendering PDF from Twig

amaenta Mar 25th, 2019 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Generate and save a PDF
  3.  *
  4.  * @Route("/contracts/{id}/pdf", name="contract_pdf")
  5. */
  6. public function pdfAction($id, Request $request) {
  7.     // get contract from database
  8.     $em = $this->getDoctrine()->getManager();
  9.     $c = $em->getRepository('AppBundle:Contract')->find($id);
  10.  
  11.     $path = $request->server->get('DOCUMENT_ROOT');    // C:/wamp64/www/
  12.     $path = rtrim($path, "/");                         // C:/wamp64/www
  13.  
  14.     $html = $this->renderView('contracts/pdf/content.html.twig', array('c' => $c));
  15.  
  16.     $header = $this->renderView('contracts/pdf/header.html.twig', array(
  17.         'path' => $path
  18.     ));
  19.     $footer = $this->renderView('contracts/pdf/footer.html.twig', array(
  20.         'customer' => $c->getCustomer()
  21.     ));
  22.  
  23.     $output = $path . $request->server->get('BASE');        // C:/wamp64/www/project/web
  24.     $output .= '/pdf/contract-'. $c->getCustomerCode() .'.pdf';
  25.  
  26.     // Generate PDF file
  27.     $this->get('knp_snappy.pdf')->generateFromHtml($html, $output, array(
  28.         'header-html' => $header,
  29.         'footer-html' => $footer,
  30.     ));
  31.  
  32.     // Message + redirection
  33.     $this->addFlash('success', 'The PDF file has been saved.');
  34.     return $this->redirectToRoute('contract');
  35. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top