Home > Enterprise >  Error: Using $this when not in object context - generate pdf using dompdf laravel
Error: Using $this when not in object context - generate pdf using dompdf laravel

Time:03-07

I wanted to generate pdf of the view file certificate_template & store the generated pdf file in public with the filename certificate.pdf . Below is what i tried.I got stuck with the below errors too. Any help is much appreciated.

use Barryvdh\DomPDF\PDF;
return PDF::loadView('certificate_template', $data)->stream();
//$file='certificate.pdf';

I've installed dompdf with composer require barryvdh/laravel-dompdf.

added the correct providers & aliases in config\app.php.

The above code gives me the error

Error: Using $this when not in object context in file C:\wamp\abc\vendor\barryvdh\laravel-dompdf\src\PDF.php on line 133

so i tried the below code, then i get the error

ArgumentCountError: Too few arguments to function Barryvdh\DomPDF\PDF::__construct(), 0 passed in C:\wamp\www\abc\app\Http\Controllers\CertificatesController.php on line 415 and exactly 4 expected in file C:\wamp\www\abc\vendor\barryvdh\laravel-dompdf\src\PDF.php on line 49

$pdf=new PDF;
return $pdf->loadView('certificate_template', $data);

EDIT

The below code saves pdf file but its in binary format. I wanted it to be storedin original format.How can i do that?

$pdf=app()->make(PDF::class); 
$pdf->loadView('certificate_template', $data); 
$pdf->save('certificate_template.pdf');
return $pdf;

CodePudding user response:

Try using the container, app()->make(PDF::class) instead of new PDF:

$pdf = app()->make(PDF::class); 
$pdf->loadView('certificate_template', $data); 
$pdf->save('certificate_template.pdf'); 
return $pdf->stream('download.pdf');

CodePudding user response:

Try this

$pdf = PDF::loadView('certificate_template', $data);
$pdf->save('certificate_template.pdf');
return $pdf;

CodePudding user response:

All run well with example code. Check my way:

Set this content in routes\web.php:

Route::get('/', function () {
    // return view('welcome');
    // Work1
    // $pdf = App::make('dompdf.wrapper');
    // $pdf->loadHTML('<h1>Test</h1>');
    // return $pdf->stream();

    // Work2
    $data = [
        'text1'=>'<h1>Test</h1><div >This line is red color</div>',
        'text2'=> 'Just text'
    ];
    return PDF::loadView('template_one', $data)->stream();
});

With sample text in resources\views\template_one.blade.php:

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <title>Template One</title>

        <style>
            .text-red {
                color: red;
            }
        </style>
    </head>
    <body>
       This is my page of test!
       {!! $text1 !!}
       <br/>
       {{ $text2 }}
    </body>
</html>

You will get successful result!

Update

To get content file you can use follow code:

// Work3
$data = [
    'text1'=>'<h1>Test</h1><div >This line is red color</div>',
    'text2'=> 'Just text'
];
$pdf = app('dompdf.wrapper');  
$pdf->loadView('template_one', $data); 
// To save content as your desire
return $pdf->output();
  • Related