102

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?

5 Answers 5

242

You can call render() on the view.

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

See the View source code for more information.

Sign up to request clarification or add additional context in comments.

4 Comments

I got an Undefined variable error when using it with compact. Any idea?
@Jonjie, that suggests that one of the variables you're trying to compact is undefined or out of scope.
Please see this code: $user = User::first(); return view('users.edit', compact('user'))->render();
@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?
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;

Comments

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(),

    
),

);

11 Comments

Thank you for trying to help, but this does not have explanation in words. What were you trying to achieve. Please improve your answer.
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']);
$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 perfectly
but you don't have an $html variable in your example
@YevgeniyAfanasyev I haved edited my example $html=view('greeting', ['name' => 'James']);
|
1

Will return as sting

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

Good luck!

4 Comments

Well, let's see how many "likes" will bring you your "laravel facade" style solution. Good luck! :)
This solution cost 3 hours :P
Why did not you like the accepted answer? Why have you decided to spend another 3 hours to look for other solutions?
render() was returning an error for a particular laravel version(4.2)
0

The correct, documented way is

$html = Blade::render('users.edit', ['user' => $user]);

2 Comments

How is that different from the accepted answer?
Do you mean to ask in what aspects does it differ? Besides being entirely different code? Well for one it's the documented way of doing things, which is guaranteed to keep working. It also does not have a @throws annotation so the statement is not marked as an inspection in PHPStorm when you use it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.