Laravel snappy file was not created

3.6k views Asked by At

I am trying to create pdf with Laravel snappy.

The problem is when I try to run wkhtmltopdf command in the console for example:

wkhtmltopdf http://google.com google.pdf

it works fine and generates pdf.

But when I try to generate pdf in my controller, then it throws this exception:

RuntimeException
The file '/home/alemil/Projects/test/storage/app/annual_reports/Pdf test.pdf' was not created (command: /usr/local/bin/wkhtmltopdf --lowquality --orientation 'landscape' --page-size 'a3' '/tmp/knp_snappy5bb49cd6bfcf14.16728595.html' '/home/alemil/Projects/test/storage/app/annual_reports/Pdf test.pdf').

I tried to copy command above and execute directly in console. This is what I get:

Loading pages (1/6) Error: Failed to load http://tmp/knp_snappy5bb49cd6bfcf14.16728595.html, with network status code 3 and http status code 0 - Host tmp not found Error: Failed loading page http:///tmp/knp_snappy5bb49cd6bfcf14.16728595.html (sometimes it will work just to ignore this error with --load-error-handling ignore) Exit with code 1 due to network error: HostNotFoundError

As I can see, it tries to fetch local file as it is remote. I couldn't find this issue so I am asking if someone can solve this or has any idea.

My environment is Ubuntu 18.04 PHP and PHP 7.2.10.

This is my code:

    // app.php aliases
   'PDF' => Barryvdh\Snappy\Facades\SnappyPdf::class,    

    // controller code
    PDF::loadView('admin.reports.annual.pdf_template', $data)
        ->setPaper('a3', 'landscape')
        ->save(
            storage_path('app/annual_reports/') . $report->pdf, 
            true // when there is file with same name it throws file already exists so I had to set owerwrite to true
        );

Also, one more thing to point out, I was using barryvdh/laravel-dompdf and everything was working fine except it cannot render charts so I'm now trying to set up snappy.

2

There are 2 answers

0
Serhii Popov On BEST ANSWER

In my case the problem was that I passed null in $html to Knp\Snappy\Pdf::getOutputFromHtml($html). Under the hood it cheks if $html !== null and creates a temporary file for further PDF generating. If temporary file doesn't exist wkhtmltopdf throws error like described in question.

2
Moubarak Hayal On

hmm

I think (not sure) that there are a a blocking in your application that blocks your controller from getting to the link. Or at the creating of the PDF.

It can also be your code syntax.

you can try this for html :

$pdf = new Pdf('/usr/local/bin/wkhtmltopdf');
$pdf->generateFromHtml('<h1>test</h1><p>test from test</p>', '/tmp/test-123.pdf');