如何从活动的View Laravel生成PDF [英] How to generate PDF from an active View Laravel
本文介绍了如何从活动的View Laravel生成PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当用户输入否时,我有一个搜索框,它返回特定的学生结果.我想生成该搜索数据的PDF,该数据在表中.主要问题:我想为已搜索数据的学生生成PDF,那么如何为当前学生生成PDF.
I have a search box when user input roll no, it returns the specific student result. I want to generate PDF of that searched data, that data is in table. Main Question : I want to generate PDF for the result of student who has searched for his/her data, so how to generate PDF for that current student result.
打印/PDF生成器按钮:
<a href="{!! url('/getPDF') !!}">Print</a>
PDFController:
class PDFController extends Controller
{
public function getPDF(Request $request){
// I want some code here to get the current student result so that I can generate pdf for the current student
$pdf = PDF::loadView('results.single');
return $pdf->stream('result.pdf');
}
}
SearchController:
class ResultsSearchController extends Controller
{
public function search()
{
$keyword = Input::get('keyword');
$row = Student::where('rollno',$keyword)->first();
$rollno = $row['rollno'];
if($keyword == $rollno){
return View::make('results.single')
->with('search',Student::where('rollno',$keyword)
->get())->with('keyword',$keyword);
}else{
return view('errors.404');
}
}
}
Routes.php:
Route::get('/getPDF', 'PDFController@getPDF');
PS:我正在使用 https://github.com/barryvdh/laravel-dompdf
推荐答案
尝试一下
首先将路线更改为
Route::get('/getPDF/{id}', 'yourController@getPDF');
像这样将搜索到的学生 id
从单个视图传递到PDF视图
Pass the Searched Student id
from single view to PDF view like this
<a href="{!! url('/getPDF', $student->id) !!}"> Print</a>
和您的 PDF控制器
public function getPDF(Request $request,$id){
$student = Student::findOrFail($id);
$pdf = PDF::loadView('pdf.result',['student' => $student]);
return $pdf->stream('result.pdf', array('Attachment'=>0));
}
并在您的视图中获得对象,例如
and get the object in your view like
{!! $student->property !!}
这篇关于如何从活动的View Laravel生成PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文