Laravel: Export PDF from blade with image - php

I'm trying to export a table blade with an image to PDF Using TCPDF.
My Controller:
public function exportPDF(){
$users = User::orderBy('id','DESC')->get();
$view = View::make('admin.users.export',compact('users'));
$contents = (string) $view;
$contents = $view->render();
$pdf = new PDF();
$pdf::SetTitle('Users List');
$pdf::SetSubject('Users List');
$pdf::SetKeywords('User');
$pdf::SetTitle('User List');
$pdf::AddPage();
$pdf::writeHTML($contents, true, false, true, false, '');
$pdf::Output('users.pdf');
}
My Blade:
<html>
<head>
<body>
<h4>Users List</h4>
<table>
<tr>
<th>Image</th>
<th>First Name</th>
<th>Last Name</th>
<th>User Name</th>
<th>Email</th>
<th>Role</th>
</tr>
#if($users->count())
#foreach($users as $user)
<tr>
<td><img src="{{asset('images/users/'.$user->image_path)}}"></td>
<td>{{$user->first_name}}</td>
<td>{{$user->last_name}}</td>
<td>{{$user->username}}</td>
<td>{{$user->email}}</td>
<td>{{$user->role}}</td>
</tr>
#endforeach
</table>
</body>
</html>
But when i want to get the pdf it take a long time and after that it gave me this message
TCPDF ERROR: [Image] Unable to get the size of the image:http://127.0.0.1:8000/images/users/1523518590.jpg
Thank's

Related

Can't display the fetch data

