如何从活动视图 Laravel 生成 PDF
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38415939/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to generate PDF from an active View Laravel
提问by Umair Shah Yousafzai
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,该数据在表中。
主要问题:我想为搜索了他/她的数据的学生的结果生成 PDF,那么如何为当前学生的结果生成 PDF。
Print/PDF Generator Button:
打印/PDF 生成器按钮:
<a href="{!! url('/getPDF') !!}">Print</a>
PDFController:
PDF控制器:
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:
路线.php:
Route::get('/getPDF', 'PDFController@getPDF');
PS : I am using https://github.com/barryvdh/laravel-dompdf
回答by Iftikhar uddin
Try this
尝试这个
First of all change route to
首先改变路线
Route::get('/getPDF/{id}', 'yourController@getPDF');
Pass the Searched Student id
from single view to PDF view like this
id
像这样将搜索到的学生从单一视图传递到 PDF 视图
<a href="{!! url('/getPDF', $student->id) !!}"> Print</a>
and in your PDF Controller
并在您的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 objectin your view like
并在您的视图中获取对象,例如
{!! $student->property !!}
回答by Julian Minde
When you call PDF::loadView()
I think you need to include the search results, just as you do in search()
当你打电话时,PDF::loadView()
我认为你需要包括搜索结果,就像你在search()
$keyword = Input::get('keyword');
$row = Student::where('rollno',$keyword)->first();
$rollno = $row['rollno'];
if($keyword == $rollno){
$results = Student::where('rollno',$keyword)->get();
$pdf = PDF::loadView('results.single', [
'search' => $results,
'keyword' => $keyword
]);
return $pdf->stream('result.pdf');
}