[英]laravel generate PDF from blade
I am trying to download my page in pdf format and I have been following this tutorial step by step to do it. 我正在尝试以pdf格式下载我的页面,并且我一直在逐步按照本教程进行操作。 I tried using a sample data to do it and it work but when I try to use my own data it keep giving me undefined $data3, $object, ..
我尝试使用示例数据来做到这一点,但是它可以正常工作,但是当我尝试使用自己的数据时,它会一直给我未定义的$ data3,$ object,..。
But I have already defined them already in my controller already: 但是我已经在我的控制器中定义了它们:
testController testController
public function getTestdata($id){
$user = PersonalInfo::find($id);
$object = PersonalInfo::where('id',$id)->get();
$object1 = AdditionalInformation::where('user_id',$id)->get();
$data3=UserImage::where('user_id',$id)->get();
return view('test',compact('id','object','object1','user','data3'));
}
test.blade.php test.blade.php
<a href="{{ url('/pdf') }}">Download PDF</a>
...
<th>
@foreach($data3 as $currentUser)
<img src="{{ url('images/' . $currentUser->name ) }}">
@endforeach
</th>
...
@php
$userFamily = $user->userFamily;
@endphp
<tr>
<td><b>FAMILY Members</b></td>
<td>
@foreach($userFamily as $val)
@if($val["meta_key"] == "relationshiplist")
{{ $val["meta_value"] }} |
@endif
@endforeach
</td>
DownloadPDFController: 下载PDFController:
public function fun_pdf(){
$pdf = PDF::loadView('test'); //load view page
return $pdf->download('test.pdf'); // download pdf file
}
Route: 路线:
Route::get('/pdf', 'pdfDownloadController@fun_pdf');
Route::get('/user/showtest/{id}/test1', 'testController@getTestData')->name('template');
I also tried following this website to do but it does't seem to work as well, instead it gave me undefined $id. 我也尝试按照此网站进行操作,但似乎效果不佳,相反,它给了我未定义的$ id。 http://itsolutionstuff.com/post/laravel-5-generate-pdf-from-html-view-file-and-download-using-dompdfexample.html
http://itsolutionstuff.com/post/laravel-5-generate-pdf-from-html-view-file-and-download-using-dompdfexample.html
If possible could you guys guide me on how to generate the PDF file 如果可能的话,你们可以指导我如何生成PDF文件
For this first, you should install Dompdf plugin https://github.com/barryvdh/laravel-dompdf 首先,您应该安装Dompdf插件https://github.com/barryvdh/laravel-dompdf
and everything else in its documentation 及其文档中的其他所有内容
I think you should update the controller like this and pass id parameter, change the route also 我认为您应该像这样更新控制器并传递id参数,同时更改路由
public function fun_pdf($id){
$user = PersonalInfo::find($id);
$object = PersonalInfo::where('id',$id)->get();
$object1 = AdditionalInformation::where('user_id',$id)->get();
$data3=UserImage::where('user_id',$id)->get();
$data =[
'id' => $id,
'object' => $object,
'object1' => $object1,
'user' => $user,
'data3' => $data3,
];
$pdf = PDF::loadView('test',$data); //load view page
return $pdf->download('test.pdf'); // download pdf file
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.