简体   繁体   中英

Symfony - TCPdf - Unable to find template

I'm stuck with an annoying error with TCPdf and Symfony:

request.CRITICAL: Uncaught PHP Exception InvalidArgumentException: "Unable to find template "" (looked into: /Applications/MAMP/htdocs/api/app/Resources/views)." at /Applications/MAMP/htdocs/api/vendor/symfony/symfony/src/Symfony/Bridge/Twig/TwigEngine.php line 128 {"exception":"[object] (InvalidArgumentException(code: 0): Unable to find template \\"\\" (looked into: /Applications/MAMP/htdocs/api/app/Resources/views). at /Applications/MAMP/htdocs/api/vendor/symfony/symfony/src/Symfony/Bridge/Twig/TwigEngine.php:128, Twig_Error_Loader(code: 0): Unable to find template \\"\\" (looked into: /Applications/MAMP/htdocs/api/app/Resources/views). at /Applications/MAMP/htdocs/api/vendor/twig/twig/lib/Twig/Loader/Filesystem.php:215)"} []

In my controller I just create a pdf and output it:

public function pdfAction()
{
    $pdf = ...;
    ...
    $pdf->output();
}

My best guess is that Symfony is trying to return a Response (but the output already gave one) and thus the error. However I cannot find how I should resolve this in Symfony. Any help or tips are welcome.

PS: The pdf works perfectly, only monologger that is spamming me with errors ..

For future reference :

This is how I solved it in the end:

$pdf->Output('output.pdf', 'I');
return new Response();

I tried many different options but this is the only one that doesn't trigger that error.

With Symfony you need to return response as array.

Maybe this piece of code works:

$pdfOutput = $pdf->output();

return array('pdfOutput' => $pdfOutput); // or just try to return $pdf;

// If you don't use annotation : 
return $this->render('your-template.html.twig', array(
    'pdfOutput' => $pdfOutput
));

And in your template, if you want to result some things, your template looks like that :

{{ dump(pdfOutput) }}

EDIT

Try with a path as arguments

$pdf->Output("output.pdf", 'I');

TCPDF output doc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM