I am trying to send an HTML template to MailTrap using this method
public function send($result_id)
{
$result = Result::whereHas('user', function ($query) {
$query->whereId(auth()->id());
})->findOrFail($result_id);
\Mail::to('test#eam.com')->send(new ResultMail);
return view('client.result', compact('result'))->withStatus('Your test result has been sent successfully!');
}
with the result.blade.file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Test No. {{ $result->id }}</title>
<link href="{{ asset('css/app.css') }}" rel="stylesheet" type="text/css" />
<style type="text/css">
html {
margin: 0;
}
body {
background-color: #FFFFFF;
font-size: 10px;
margin: 36pt;
}
</style>
</head>
<body>
<p class="mt-5">Total points: {{ $result->total_points }} points</p>
<table class="table table-bordered">
<thead>
<tr>
<th>Question Text</th>
<th>Points</th>
</tr>
</thead>
<tbody>
#foreach($result->questions as $question)
<tr>
<td>{{ $question->question_text }}</td>
<td>{{ $question->pivot->points }}</td>
</tr>
#endforeach
</tbody>
</table>
</body>
</html>
but I'm get an error
Undefined variable: result (View: C:\Users\USER\Documents\Laravel-Test-Result-PDF-master\Laravel-Test-Result-PDF-master\resources\views\client\result.blade.php)
Well, as far as I know, you need to have Mailable class and from the mailable class, you need to return the view and pass the data there.
You'r mailable class should
class ResultMail extends Mailable
{
use Queueable, SerializesModels;
public $result;
/**
* Create a new message instance.
*
*/
public function __construct($result)
{
$this->result = $result;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->view('client.result');
}
}
Should be something like this.And then you need to pass the data to ResultMail
\Mail::to('test#eam.com')->send(new ResultMail($result));
Related
I am using laravel framework. here in my code HTML table view is ok.But when i am converting the same to pdf using mpdf, the table is displaying without any borders. i am using css in same html. no external style sheets are included
my code is here
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.0/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-KyZXEAg3QhqLMpG8r+8fhAXLRk2vvoC2f3B09zVXn8CA5QIVfZOJ3BCsw2P0p/We" crossorigin="anonymous">
<style>
td{
border-top: 1px dotted black;
border-bottom: 1px dotted black;
border-left: 1px dotted black;
border-right: 1px dotted black;
}
table {
margin-left: 5px;
border: 1px solid black;
}
td {
border: 1px solid black;
}
</style>
<title>Document</title>
</head>
<body>
<table class="pdf">
<thead>
<tr>
<th>Name</th>
<th>Phone</th>
<th>Password</th>
<th>Role ID</th>
</tr>
</thead>
<tbody style="font-size: 14px;">
#foreach ($users as $user)
<tr>
<td>{{ $user->name }}</td>
<td>{{ $user->mobile }}</td>
<td style="width: 20%;">{{ $user->password }}</td>
<td>{{ $user->department_id }}</td>
<td>{{ $user->i }}</td>
</tr>
#endforeach
</tbody>
</table>
<script src="https://cdn.jsdelivr.net/npm/#popperjs/core#2.9.3/dist/umd/popper.min.js"
integrity="sha384-eMNCOe7tC1doHpGoWe/6oMVemdAVTMs2xqW4mwXrXsW0L84Iytr2wi5v2QjrP/xp" crossorigin="anonymous">
</script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.1.0/dist/js/bootstrap.min.js"
integrity="sha384-cn7l7gDp0eyniUwwAZgrzD06kc/tftFf19TOAs2zVinnD/C7E91j9yyk5//jjpt/" crossorigin="anonymous">
</script>
</body>
</html>
Here is my controller
<?php
namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Http\Request;
use Mpdf\Mpdf;
use Illuminate\Support\Facades\View;
// use phpDocumentor\Reflection\PseudoTypes\False_;
class PdfController extends Controller
{
public function index()
{
$users = User::all();
return view('pdf',compact('users'));
}
public function generate()
{
require_once base_path('vendor/autoload.php');
$defaultConfig = (new \Mpdf\Config\ConfigVariables())->getDefaults();
$fontDirs = $defaultConfig['fontDir'];
$defaultFontConfig = (new \Mpdf\Config\FontVariables())->getDefaults();
$fontData = $defaultFontConfig['fontdata'];
$custom_font_path = storage_path().'\fonts';
// dd($custom_font_path);
$mpdf = new \Mpdf\Mpdf([
'fontDir' => array_merge($fontDirs, [
$custom_font_path,
]),
'fontdata' => $fontData + [
'gayathri' => [
'R' => 'Gayathri-Regular.ttf',
'useOTL' => 0xFF,
]
],
'default_font' => 'gayathri'
]);
$mpdf->SetFont('dejavusanscondensed');
$mpdf->simpleTables=true;
// $mpdf->packTableData=true;
// $mpdf->keep_table_proportions=TRUE;
// $mpdf->shrink_tables_to_fit=1;
// $users = User::all();
// foreach($users as $user)
// {
// $mpdf->WriteHTML('<p style="font-size:28px;">'. $user->name .'</p> ');
// }
// calculate two numbers
$users = User::all();
$view = View::make('pdf', compact('users'));
$mpdf->WriteHTML($view);
$mpdf->Output();
}
}
In the generated pdf there is no borders but cell divisions and other layouts are ok, how to include borders for the entire table.( including cells, colums etc)
Don't use the style tag. Try to use stylesheet instead:
$stylesheet = file_get_contents('css/style.css');
//CSS
$mpdf->WriteHTML($stylesheet, \Mpdf\HTMLParserMode::HEADER_CSS);//This is css/style only and no body/html/text
//BODY
$mpdf->WriteHTML($html,\Mpdf\HTMLParserMode::HTML_BODY);//This is html content, without <head> information.
Check out this documentation.
Until now that I am dropping some lines here, there is still something wrong with MPDF when dealing with table borders.
When you style <td> and add border there, what you can see in the output is just border left and border right, and no border top and border down. For me, so as to bypass the problem, I styled both and to get all the borders working, that is top, bottom, right, and left. I just shared my solution in case someone would find it useful.
table td, table th, table tr {
border: 1px solid #c9c9c9 !important
}
Since I am completely new in Laravel, I have a problem pretty complicated for me.
I have controller file ApiHandlerController like this:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use GuzzleHttp\Client;
class ApiHandlerController extends Controller
{
//
public function index()
{
$client = new Client([
// Base URI is used with relative requests
'base_uri' => 'http://jsonplaceholder.typicode.com/todos',
]);
$response = $client->request('GET', '/todos');
//get status code using $response->getStatusCode();
$body = $response->getBody();
$arr_body = json_decode($body);
return view('myview', $arr_body);
}
}
?>
myview.blade.php as a view file:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Todo</title>
<link rel="shortcut icon" type="image/x-icon" href="icon.ico">
<meta name="description" content="HTML, PHP, JSON, REST API">
<style>table, th, td {border: 1px solid black;}</style>
</head>
<body>
<table style="width:100%"; class="data-table">
<tr>
<th>User ID:</th>
<th>ID:</th>
<th>Title:</th>
<th>Completed</th>
</tr>
<?php foreach ($arr_body as $value) { ?>
<tr>
<td><?php echo $value -> userId ?> </td>
<td><?php echo $value -> id ?> </td>
<td><?php echo $value -> title ?> </td>
<td><?php echo $value -> completed ?> </td>
</tr>
<?php } ?>
</table>
And web.php file with route:
Route::get('myview', function () {
return view('myview');
});
My question is:
How to show result of todos from jsonplaceholder.typicode.com in this blade (view) file?
Currently, it's not possible, and every time I try this is an error:
ErrorException
Undefined variable: arr_body (View: C:\xampp\htdocs\TodoList\resources\views\myview.blade.php)
Thank you guys in advance!
To show array type data in blade...
First edit your controller code.
From:
return view('myview', $arr_body);
To:
return view('myview')->with('arr_body', $arr_body);
Below is core php syntex.
<td><?php echo $value['userId'] ?> </td>
Laravel syntex
<td>{{$value['userId']}}</td>
Hope this will be useful.
why is my queue job timing out? I am using database as a driver I tried the following:
class PdfGenerator implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $userData;
protected $filename;
protected $path;
public $timeout = 1200;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct($userData, $filename)
{
//
$this->userData = $userData;
$this->filename = $filename;
$this->path = \public_path('\\pdfs\\'.$this->filename.'.pdf');
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
$pdf = App::make('snappy.pdf.wrapper');
$footer = \view('supporting.footer')->render();
$header = \view('supporting.header')->render();
//$userData = \collect([$this->userData[1]]);
$pdf->loadView('order_clean', ['users' => $this->userData])
->setOption('margin-top', '20mm')
->setOption('margin-bottom', '20mm')
->setOption('minimum-font-size', 25)
->setOption('header-html', $header)
->setOption('footer-html', $footer);
$pdf->save($this->path);
}
}
php artisan queue:work --timeout=1200
php artisan queue:listen --timeout=0
yet my queue job still fails due to timeout of 60s according to the logs because the symfony process timed out. I am not using supervisor yet, just trying out how the queue works from the console
edit:: controller code + blade code
controller code:
class OrderController extends Controller
{
public function renderPDF()
{
$user_data = $this->getUserData();
$filename = 'test '.\now()->timestamp;
//no timeouts here
//if not ran on queue and with set_time_limit, takes around 70s
//at current data size
$this->dispatch(new PdfGenerator($user_data,$filename));
//view returns successfully
return \view('test.test', ['filename'=>$filename]);
}
}
Blade file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Order Layout</title>
<style>
*{
font-family: cursive;
}
.wrapper {
display: block;
}
.invoice {
display: block;
width: 80%;
margin: 0 auto;
margin-top: 10px;
padding-top: 10px;
padding-bottom: 10px;
background: #d3d3d3;
page-break-inside: avoid !important;
padding-left: 20px;
}
.order-items {
padding-left: 100px;
}
.table {
width: 90%;
align-self: center;
border: 1px solid black;
orphans: 15;
}
.table>* {
text-align: center;
}
</style>
</head>
<body>
<main class="wrapper">
#foreach ($users as $user)
#php
$orders = $user->orders //just for renaming
#endphp
#foreach ($orders as $order)
<div class="invoice">
#php
$sum=0;
#endphp
<h2>{{$user->name}}: {{$order->id}}</h2>
<div class="order-items">
<table class="table" border="1">
<thead>
<tr>
<th>Product Name</th>
<th>Unit Price</th>
<th>Qty</th>
<th>subtotal</th>
</tr>
</thead>
<tbody>
#foreach ($order->products as $product)
<tr>
<th>
{{$product->name}}<br>
{{$product->name}}<br>
{{$product->name}}<br>
{{$product->name}}<br>
</th>
<td>{{$product->unit_price}}</td>
<td>{{$product->pivot->quantity}}</td>
#php
$sum+= $product->pivot->quantity*$product->unit_price
#endphp
<td>{{$product->pivot->quantity*$product->unit_price}}</td>
</tr>
#endforeach
</tbody>
<tfoot>
<tr>
<th colspan="3">Total:</th>
<td>{{$sum}}</td>
</tr>
</tfoot>
</table>
</div>
</div>
#endforeach
#endforeach
</main>
</body>
</html>
If you want to be able to set the timeouts you should make sure you have the pcntl PHP extension installed and enabled.
"The pcntl PHP extension must be installed in order to specify job timeouts."
Laravel 8.x Docs - Queues - Dispatching Jobs - Specifying Max Job Attempts / Timeout Values - Timeout
In my education resources controller system, we have to prepare progress reports for student.To get these reports as a pdf I used Laravel dom-pdf.
code of pdfGeneratorController
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\User;
use App\Subject;
use PDF;
class pdfGenerator extends Controller
{
public function reportPdf()
{
$users = DB::table('users')->get();
$subjects = DB::table('subjects')->get();
$pdf = PDF::loadview('reports', ['users' => $users],['subjects' => $subjects]);
// $pdf = PDF::loadview('reports', ['subjects' => $subjects]);
return $pdf->download('report.pdf');
}
public function report()
{
// $users = DB::table('users')->get();
$users = DB::table('users')
->join('subjects','users.id','=','subjects.user_id')
->join('marks','subjects.user_id','=','subjects.user_id')
->select('users.*','subjects.subject','marks.marks')
->get();
//$subject = Subject::all();
// return $subject;
return view('reports', ['users' => $users]);
}
}
codes of reports.blade.php
<!DOCTYPE html>
<html>
<head>
<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
</style>
</head>
<body>
<h1><a href="{{url('/pdfs')}}" > Download pdf</a></h1>
<table>
<caption>Student Details</caption>
<tr>
<td>Name</td>
<td> lname </td>
<td>subject</td>
<td>marks</td>
</tr>
#foreach($users as $user)
<tr>
<td>{{ $user->name}}</td>
<td>{{ $user->lname}}</td>
<td>{{ $user->subject}}</td>
<td>{{ $user->marks}}</td>
#endforeach
</tr>
</table>
</body>
</html>
When I click reports link it shows all the relevant data webpage. but when i click print pdf link it gives a error.the error of this time is ErrorException
Undefined property: stdClass::$subjects (View: E:\nimnayawebsite\resources\views\reports.blade.php)
You are passing two array with comma separated value, pass it making one array.
Change this line-
$pdf = PDF::loadview('reports', ['users' => $users],['subjects' => $subjects]);
To this-
$pdf = PDF::loadview('reports', ['users' => $users,'subjects' => $subjects]);
You're trying yo use $user->subject in the view. Since report() method works for you with the same view, change this:
$users = DB::table('users')->get();
$subjects = DB::table('subjects')->get();
To:
$users = DB::table('users')
->join('subjects','users.id','=','subjects.user_id')
->join('marks','subjects.user_id','=','subjects.user_id')
->select('users.*','subjects.subject','marks.marks')
->get();
I've a problem about this illegal string offset
This is the controller named formlembur.php
function download($lid)
{
$form_lembur = $this->form_lembur->read(array('lid' => $lid));
$data['form_lembur'] = $form_lembur;
//echo $data;die;(until this statement,it can load the data on database.
//it actually showing the data.
$this->load->view('exp-lembur', $data);
}
This is the model named form_lembur.php
public function read($where = FALSE)
{
if ($where === FALSE)
{
$query = $this->db->get('form_lembur');
return $query->result_array();
}
$query = $this->db->get_where('form_lembur', $where);
return $query->row_array();
}
and this is the view before i download
<td> <input type="submit" name="download" value="Download"></td>
and this is the view where i want the data viewed on excel named exp-lembur.php
<?php
header("Content-type: application/vnd.openxmlformats xlsx");
header("Content-disposition: attachment; filename=\"Lembur.xls\"");
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv=Content-Type content="text/html; charset=us-ascii">
<meta name=ProgId content=Excel.Sheet>
<title>Reports</title>
<style>
table.member
{
border:.5pt solid #000;
border-collapse:collapse;
font:14px tahoma;
margin:0 auto;
width:50%
}
table.member tr th{background-color:#999;border:.5pt solid #000}
table.member tr.head th{color:#fff}
table.member tr.data td{border:.5pt solid #000; vertical-align: top}
</style>
</head>
<body>
<?php
//print 'Generated On ' .date("j M y, g:i a",time()). '<br>';
?>
<table class="member">
<thead>
<tr class="head">
<th>Tanggal</th>
<th>Nama</th>
<th>Jabatan</th>
<th>Jam Pulang</th>
<th>Keperluan</th>
<th>Instruktur</th>
<th>Status</th>
<th>Uang Lembur</th>
</tr>
</thead>
<tbody>
<?php
if (isset($form_lembur)){
foreach($form_lembur as $k){
print <<<h
<tr class="data">
<td>{$k['date']}</td>
<td>{$k['nama']}</td>
<td>{$k['jabatan']}</td>
<td>{$k['jampulang']}</td>
<td>{$k['keperluan']}</td>
<td>{$k['instruktur']}</td>
<td>{$k['status']}</td>
<td>{$k['uanglembur']}</td>
</tr>
h;
}
}
?>
</tbody>
</table>
</body>
</html>
but the problem is, it said that its an illegal string offset.
what is the problem ?
Please help me.
this is the error message ( showed on excel document because i try to download it into an excel document )
A PHP Error was encountered
Severity: Warning
Message: Illegal string offset 'date'
Filename: views/exp-lembur.php
Line Number: 50
The problem is in the model. It sometimes returns
$query->result_array()
and other time returns
$query->row_array()
depends on $where value. But result_array() gives this format:
$row[0]['date'];
when row_array() gives this format:
$row['date'];
That's why you have "Illegal string offset 'date'".