简体   繁体   中英

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

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;

You need to change the imported class in the top of your controller, so just replace

use Barryvdh\DomPDF\PDF;

with

use Barryvdh\DomPDF\Facade\Pdf;

Or simply

use PDF;

That's all.

Try this

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

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 class="text-red">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 class="text-red">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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM