Question

Is there any Zend Helper that can make PDF document with tables. i need to pass result of my Query and the helper will return a PDF document with data in a table. Just like the below Csv Helper.

what i did here is that i put the given class in

Zend/Controller/Action/Helper

and than in my Action i do so

    public function getcsvAction() {
    $this->_helper->layout()->disableLayout();
    $this->_helper->viewRenderer->setNeverRender();
    try{
        $clModel = new Application_Model_DbTable_Mytable();
        $select = $clModel->clCSV());
        $this->_helper->Csv($select, "genertadfile name");
    }catch(Exception $e){
        echo 'Oops! Something went wrong.';
    }
    exit;
 }

and this the class

  <?php


// Zend_Controller_Action_Helper_Csv

class Zend_Controller_Action_Helper_Csv extends Zend_Controller_Action_Helper_Abstract{


public function direct($aryData = array(), $strName = "csv", $bolCols = true){
    $this->printExcel($aryData, $strName, $bolCols);
     }


  public function printExcel($aryData = array(), $strName = "csv", $bolCols = true){
          if (!is_array($aryData) || empty($aryData)){  exit(1); }

// header
    header('Content-Description: File Transfer');
    header('Content-Type: text/csv; charset=utf-8');
    header("Content-Disposition: attachment; filename=" . $strName . "-export.csv");
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-control: private, must-revalidate');
    header("Pragma: public");

    if ($bolCols){
      $aryCols = array_keys($aryData[0]);
      array_unshift($aryData, $aryCols);
       }
      ob_start();

    $fp = fopen("php://output", "w");
     if (is_resource($fp)){
   foreach ($aryData as $aryLine)  {
    fputcsv($fp, $aryLine, ',', '"');
        }
$strContent = ob_get_clean();

   $strContent = preg_replace('/^ID/', 'id', $strContent);
   $strContent = utf8_decode($strContent);
   $intLength = mb_strlen($strContent, 'utf-8');

header('Content-Length: ' . $intLength);
echo $strContent;
    exit(0);
}
ob_end_clean();
exit(1);
 }
   }
?>
Was it helpful?

Solution

There is a official module for pdf on zend framework 2: zendpdf

You can install it by adding zendframework/zendpdf to your composer.json file.

Its not finnished yet, that why you can't find any information about it. You can find here the previous documentation... just look for zend.pdf.*.rst files.

There is a ZF2 module for the DOMPDF library too. Find more info here. The code you can finde here. Easy installation by adding "dino/dompdf-module": "dev-master" to your composer.json file.

ADDING:

DOMPDF can be used like this:

public function dompdfAction()
{
    return new DOMPDFModule\View\Model\PdfModel\PdfModel(
        array(), // Variable assignments per Zend\View\Model\ViewModel
        array(
            'fileName' => 'monthly-report', // Optional; triggers PDF download, automatically appends ".pdf"
            'paperSize' => 'a4',
            'paperOrientation' => 'landscape'
        )
    );
}

It will generate an pdf file for the dompdfAction's viewscript.

ZendPdf can be used like this:

public function pdfAction()
{
    $pdf = new \ZendPdf\PdfDocument();

    $pdf->pages[] = ($page = $pdf->newPage(\ZendPdf\Page::SIZE_A4));
    $font = \ZendPdf\Font::fontWithName(\ZendPdf\Font::FONT_HELVETICA);
    $page->setFont($font, 36);
    $page->drawText('<h1>html não funciona...</h1>', 10, 536, 'utf-8');
    $page->drawText('PDF FUNCIONA', 10, 500, 'utf-8');
    $image = \ZendPdf\Image::imageWithPath('public/images/zf2-logo.png');
    $page->drawImage($image, 100, 100, 400, 300);
    $pdf->save("teste.pdf");
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top