错误:不在对象上下文中时使用 $this - 使用 dompdf laravel 生成 pdf

发布于 2025-01-12 01:55:30 字数 1286 浏览 1 评论 0原文

我想生成 view 文件 certificate_templatepdf 和将生成的 pdf 文件存储在 public 中,文件名为 certificate.pdf 。以下是我尝试过的。我也遇到了以下错误。非常感谢任何帮助。

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

我已经安装了 dompdfcomposer require barryvdh/laravel-dompdf

添加了正确的providers & config\app.php 中的别名

上面的代码给了我错误

错误:不在文件 C:\wamp\abc\vendor\barryvdh\laravel-dompdf\src\PDF.php 第 133 行的对象上下文中时使用 $this

,所以我尝试了以下代码,然后收到错误

ArgumentCountError:函数 Barryvdh\DomPDF\PDF::__construct() 的参数太少,第 415 行在 C:\wamp\www\abc\app\Http\Controllers\CertificatesController.php 中传递了 0 个参数,并且在第 415 行中传递了 4 个参数文件 C:\wamp\www\abc\vendor\barryvdh\laravel-dompdf\src\PDF.php 第 49 行

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

编辑

下面的代码保存 pdf 文件,但它是二进制格式的。我希望它以原始格式存储。我该怎么做?

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

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

紅太極 2025-01-19 01:55:30

您需要更改控制器顶部的导入类,因此只需替换

use Barryvdh\DomPDF\PDF;

use Barryvdh\DomPDF\Facade\Pdf;

或者 就

use 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.

凝望流年 2025-01-19 01:55:30

试试这个

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

Try this

$pdf = PDF::loadView('certificate_template', $data);
$pdf->save('certificate_template.pdf');
return $pdf;
小清晰的声音 2025-01-19 01:55:30

所有示例代码都运行良好。检查我的方法:

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();
});

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>

您将获得成功的结果!

更新

要获取内容文件,您可以使用以下代码:

// 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();

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文