错误:不在对象上下文中时使用 $this - 使用 dompdf laravel 生成 pdf
我想生成 view
文件 certificate_template
的 pdf
和将生成的 pdf 文件存储在 public
中,文件名为 certificate.pdf
。以下是我尝试过的。我也遇到了以下错误。非常感谢任何帮助。
use Barryvdh\DomPDF\PDF;
return PDF::loadView('certificate_template', $data)->stream();
//$file='certificate.pdf';
我已经安装了 dompdf
和 composer 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要更改控制器顶部的导入类,因此只需替换
为
或者 就
这样。
You need to change the imported class in the top of your controller, so just replace
with
Or simply
That's all.
试试这个
Try this
所有示例代码都运行良好。检查我的方法:
在
routes\web.php
中设置此内容:在
resources\views\template_one.blade.php
中使用示例文本:您将获得成功的结果!
更新
要获取内容文件,您可以使用以下代码:
All run well with example code. Check my way:
Set this content in
routes\web.php
:With sample text in
resources\views\template_one.blade.php
:You will get successful result!
Update
To get content file you can use follow code: