How to get Blade template view as a raw HTML string?
Asked Answered
G

4

100

I need the HTML of my Blade template as a string.

I'm going to use that HTML string to generate a PDF from it.

At the moment I have Blade streaming as a response back to browsers.

 return view('users.edit', compact('user'));

How can I get the raw HTML string from the blade template?

Glia answered 19/6, 2018 at 23:38 Comment(0)
S
233

You can call render() on the view.

$html = view('users.edit', compact('user'))->render();

See the View source code for more information.

Safelight answered 19/6, 2018 at 23:44 Comment(4)
I got an Undefined variable error when using it with compact. Any idea?Buchenwald
@Jonjie, that suggests that one of the variables you're trying to compact is undefined or out of scope.Safelight
Please see this code: $user = User::first(); return view('users.edit', compact('user'))->render();Buchenwald
@Jonjie - I can see nothing wrong with what you've posted. There must be something else amiss. Perhaps ask a new question and post all relevant code?Safelight
K
4

This is the perfect solution of download/converting a blade file to HTML.

$view = view('welcome')->render();
header("Content-type: text/html");
header("Content-Disposition: attachment; filename=view.html");
return $view;
Karynkaryo answered 20/11, 2021 at 8:5 Comment(0)
C
2
    <!-- View stored in resources/views/greeting.blade.php -->
    <html>
        <body>
            <h1>Hello, {{ $name }}</h1>
        </body>
    </html>

<!-- In your php controller  -->
    
    return view('greeting', ['name' => 'James']);

edited

<!-- In your PHP controller You can add html variable , and then use it for example to print PDF -->

$html=view('greeting', ['name' => 'James']);

 $pdf = \App::make('snappy.pdf.wrapper');
 $output = $pdf->loadHTML($html)->output();


$headers = [
            'Content-Type' => 'application/pdf',
            'Content-Disposition' => 'inline; filename="' . $filename . '"',
        ];
        
\Storage::put("pdfs/$filename", $output);
return response()->download(storage_path("app\\pdfs\\$filename"), $filename . '.pdf', $headers);

<!-- or return \Response::make($output, 200, $headers); -->

to use snappy you need to follow the instructions : https://github.com/barryvdh/laravel-snappy

1. Download wkhtmltopdf from here https://wkhtmltopdf.org/downloads.html

2. Package Installation: composer require barryvdh/laravel-snappy

3. In (app.php) providers array add Barryvdh\Snappy\ServiceProvider::class,

4. In (app.php) aliases array add 'PDF' => Barryvdh\Snappy\Facades\SnappyPdf::class, 'SnappyImage' => Barryvdh\Snappy\Facades\SnappyImage::class,

5. Run php artisan vendor:publish --provider="Barryvdh\Snappy\ServiceProvider"

6. In (snappy.php) edit the binary path based on your installation path for wkhtmltopdf For me it is the following : return array(

'pdf' => array(
    'enabled' => true,
    // base_path('vendor\wemersonjanuario\wkhtmltopdf-windows\bin\64bit\wkhtmltopdf'),
    'binary'  =>'"C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf"',
    'timeout' => false,
    'options' => array(),
    'env'     => array(),
),
'image' => array(
    'enabled' => true,
    'binary'  =>'"C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf"',
    'timeout' => false,
    'options' => array(),
    'env'     => array(),

    
),

);

Carduaceous answered 30/4, 2019 at 8:3 Comment(11)
Thank you for trying to help, but this does not have explanation in words. What were you trying to achieve. Please improve your answer.Glia
This works for returning as a response from the controller. But for getting that HTML string in a variable you will have to use $html = view('greeting', ['name' => 'James']);Douse
$html = view('greeting', ['name' => 'James']); this will get you the html rendered as string in $html variable. And then you can use the $html variable to create the PDF. I have done that and it works perfectlyCarduaceous
but you don't have an $html variable in your exampleGlia
@YevgeniyAfanasyev I haved edited my example $html=view('greeting', ['name' => 'James']);Carduaceous
it says Class snappy.pdf.wrapper does not existGlia
@YevgeniyAfanasyev I have edited my answer , added instruction for how to install snappy.pdf.wrapperCarduaceous
@AminaDarwish. Than you for editing your answer. I think the other answer is better. It gives a single function to achieve the requested result without using any extra packages.Glia
previous answer using Render(), when applied to a view, will only generate the corresponding raw html and store the result in a variable. It would not generate the PDF for you. as you have mentioned in your question <<<I'm going to use that HTML string to generate a PDF from it.>>> my answer shows how to get the raw html and then a way to use it to generate a PDF.Carduaceous
Thank you for your comment. It is my mistake, I should have mention that I'm already using github.com/dompdf/dompdf package for html to pdf conversions. I totally recommend you dompdf - it is much more popular than barryvdh/laravel-snappy. Besides, It allows to make pdf-s as strings, and then, as a string it can be attached to email, without being saved as files.Glia
Thanks a lot for your suggestion. I have a long great experience with snappy as well. before selecting snappy I have searched and found many people preferring snappy over dompdf github.com/barryvdh/laravel-snappy/issues/226 , I will give dompdf a try , compare , and let you know the results.Carduaceous
P
2

Will return as sting

$myViewData = View::make('test.view',compact('data'))->render();

Good luck!

Passion answered 30/5, 2022 at 7:41 Comment(4)
Well, let's see how many "likes" will bring you your "laravel facade" style solution. Good luck! :)Glia
This solution cost 3 hours :PPassion
Why did not you like the accepted answer? Why have you decided to spend another 3 hours to look for other solutions?Glia
render() was returning an error for a particular laravel version(4.2)Passion

© 2022 - 2024 — McMap. All rights reserved.