示例#1
0
 /**
  * @access core.read
  */
 public function exportAction($request)
 {
     $invoice = $this->repo('Invoice')->find($request->query->get('id'));
     if (!$invoice) {
         $this->flash('error', $this->t('invoiceDoesntExists'));
         return $this->redirect('Invoice', 'Invoice', 'index');
     }
     $view = $this->repo('Invoice')->getControllerNameByType($invoice);
     $html = $this->get('templating.engine')->render($view . '.PDF', ['products' => $this->repo('Product')->findAllByInvoice($invoice->getId()), 'invoice' => $invoice, 'type' => $request->get('original') == 1 ? $this->t('invoiceOriginal') : $this->t('invoiceCopy')]);
     if ($request->get('type') == 'pdf') {
         $response = new Response();
         $response->headers->setDisposition('attachment', $invoice->getHeadline() . ' - ' . str_replace('/', '-', $invoice->getNumber()) . ' (' . ($request->get('original') == 1 ? $this->t('invoiceOriginal') : $this->t('invoiceCopy')) . ').pdf');
         $response->headers->set('Content-Type', 'application/pdf');
         try {
             // Remote Images
             define('DOMPDF_ENABLE_REMOTE', true);
             define('DOMPDF_ENABLE_AUTOLOAD', false);
             require_once BASEPATH . '/vendor/dompdf/dompdf/dompdf_config.inc.php';
             $dompdf = new \DOMPDF();
             $dompdf->load_html($html);
             $dompdf->render();
             $response->setContent($dompdf->output());
         } catch (\Exception $e) {
             $this->flash('error', 'Wystąpił błąd podczas generowania pliku PDF: "' . $e->getMessage() . '"');
             return $this->redirect('Invoice', 'Invoice', 'index');
         }
         return $response;
     } else {
         return $this->response($html);
     }
 }