Jomon Antony Jomon Antony - 2 months ago 16
PHP Question

How to write Dom pdf output to file in Zf2

I am using dompdf to create one pdf file in zf2.

$pdf = new PdfModel();
$pdf->setOption('filename', 'monthly-report'); // Triggers PDF download, automatically appends ".pdf"
$pdf->setOption('paperSize', 'a4'); // Defaults to "8x11"
$pdf->setOption('paperOrientation', 'landscape'); // Defaults to "portrait"

// To set view variables
$pdf->setVariables(array(
'message' => 'Hello'
));

return $pdf;


Like this. But I want to write my pdf to a file.

Answer

Try This

$pdfView = new ViewModel();
    $pdfView->setTerminal(true)
        ->setTemplate('Order/order/forme-pdf-view.phtml')
        ->setVariables(array(
            'fetchResult'   => $fetchResult,
        ));
    $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(FILE_UPLOAD_PATH.'/ac.pdf',$pdfCode);