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.
In my case the problem was that I passed
null
in$html
toKnp\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 existwkhtmltopdf
throws error like described in question.