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

PhpLaravelLaravel Blade

Php Problem Overview


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?

Php Solutions


Solution 1 - Php

You can call render() on the view.

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

See the View source code for more information.

Solution 2 - Php

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;

Solution 3 - Php

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

    
),

);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionYevgeniy AfanasyevView Question on Stackoverflow
Solution 1 - PhpfubarView Answer on Stackoverflow
Solution 2 - PhpPri NceView Answer on Stackoverflow
Solution 3 - PhpAmina DarwishView Answer on Stackoverflow