How to generate PDF from an active View Laravel - php

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.
Print/PDF Generator Button:
Print
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 : I am using https://github.com/barryvdh/laravel-dompdf

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
Print
and in your PDF Controller
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 !!}

When you call PDF::loadView() I think you need to include the search results, just as you do in 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');
}

Related

I am trying to pass searched query based on user input to my dompdf view

i want to use the result of that query and pass it into my pdfview and create a pdf based on the result. i have using sessions but did not work, is there any other solution
my controller for the search query:
public function index(Request $request){
$rsearch= $request->rsearch;
$fromdate= $request->fromdate ;
$todate= $request->todate ;
$rents = DB::table('rentcollection')
->join('tenants','rentcollection.AccountNo','=','tenants.AccountNo')
->join('propertys','tenants.PropertyID','=','propertys.PropertyID')
->select(DB::raw("CONCAT(tenants.OtherNames,' ',tenants.Surname) as
Fullname"),'rentcollection.Date as Date','rentcollection.Amount as
Amount','rentcollection.Period as Period','rentcollection.ReceiptNo as
Receipt','tenants.RegDate
as RegDate', 'tenants.AccountNo as Accno','tenants.Rent as Rent',
'tenants.NoOfProperty as
NoOfProperty','propertys.Address as Address','propertys.Property as Property')
->where('propertys.Address','like','%'.$rsearch.'%')
->whereBetween('Date', [$fromdate, $todate])
->paginate(20);
return view('reports.rent', compact('rents',));
}
my controller for creating pdf:
public function createPDF(Request $request){
$datas = $request->session()->get('cart');
$pdf = PDF::loadView('pdf.rent', ['orderedrent' => $datas,])->setPaper('A4','landscape');
return $pdf->stream('invoice.pdf');
}

Retrieve data from database in codeigniter

I am new to Codeigniter so I'm having some difficulties I want to retrieve data from the database and load it in the view but I couldn't figure out how.
Here is my model:
public function viewClientWaterwell(){
$userid = get_client()->userid;
$waterwells = $this->db->select('*')
->from($this->table)
->where('ww_client_id', $userid)
->get()->result_array();
return $waterwells;
}
Here is my clientController:
public function viewClient()
{
$data['waterwells'] = $this->waterwell_model->viewClientWaterwell();
$this->data($data);
$this->view('waterwells/clients/viewClient');
$this->layout();
}
I want to pass some data in a view to the client side but I can't figure out how. I think there is some problem with my model but I cannot figure out what. I'm new to this and will appreciate your feedback. Thanks!
You just need to use second argument to pass data to your template or view file
Here is example to start
Inside your controller function
public function viewClient()
{
$data = array( 'myvar' => 'I love stackoverflow' );
$this->load->view('waterwells/clients/viewClient', $data);
}
And inside your template or view file you can access them by their name.
<?php echo $myvar; ?>

Search all method missing where argument in Laravel 5

I'm trying to write a method in my controller to allow searching the results of my query on my view page. The query selects all results from the "ads" table and this method should allow filtering results by the ad's name inputting keywords in the search bar.
The controller code goes like this:
public function index(Request $request)
{
$title = trans('ad.title');
$ads = Ad::paginate(10);
if (!empty($request->input('search_all'))) {
$search_all = urldecode($request->input('search_all'));
$ads->where(function ($query) use ($search_all) {
$query->where('name', 'like', '%'.$search_all.'%')->get();
});
}else {
// Returning to view
return view('admin.ad.list')
->with('ads', $ads)
->with('title', $title);
}
}
However, when I run a search I get the following error: "Missing argument 2 for Illuminate\Support\Collection::where()".
What am I doing wrong?
At the top, when doing $ads = Ad::paginate(10); you have already pulled the results from the database so no more filtering can be done at DB level.
I'd do something like this instead:
public function index(Request $request)
{
$title = trans('ad.title');
$ads = Ad::select();
if ($request->input('search_all')) {
$search_all = urldecode($request->input('search_all'));
$ads->where('name', 'like', '%'.$search_all.'%');
}
// Returning to view
return view('admin.ad.list')
->with('ads', $ads->paginate(10))
->with('title', $title);
}

CakePHP 3+ issue with DomPdf

I currently have a function in a project controller class that I am calling to export a specific project to a pdf. I am running into problems when I try to pass that single project page that I am pulling from. If I call function from my view and pass in a string of valid html from my export() function it will create a pdf correctly. I am just wondering how I can get it from that ctp template to my controller to be created as a pdf. Thanks.
In my ProjectsController.php
public function view($id)
{
$creator = $this->Auth->user();
$project = $this->Projects->get($id, [
'contain' => [
'Countries', 'Languages', 'Tags',
'ProjectsLanguages', 'Students'
]
]);
$languages = $this->__getLanguageReqs($id);
$tags = $this->__getTagReqs($id);
$projSupervisors = $this->__getSupervisorsProjects($id);
$this->set('locations',$this->__getLocations($id,"project"));
$this->set('projSupervisors',$projSupervisors);
if($creator['role_id'] == 2){
$this->set('is_owner',in_array($creator['id'],array_keys($projSupervisors)));
}
else{
$this->set('is_owner', false);
}
$this->set('languages',$languages);
$this->set('tags',$tags);
$this->set('project', $project);
$this->set('_serialize', ['project']);
}
public function export($id = null) {
$dompdf = new Dompdf();
$dompdf->loadHtmlFile('/projects/view/' . $id);
$dompdf->render();
$dompdf->output();
$dompdf->stream('project');
}
In my view.ctp
<button class = 'project_edit' onclick = "location.href='/projects/export/<?= h($project->id) ?>'">Export this Project</button>
Update
I got it figured out. Configured a new .ctp with the same information from my view.ctp and called an export there with the populated data in a php script at the end of my file.
You can use a plugin like cakephp-dompdf, your code will be cleaner.

i create a custom url in Codeigniter but post data can't show

i'm create a online shopping store with vendor system and i want every vendor profile show with his username in url.
My current url show with id number like
www.mydomain.com/home/profile/1
i want my my url show user name in url like:
www.mydomain.com/username
My Code is Here:
Model
//get all vendors
public function vendor_profiles()
{
$where = array('ven_status' => 'Active');
$this->db->select()
->from('vendor_login')
->where($where,'DESC');
$data = $this->db->get();
return $data->result_array();
}
//get single vendor through id number
public function vendor_profile($id)
{
$where = array(
'vendor_id' => $id,
'ven_status' => 'Active',
);
$this->db->select()
->from('vendor_login')
->where($where,'DESC');
$data = $this->db->get();
return $data->first_row('array');
}
Controller
//show all vendors profile
public function vendor_profiles()
{
$data['profiles'] = $this->front->vendor_profiles();
$this->load->view('vendors',$data);
}
//show single vendor profile through id
public function profile($id)
{
$data['profile'] = $this->front->vendor_profile($id);
$this->load->view('profile',$data);
}
Views:
//here is show all vendor profiles (vendors.php)
<?php foreach($profiles as $vendor) : ?>
<img src="<?=base_url();?>uploads/<?=$vendor['ven_img'];?>">
<?=$vendor['ven_firstname'];?>
<?php endforeach; ?>
//Here is single vendor profile show (profile.php)
<p><img src="<?=base_url();?>uploads/<?=$profile['ven_logo'];?>"></p>
<p><img src="<?=base_url();?>uploads/<?=$profile['ven_img'];?>"></p>
<p>First Name : <?=$profile['ven_firstname'];?></p>
<p>Last Name : <?=$profile['ven_lastname'];?></p>
<p>Username : <?=$profile['username'];?></p>
Here is My Routs:
require_once( BASEPATH .'database/DB.php');
$db =& DB();
$query = $db->get('vendor_login');
$result = $query->result();
foreach ($result as $row) {
$rout["home/profile/" . $row->username] = "home/profile/" . $row->vendor_id;
}
A suggestion:
Try to avoid creating profile URLs like
www.mydomain.com/username
It may lead to problems if someone create a username same as an existing page in your application.
E.g. Assume you have a URL like www.mydomain.com/shop where your shop front page is displayed. If someone creates a username shop, it will not work.
If your username field is unique, you needn't to create routes in this way. Instead you can use
$route['profile/(:any)']="home/profile/$1";
With this you can create a meaningful profile URL something like
www.mydomain.com/profile/username
Now in your controller Home.php change the profile method as follows
public function profile($username)
{
// call your model function to retrieve userprofile with the unique username
// the username will be on $username variable
}
Your profile URL can be generated as below:
<a href='<?php echo base_url('profile/$username');?>'><?php echo $username;?></a>

Categories