Search code examples
pdftwigslimdompdf

Can't print to PDF in Slim Framework, Twig, and DomPDF


I have been struggling with this and I can't get it to work, despite that in some other posts they say you can use the Twig's render() function, I get the folowing error:

Requested HTML document contains no data. Dompdf\Exception

My code so far is:

use Dompdf\Dompdf;

$app->post('/my/path/for/printing',function()use($app){
    //echo '<p>printing to pdf ... ☺</p>';
    $request=$app->request;
    /* code for retrieving data from the database ... done.*/

    $variables = [
        'param1' =>  $param1,
        'param2' =>  $param2,
    ];

    $dompdf = new Dompdf();


    $content = $app->render('path/for/the/view.print.twig', $variables);
    $dompdf->loadHtml($content);
    $dompdf->render();
    $dompdf->stream('sample.pdf');

})->name('my.path.for.printing');

If I render my view, the data is displayed correctly. But with dompdf is not working.

Does anybody know how to fix this?

Otherwise, can you explain with an example of any other package how to print a Twig view in PDF?


Solution

  • render() doesn't return the HTML. Try:

    $content = $app-view->fetch('path/for/the/view.print.twig', $variables);