Search code examples
phppdfviewwkhtmltopdfphalcon

how to get phalcon view render for wkhtmltopdf?


I'm using Phalcon Framework in my project. I have generated reports page and I want to convert it to pdf and download. After lots of googling I get wkhtmltopdf, but it's converting only html file to pdf. I have phtml file. Can Phalcon render the view as html-strings or any other way I can convert?

Here is my code:

$pdf = new mikehaertl\wkhtmlto\Pdf(APP_PATH . '\\views\\views\\sales\\salesreports.phtml');
if (!$pdf->send()) {
   throw new Exception('Could not create PDF: ' . $pdf->getError());
}

Solution

  • Phalcon rendering a template into variable as html string:

    $view = new \Phalcon\Mvc\View\Simple();
    $view->setViewsDir(APP_PATH . '\app\views\views\sales\');
    $optionalParams = [
        'var1' => 123,
        'var2' => 345,
    ];
    // DO NOT put .phtml extension in the template name
    $html = $view->render('salesreports', $optionalParams); 
    

    I'm, not familiar with wkhtmlto\Pdf library, but judging from the docs should look something like this:

    use mikehaertl\wkhtmlto\Pdf;
    
    $pdf = new Pdf;
    $pdf->addPage($html);
    $pdf->send();