Search code examples
pdfzend-framework2pdf-generationdompdf

ZF2 How to save the pdf rendered by pdfModel?


i need some help plz , how can i save the pdf rendered by PdfModel, here is the code i use for generating the pdf ( the action/controller function ) :

public function genererPdfAction()
    {
        $id = (int) $this->params('id', null);


        $pdf = new PdfModel();
        $pdf->setOption("paperSize", "a4"); //Defaults to 8x11

        $pdf->setVariables(array(
          'produit' => $id
        ));

        return $pdf;
    }

Thanks.


Solution

  • here is the way i resolved the problem :

     public function generatePdfAction( $id = null){
    $product = $id;
         $pdf = new PdfModel();
                $pdf->setOption("paperSize", "a4"); //Defaults to 8x11
         $pdfView = new ViewModel($pdf);
                    $pdfView->setTerminal(true)
                        ->setTemplate('application/object/generate-pdf.phtml')
                        ->setVariables(array(
                           'produit' => $commande->getFkCategorie()->getDesignation(),
                          'product'=> $product
                        ));
                    $html = $this->getServiceLocator()->get('viewpdfrenderer')->getHtmlRenderer()->render($pdfView);
                    $eng = $this->getServiceLocator()->get('viewpdfrenderer')->getEngine();
    
                    $eng->load_html($html);
                    $eng->render();
                    $pdfCode = $eng->output();
                    file_put_contents('public/files/tmp-'.$id.'.pdf', $pdfCode);
        }
    

    Hope it helps.