【问题标题】:How to get Blade template view as a raw HTML string?如何将 Blade 模板视图作为原始 HTML 字符串?
【发布时间】:2018-11-29 00:01:01
【问题描述】:

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

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

目前,我将 Blade 流式传输作为对浏览器的响应。

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

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

【问题讨论】:

    标签: php laravel laravel-blade


    【解决方案1】:

    您可以拨打render()view

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

    查看View源代码了解更多信息。

    【讨论】:

    • 在与compact 一起使用时出现未定义变量错误。有什么想法吗?
    • @Jonjie,这表明您尝试 compact 的变量之一未定义或超出范围。
    • 请看这个代码:$user = User::first();return view('users.edit', compact('user'))->render();
    • @Jonjie - 我看不出你发布的内容有什么问题。一定还有什么不对劲的地方。也许提出一个新问题并发布所有相关代码?
    【解决方案2】:

    这是将刀片文件下载/转换为 HTML 的完美解决方案。

    $view = view('welcome')->render();
    header("Content-type: text/html");
    header("Content-Disposition: attachment; filename=view.html");
    return $view;
    

    【讨论】:

      【解决方案3】:
          <!-- 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' =&gt; Barryvdh\Snappy\Facades\SnappyPdf::class, 'SnappyImage' =&gt; 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(),
      
          
      ),
      

      );

      【讨论】:

      • 感谢您的帮助,但这没有文字解释。你想达到什么目的。请改进您的答案。
      • 这适用于作为来自控制器的响应返回。但是要在变量中获取该 HTML 字符串,您必须使用 $html = view('greeting', ['name' => 'James']);
      • $html = view('greeting', ['name' => 'James']);这将为您提供在 $html 变量中呈现为字符串的 html。然后您可以使用 $html 变量来创建 PDF。我已经做到了,而且效果很好
      • 但您的示例中没有 $html 变量
      • @YevgeniyAfanasyev 我已经编辑了我的示例$html=view('greeting', ['name' =&gt; 'James']);
      猜你喜欢
      • 1970-01-01
      • 2018-08-30
      • 1970-01-01
      • 2016-12-30
      • 1970-01-01
      • 2014-08-06
      • 1970-01-01
      • 2018-09-26
      • 1970-01-01
      相关资源
      最近更新 更多