如何获取Blade模板视图作为原始HTML字符串?

2020/12/21 08:21 · php ·  · 0评论

我需要Blade模板的HTML作为字符串。

我将使用该HTML字符串从中生成PDF。

目前,我有Blade流作为对浏览器的响应。

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

如何从刀片模板获取原始HTML字符串?

你可以叫render()view

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

请参阅View源代码以获取更多信息。

    <!-- 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']);

已编辑

<!-- 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); -->

要使用snappy,您需要按照说明进行操作: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 :
返回数组(

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

    
),

);

本文地址:http://php.askforanswer.com/ruhehuoqublademobanshituzuoweiyuanshihtmlzifuchuan.html
文章标签: ,   ,  
版权声明:本文为原创文章,版权归 admin 所有,欢迎分享本文,转载请保留出处!

文件下载

老薛主机终身7折优惠码boke112

上一篇:
下一篇:

评论已关闭!