Home > database >  How I can convert a dynamic blade content into PDF with mPDF in Laravel 7?
How I can convert a dynamic blade content into PDF with mPDF in Laravel 7?

Time:10-03

This my frontend.create-cv.pdf.blade.php :

<section>

    <h1>Language</h1>

    @foreach ($data['languages'] as $lang)

        <em>{{$lang->language}} | {{$lang->language_level}}</em>

    @endforeach

</section>

And this is my controller :

$data['languages'] = language_skill::where('jobseeker_id', $id)->get();

$mpdf->WriteHTML(view('frontend.create-cv.pdf', ['data' => $data]));

I'm geting this error :

Undefined variable: data (View: E:\xampp\htdocs\rzgroup.az\resources\views\frontend\create-cv\pdf.blade.php)`

How can i solve this problem ?

CodePudding user response:

First you should use PDF service provider in your controller and then pass data as array. For example: Controller:

use PDF;

related class {
public function createPDF($data)
    {      
      $data['languages'] = language_skill::where('jobseeker_id', $id)->get();
            
      view()->share('data', $data);
      $pdf = PDF::loadView('frontend.create-cv.pdf', $data);      
      
      return $pdf->stream('pdf_output.pdf');
    }
}

Route(web.php):

use App\Http\Controllers\YOURController;
Route::get('/pdf', [YOURController::class, 'createPDF']);

CodePudding user response:

Instead of that, $mpdf->WriteHTML(view('frontend.create-cv.pdf', ['data' => $data]));

use this, $mpdf->WriteHTML(view('frontend.create-cv.pdf', compact('data));

  • Related