I am new in ReactJS so fetching data isn't my expertise, so I'm trying to learn ReactJS with my PHP Native. The problem is fetching data is difficult for me. This code below there's no error but it won't display my data.
It can't display in my UI, the data is stayed in console(Network -> Response), i tried everything still can't output it. Can someone help?
import './Timesheet.css';
import React, { useState, useEffect } from 'react';
import { Table, Container, Button, } from 'react-bootstrap';
const Timesheet = () => {
const [item, setItem] = useState([]);
useEffect(() => {
fetch("http://localhost/WaterRefillingStation/app/controllers/timesheet_controller.php")
.then(response => response.json())
.then(
(result) => {
setItem(result);
}
)
}, [])
return (
<Container fluid>
<div className="d_flex my-4, text-uppercase">
<h1>
Timesheet Screen
</h1>
</div>
<div className="float-end, tryy" >
<Button variant="success" size="md">Time IN</Button>{' '}
<Button variant="secondary" size="md">Time OUT</Button>{' '}
<Button variant="danger" size="md">Logout</Button>
</div>
<Table striped bordered hover size="md" variant="dark" responsive="xl" >
<thead className="thead-dark">
<tr style={{textAlign: "center"}}>
<th>ID</th>
<th>Date</th>
<th>Profile Picture</th>
<th>Last Name</th>
<th>First Name</th>
<th>Designation</th>
<th>Time IN</th>
<th>Time OUT</th>
</tr>
</thead>
<tbody>
{item.map(item => (
<tr key={item}>
<td>{item.userID}</td>
<td>{item.birthday}</td>
<td>{item.profpic}</td>
<td>{item.lastname}</td>
<td>{item.firstname}</td>
<td>{item.designation}</td>
<td>{item.created_at}</td>
<td>{item.updated_at}</td>
</tr>
))}
</tbody>
</Table>
</Container>
);
}
export default Timesheet;
Ok, it seems that you have used .map in the wrong way. You can use it for an array, not an object. use this instead:
return (
<Container fluid>
<div className="d_flex my-4, text-uppercase">
<h1>
Timesheet Screen
</h1>
</div>
<div className="float-end, tryy" >
<Button variant="success" size="md">Time IN</Button>{' '}
<Button variant="secondary" size="md">Time OUT</Button>{' '}
<Button variant="danger" size="md">Logout</Button>
</div>
<Table striped bordered hover size="md" variant="dark" responsive="xl" >
<thead className="thead-dark">
<tr style={{textAlign: "center"}}>
<th>ID</th>
<th>Date</th>
<th>Profile Picture</th>
<th>Last Name</th>
<th>First Name</th>
<th>Designation</th>
<th>Time IN</th>
<th>Time OUT</th>
</tr>
</thead>
<tbody>
<tr key={item}>
<td>{item.userID}</td>
<td>{item.birthday}</td>
<td>{item.profpic}</td>
<td>{item.lastname}</td>
<td>{item.firstname}</td>
<td>{item.designation}</td>
<td>{item.created_at}</td>
<td>{item.updated_at}</td>
</tr>
</tbody>
</Table>
</Container>
if you want to have a table for each user (for example), you should to provide data like below in your backend:
{
data:[
{ USER1 Info},
{ USER2 Info},
// so on
]
}

Table heading on every page using MPDF

I want the table heading to be displayed on each page while generating pdf using MPDF library.
$mpdf = new \Mpdf\Mpdf([
'mode'=>'utf-8',
'format' => 'A4-L'
]);
$mpdf->SetHTMLHeader("<table>
<tr>
<th>Sl No.</th>
<th>Name</th>
</tr>
");
for($i=0; $i<500;$i++){
$mpdf->WriteHTML("<tr>");
$mpdf->WriteHTML("<td>".$res[$i]["slno"]."</td>");
$mpdf->WriteHTML("<td>".$res[$i]["name"]."</td>");
$mpdf->WriteHTML("</tr>");
}
$mpdf->WriteHTML("</table>");
Just use a <thead> tag. Outside the page header.
$mpdf->WriteHTML('
<table>
<thead>
<tr>
<th>Sl No.</th>
<th>Name</th>
</tr>
<thead>
...');
https://mpdf.github.io/tables/tables.html#repeating-table-header-row-on-new-page

Looping through json laravel

I have a table in which a column stores json data,but I am unable to loop through it in the view.
Sample Data :
[
{
"id":28,
"item_id":4,
"item_name":"Texco Dx",
"rate":"15.00",
"unit":"box",
"qty":"20",
"price":300,
"tax":12,
"taxType":"GST",
"tax_paid":36,
"disc":0,
"value":336,
"barcode":"1234ergeg",
"hsn":"q23423",
"lot_number":"1235r23r",
"checked":true,
"error":false,
"returnedQty":12,
"returnedValue":33.6,
"returnedTax":3.6,
"allowedQty":10
},
{
"id":27,
"item_id":3,
"item_name":"Norflox Tz",
"rate":"124.00",
"unit":"qty",
"qty":"10",
"price":1240,
"tax":10,
"taxType":"GST",
"tax_paid":124,
"disc":0,
"value":1364,
"barcode":"11121231",
"hsn":"123213",
"lot_number":"123",
"checked":true,
"error":false,
"returnedQty":3,
"returnedValue":272.8,
"returnedTax":24.8,
"allowedQty":9
}
]
This is how I am trying to loop through it :
<table>
<tr class="left">
<th>SR NO</th>
<th>ITEM NAME</th>
<th>PRICE/UNIT</th>
<th>QTY</th>
<th>DISCOUNT %</th>
<th>TAX</th>
<th>TOTAL</th>
</tr>
#foreach ($bill[0]->ITEM_DETAILS as $item)
<tr>
<td>{{$item->id}}</td>
<td>{{$item0->item_name}}</td>
</tr>
#endforeach
</table>
When I stored the same data using ng-init and tried iterating through it using ng-repeat it worked.
Can anyone explain this behaviour.
Thanks in advance
If you are getting a json in the view and assuming that $bill contains the json:
<table>
<tr class="left">
<th>SR NO</th>
<th>ITEM NAME</th>
<th>PRICE/UNIT</th>
<th>QTY</th>
<th>DISCOUNT %</th>
<th>TAX</th>
<th>TOTAL</th>
</tr>
#foreach (json_decode($bill, true) as $item)
<tr>
<td>{{$item['id']}}</td>
<td>{{$item['item_name']}}</td>
<td>{{$item['price']}}</td>
<td>...</td>
</tr>
#endforeach
</table>
If your json comes from the controller you could pass an array and do this.
In controller
$bills = ....
return view('your.template', ['bill' => json_decode($bills, true)]);
In view
<table>
<tr class="left">
<th>SR NO</th>
<th>ITEM NAME</th>
<th>PRICE/UNIT</th>
<th>QTY</th>
<th>DISCOUNT %</th>
<th>TAX</th>
<th>TOTAL</th>
</tr>
#foreach ($bill as $item)
<tr>
<td>{{$item['id']}}</td>
<td>{{$item['item_name']}}</td>
<td>{{$item['price']}}</td>
<td>...</td>
</tr>
#endforeach
</table>

Undefined property: Symfony\Component\HttpFoundation\ResponseHeaderBag (download report using dompdf )

I'm trying to download my html report into pdf file using dompdf, but I'm getting this error:
Undefined property: Symfony\Component\HttpFoundation\ResponseHeaderBag::$first_name
here's my controller code so far:
public function monthlypayrollProcess(Request $request)
{
$monthlypayrolls = Driver::join('driver_payables', 'driver_payables.driver_id', '=', 'drivers.id')
->join('driver_payable_details', 'driver_payable_details.driver_payable_id', '=', 'driver_payables.id')
->select(
'drivers.id',
'drivers.first_name',
'drivers.last_name',
'drivers.nric',
'drivers.join_date',
'drivers.basic_salary',
'drivers.uniform_size',
'driver_payables.total_working_hours',
'driver_payables.take_home_pay'
)
->where('driver_payables.payroll_period_id', $request->payroll_period_id)->get();
if ($monthlypayrolls->isEmpty()) {
return $this->sendErrorResponse($request, 'No Data');
}
$view = view('reporting.monthly_payroll.detail')->with(compact('monthlypayrolls'));
return parent::render($view, null, null);
}
public function downloadMonthlyPayrollReport(Request $request)
{
$monthlypayrolls = $this->monthlypayrollProcess($request);
$isPdf = true;
$view = 'reporting.monthly_payroll.pdfview';
$pdf = PDF::loadView($view, compact('monthlypayrolls', 'isPdf'))->setPaper('A4', 'landscape');
return $pdf->stream();
}
and here's my pdf view blade code so far :
<div class="table-responsive text-center">
<table class="table table-hover" id="datatable" style="width: 100%">
<thead>
<tr>
<th class="text-center">Driver Name</th>
<th class="text-center">NRIC</th>
<th class="text-center">Employment Type</th>
<th class="text-center">Hired Date</th>
<th class="text-center">Basic Salary</th>
<th class="text-center">Uniform</th>
<th class="text-center">Total Working Hours</th>
<th class="text-center">Take Home Pay</th>
</tr>
</thead>
<tbody>
#foreach($monthlypayrolls as $monthlypayroll )
<tr>
<td>{{$monthlypayroll->first_name }} {{ $monthlypayroll->last_name}}</td>
<td>{{$monthlypayroll->nric}}</td>
<td>{!! DriverEmployedType::getString($monthlypayroll->employed_type) !!}</td>
<td>{{$monthlypayroll->join_date}}</td>
<td>{{$monthlypayroll->basic_salary }}</td>
<td>{{$monthlypayroll->uniform_size}} {{$monthlypayroll->uniform_quantity}}</td>
<td>{{$monthlypayroll->total_working_hours}}</td>
<td>{{$monthlypayroll->take_home_pay}}</td>
</tr>
#endforeach
</tbody>
</table>
</div>
any idea ?
The monthlypayrollProcess() method returns a Response view, then you are assigning it to another variable
$monthlypayrolls = $this->monthlypayrollProcess($request);
Finally in your view
<td>{{$monthlypayroll->first_name }} {{ $monthlypayroll->last_name}}</td>
Something wrong with your logic, the method probably should return an object not a view. The method should be something like that:
public function monthlypayrollProcess(Request $request)
{
$monthlypayrolls = Driver::join('driver_payables', 'driver_payables.driver_id', '=', 'drivers.id')
->join('driver_payable_details', 'driver_payable_details.driver_payable_id', '=', 'driver_payables.id')
->select(
'drivers.id',
'drivers.first_name',
'drivers.last_name',
'drivers.nric',
'drivers.join_date',
'drivers.basic_salary',
'drivers.uniform_size',
'driver_payables.total_working_hours',
'driver_payables.take_home_pay'
)
->where('driver_payables.payroll_period_id', $request->payroll_period_id)->get();
if ($monthlypayrolls->isEmpty()) {
return $this->sendErrorResponse($request, 'No Data');
}
return $monthlypayrolls;
}
I had this very issue when trying to retrieve data as a response->json($myObject) and then iterating through it with a foreach with blade in the view.
I solved it by just returning the object "as is" (i.e. return $myObject instead of return response->json($myObject) ).
Hope this helps!

laravel-excel not working to export xls file using blade with image

Excuse me,
I want to use laravel-excel to export my report.
This is the controller:
public function getExcelfile(){
$users = UserService::findAllPublic();
$total = UserService::count();
$total_with_photo = UserService::countWithPhoto();
Excel::create('excelfile', function($excel) use ($users, $total, $total_with_photo) {
$excel->sheet('Excel', function($sheet) use ($users, $total, $total_with_photo) {
$sheet->loadView('report.excel')->with("users", $users)->with("total", $total)->with("total_with_photo", $total_with_photo);
});
})->export('xls');
}
This is the excel.blade.php located at report folder:
<html> <body> Total Registered User : {{$total}} Total Registered User With Photo : {{$total_with_photo}} <h1>Data User</h1> #if(!empty($users)) <table class="table table-striped table-bordered">
<thead>
<tr>
<th>ID</th>
<th>Photo</th>
<th>Nama</th>
<th>Struck</th>
<th>Email</th>
<th>Phone</th>
<th>FB ID</th>
<th>Timestamp</th>
<th>Publish</th>
</tr>
</thead>
#foreach ($users as $row)
<tr>
<td>{{$row->id}}</td>
<td><img src="{{URL::asset('assets/images/upload/' . $row->photo)}}" width="200px"/></td>
<td>{{$row->nama}}</td>
<td>{{$row->struck}}</td>
<td>{{$row->email}}</td>
<td>{{$row->phone}}</td>
<td>{{$row->fbid}}</td>
<td>{{$row->timestamp}}</td>
<td>{{$row->publish}}</td>
</tr>
#endforeach </table> #endif </body> </html>
Then this error appears:
PHPExcel_Exception
File http://myserver.com/projectname/public/assets/images/upload/DSCN1164.jpg not found!
That file is exist when I try to access it at browser.
Instead of:
<img src="{{URL::asset('assets/images/upload/' . $row->photo)}}"
you should use:
<img src="assets/images/upload/{{{ $row->photo }}}"
Of course assuming it's correct path

Categories