source

블레이드 템플릿 뷰를 원시 HTML 문자열로 가져오는 방법

goodcode 2022. 10. 26. 22:29
반응형

블레이드 템플릿 뷰를 원시 HTML 문자열로 가져오는 방법

블레이드 템플릿의 HTML을 문자열로 만들어야 합니다.

이 HTML 문자열을 사용하여 PDF를 생성합니다.

현재 브라우저에 대한 응답으로 블레이드 스트리밍을 사용하고 있습니다.

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

블레이드 템플릿에서 원시 HTML 문자열을 가져오려면 어떻게 해야 합니까?

전화하시면 됩니다.render()에서view.

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

상세한 것에 대하여는, 소스코드를 참조해 주세요.

블레이드 파일을 HTML로 다운로드/변환하는 완벽한 솔루션입니다.

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

snapy 를 사용하려면 , 다음의 순서에 따를 필요가 있습니다.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 어레이()

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

    
),

);

따끔따끔하게 돌아오다

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

행운을 빕니다.

언급URL : https://stackoverflow.com/questions/50938285/how-to-get-blade-template-view-as-a-raw-html-string

반응형