Reputation: 10028
In my laravel project I have the following myview.blade.php content:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{{ __('menu.title') }}</title>
</head>
<body>
{{__('content') }}
</body>
But instead of rendering it into a http response, I want to be able to generate a pdf file out of it. Do you know How I can do this?
Upvotes: 1
Views: 2368
Reputation: 348
I used many solutions, including DOMPDF, but on average the output generated by WKHTMLTOPDF is far better than DOMPDF.
WKHTMLTOPDF follow CSS rules more precisely.
Well, wkhtmltopdf is a command line program that accepts an html file as parameter, so you just have to install it and you are ready to go. It has package for Linux and Windows.
To the code
Generate the desired html from your view, like this:
$html = view('my_view', $arr);
Write the HTML to a temporary file:
$temp_file = tempnam(sys_get_temp_dir(), 'pdf_');
file_put_contents($temp_file, $html);
Run exec() on wkhtmltopdf:
$pdf_file = $temp_file . '.pdf';
exec('wkhtmltopdf ' . $temp_file . ' ' . $pdf_file);
Download your $pdf_file
.
Be happy.
Upvotes: 1
Reputation: 1188
You will need to use a HTML to PDF library such as DOMPDF to do the actual conversion of HTML -> PDF
https://github.com/dompdf/dompdf
To get HTML from a blade view, you can do:
$html = view('path/to/view', ['vars' => 'hello'])->render();
Upvotes: 1