I am beginner in Laravel. I use in my project Laravel 5.8.
I have this code for generate Word file:
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$month = $request->input('month');
if ($month == null) {
$now = Carbon::now();
$month = $now->month;
}
$events = $this->frontendGateway->getEventCalendarDownload($request, $month);
$logo = public_path('assets/images/logo3.jpg');
$view_content = View::make('psCMS.prints.events-view', ['events' => $events, 'logo' => $logo])->render();
$section = $phpWord->addSection();
$text = $section->addText('aaaaaaaa');
$text = $section->addText('bbbbbbbbbb');
$text = $section->addText('ccccccccccc');
$text = $section->addText($view_content);
//ob_clean();
$fileName = 'Event_calendar' . '-' . now()->toDateString() . '.doc';
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save($fileName);
header('Content-Type: application/octet-stream');
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
header("Content-Disposition: attachment; filename=$fileName");
ob_clean();
readfile($fileName);
and this is my Blade file:
<div id="header" class="fontSize14">
<table width="100%">
<tr>
<td align="left" style="width: 20%;">
<img src="{{ $logo }}" class="logo" />
</td>
<td align="left" style="width: 80%;">
<span class="fontSize19"><b>my name</b></span><br />
street<br />
</td>
</tr>
</table>
</div>
<div id="content" class="fontSize11">
<b class="fontSize19">Kalendarz wydarzeń</b><br /><br />
<table width="100%">
<thead style="background-color: lightgray;">
<tr>
<th>#</th>
<th>Data</th>
<th>Godzina</th>
<th>Nazwa imprezy</th>
<th>Miejsce</th>
</tr>
</thead>
<tbody>
#foreach($events as $event)
#php
$hourFromX = explode(":", $event->hour_from);
$hourToX = explode(":", $event->hour_to);
$hourFrom = $hourFromX['0'].":".$hourFromX['1'];
$hourTo = $hourToX['0'].":".$hourToX['1'];
#endphp
<tr>
<th scope="row">{{ $loop->iteration }}</th>
<td>{{ $event->date_from }}</td>
<td align="left">{{ $hourFrom }}-{{ $hourTo }}</td>
<td align="left">{{ $event->title }}</td>
<td align="left">#if(isset($event->localization)) {{ $event->localization->name }},
{{ $event->localization->city }}
{{ $event->localization->street }} #endif</td>
</tr>
#endforeach
</tbody>
</table>
</div>
When I run this code, I have error: Word file is corrupted.
When I comment this line:
$text = $section->addText($view_content);
Word file is okey. Generated file was open in Word without any problems.
How can I repair it?
Can you try adding addHtml instead of addText
\PhpOffice\PhpWord\Shared\Html::addHtml($section, $view_content , false, false);
Related
I am trying to send an array per compact but when viewing it it throws an error.
SaleController.php
public function generarRecibo($id)
{
$sales = Sale::with('client', 'products')->where('id', '=', $id)->get();
$pdf = PDF::loadView('pdf.sales-recibo', compact(['sales']) );
return $pdf->stream('Recibo N° '.$id.'.pdf');
}
sales-recibo.blade.php
<table class="table">
<tbody>
#foreach ($sales->products as $product)
<tr>
<td style="border: 0px;" colspan="3">{{ $product->name }}</td>
</tr>
<tr>
<td style="border: 0px;">
<ul>
<li>{{ $product->quantity }} X ${{ number_format($product->price, 0,',','.') }}</li>
</ul>
</td>
<td style="border: 0px;" width="150px"></td>
<td style="border: 0px;" width="150px">${{ number_format($product->price, 0,',','.') }}</td>
</tr>
#endforeach
</tbody>
</table>
Instead of get() use find() method
$sales = Sale::with('client', 'products')->find($id);
and also you have issue with compact
$pdf = PDF::loadView('pdf.sales-recibo', compact('sales') );
Hi I am going to Export my html form to excel
Here is my code:
<script type="text/javascript">
function callexports()
{
var tabdata=document.getElementById("tabledata").innerHTML;
document.getElementById("tableval").value=tabdata;
document.getElementById("sendtable").submit();
}
</script>
<form id="sendtable" action="exportexcel.php" method="post">
<input type="button" name="Export Excel"
value="Export Excel" onclick="callexports();"/>
<div id="tabledata">
<table border='1'>
<tr >
<th style="background-color:yellow;">Sno</th>
<th style="background-color:yellow;">Name</th>
<th style="background-color:yellow;">Marks</th>
<th style="background-color:yellow;">Age : Age Testing </th>
</tr>
<tr><td>1</td><td>Anvesh</td><td>90</td></tr>
<tr><td>2</td><td>Kapil</td><td>80</td></tr>
<tr><td>3</td><td>Mahesh</td><td>70</td></tr>
<tr><td>4</td><td>Sravan</td><td>90</td></tr>
<tr><td colspan='2'>Total</td><td>330</td></tr>
</table>
</div>
<input type="hidden" name="tableval" id="tableval" />
</form>
**exportexcel.php**
<?php
$filename = "Student_Info".time(); //File Name
$file_ending = "xls";
//header info for browser
header("Content-Type: application/xls");
header("Content-Disposition: attachment; filename=$filename.xls");
header("Pragma: no-cache");
header("Expires: 0");
Echo $_REQUEST['tableval'];
?>
Now it's working fine.But i have a doubt
<th style="background-color:yellow;">Age : Age new Testing </th>
Here Age is My heading and Age new Testing is my description. Is there any way to remove description from <th>. I mean i need only Heading and i want to remove description? Please help me?
i have some coding, i wanna to update multiple data from select option value in database using laravel 5.1. i'm using ajax onchange="form.submit()", so i can update data without submit.
this is my view
{!! Form::model($UserAccess,['method' => 'POST','url'=>['setting/updaterole']]) !!}
<table class="dataTable" id="table-user">
<thead class="grey lighten-3">
<tr>
<td class="center-align no-sort">Photo</td>
<td class="center-align">Fullname</td>
<td class="center-align">Rule</td>
<td class="center-align">Action</td>
</tr>
</thead>
<tbody>
<?php $hitung = $UserAccess->count(); ?>
#foreach($UserAccess as $list)
<tr>
<td class="center-align" width="50">
#if($list->avatar == NULL)
<img class="circle responsive-img" width="50" src="{{asset(config('param.url_uploads').'blank.jpg')}}"/>
#else
<img class="circle responsive-img" width="50" src="{{$list->avatar}}"/>
#endif
</td>
<td class="red1-text lato-bold center-align">{{$list->name}}</td>
<td class="center-align" width="150">
<select id="selectrole" name="selectrole" onchange="form.submit()">
#foreach($UserAccessRole as $listRole)
<option value="{{$listRole->id}}" #if($list->user_access_role_id_fk == $listRole->id) selected="selected"#endif>{{$listRole->name}}</option>
#endforeach
</select>
</td>
<input type = "hidden" value = "{{$list->id}}" name = "idmain">
#if($hitung > 2)
<td class="center-align">
<a href = "remove_access/{{$list->id}}/delete" >Remove Access<a/>
</td>
#else
<td class="center-align">Remove Access</td>
#endif
</tr>
#endforeach
</tbody>
</table>
{!! Form::close() !!}
this is my controller
public function doUpdateAccessRole(Request $request)
{
$UserAccess = UserAccess::orderBy('name', 'asc')->get();
$id_main = $request->input('idmain');
$id_role = $request->input('selectrole');
$Role = UserAccess::findOrFail($id_main);
$Role->user_access_role_id_fk = $id_role;
$Role->update($request->all());
return redirect('setting/useraccess');
}
this is my route
Route::post('setting/updaterole',['uses'=>'SettingController#doUpdateAccessRole','as'=>'updateaccessrole']);
using my coding update function already work, but just last id has been update. please help me, thank you
my code already work. just add foreach in my controller. like this, my controller :
public function doUpdateAccessRole(Request $request)
{
$UserAccess = UserAccess::all();
foreach($UserAccess as $list)
{
$id_main = $request->input('idmain'.$list->id);
$id_role = $request->input('selectrole'.$list->id);
$Role = UserAccess::find($id_main);
$Role->user_access_role_id_fk = $id_role;
$Role->update($request->all());
}
return redirect('setting/useraccess');
}
and this my view :
{!! Form::model($UserAccess,['method' => 'POST','url'=>['setting/updaterole']]) !!}
<table class="dataTable" id="table-user">
<thead class="grey lighten-3">
<tr>
<td class="center-align no-sort">Photo</td>
<td class="center-align">Fullname</td>
<td class="center-align">Rule</td>
<td class="center-align">Action</td>
</tr>
</thead>
<tbody>
<?php $hitung = $UserAccess->count(); ?>
#foreach($UserAccess as $list)
<tr>
<td class="center-align" width="50">
#if($list->avatar == NULL)
<img class="circle responsive-img" width="50" src="{{asset(config('param.url_uploads').'blank.jpg')}}"/>
#else
<img class="circle responsive-img" width="50" src="{{$list->avatar}}"/>
#endif
</td>
<td class="red1-text lato-bold center-align">{{$list->name}}</td>
<td class="center-align" width="150">
<select id="selectrole" name="selectrole{{$list->id}}" onchange="form.submit()">
#foreach($UserAccessRole as $listRole)
<option value="{{$listRole->id}}" #if($list->user_access_role_id_fk == $listRole->id) selected="selected"#endif>{{$listRole->name}}</option>
#endforeach
</select>
</td>
<input type = "hidden" value = "{{$list->id}}" name = "idmain{{$list->id}}">
#if($hitung > 2)
<td class="center-align">
<a href = "remove_access/{{$list->id}}/delete" >Remove Access<a/>
</td>
#else
<td class="center-align">Remove Access</td>
#endif
</tr>
#endforeach
</tbody>
</table>
{!! Form::close() !!}
and now my coding already work.
I'm building an invoice system, first you'll have to fill in the products you want to have on your invoice then it will convert the html view(where it will show the products in a foreach loop) to pdf. But if I have more than 6 products it will throw the following exception:
Frame not found in cellmap
I know this question has been asked a lot. But it seems there is still no real solution. It occurs when the input on the page is bigger than the first page and it can't jump to a new page so it throws an error:
Already tried a lottt!
This won't work for me:
$paper_orientation = 'landscape';
$customPaper = array(0,0,950,950);
$dompdf->set_paper($customPaper,$paper_orientation);
https://github.com/dompdf/dompdf/issues/657
This is my html view:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Factuur</title>
<link rel="stylesheet" href="/administration/style.css" media="all" />
</head>
<body>
<header class="clearfix">
<img src="/administration/logo.png">
<div id="company">
<h2 class="name">Company name</h2>
<div>Address</div>
<div>zip</div>
<div>email</div>
</div>
</div>
</header>
<main>
<div id="details" class="clearfix">
<div id="client">
<div class="to">{{ $sort == 0 ? 'Offerte' : 'Factuur' }} voor:</div>
<h2 class="name">{{ $company }}</h2>
<div class="address">{{ $address }}</div>
<div class="address">{{ $zip }}</a></div>
</div>
<div>
<h1>{{ $sort == 0 ? 'Offerte' : 'Factuur' }}nummer: {{ $number }}</h1>
<div class="date">Datum: {{ $date }}</div>
</div>
</div>
<table border="0" cellspacing="0" cellpadding="0">
<thead>
<tr>
<th class="no">#</th>
<th class="desc">Beschrijving</th>
<th class="unit">Prijs</th>
<th class="qty">Aantal</th>
<th class="total">Totaal</th>
</tr>
</thead>
<tbody>
#foreach($products as $product)
<tr>
<td class="no">{{ $product->id }}</td>
<td class="desc"><h3>{{ $product->title }}</h3>{{ $product->description }}</td>
<td class="unit">€{{ $product->price }}</td>
<td class="qty">{{ $product->number }}</td>
<td class="total">€{{ $product->total }}</td>
</tr>
#endforeach
</tbody>
<tfoot>
<tr>
<td colspan="2"></td>
<td colspan="2">SUBTOTAAL</td>
<td>€ {{ $subtotal }}</td>
</tr>
<tr>
<td colspan="2"></td>
<td colspan="2">BTW 21%</td>
<td>€ {{ $btw }}</td>
</tr>
<tr>
<td colspan="2"></td>
<td colspan="2">TOTAAL</td>
<td>€ {{ $total }}</td>
</tr>
</tfoot>
</table>
<div id="notices" class="{{ $sort == 0 ? 'hidePayment' : 'notices' }}">
<div>BETALING:</div>
</div>
</main>
<footer>
KvK nummer: | BTW nummer:
</footer>
</body>
</html>
Is there a solution?
I have a large single pdf document which consists of multiple records.I need to break or split the table records into second pages in the pdf using laravel.
Using the below codings in the controller the table records are not fully displayed.Some table records are missing in that pdf file.so I need to show the missing table records into second pafe of the pdf file.
private function generatePdf($customerstatement, $content) {
if (!is_dir(RECEIPTS_DIR)){
mkdir(RECEIPTS_DIR, 0755, true);
}
$outputName = $customerstatement->card_id . '-' . strtoupper(date("MY")) .'.pdf';
$pdfFileName = RECEIPTS_DIR.'/' . $outputName;
$view = View::make("admin.document.statement", compact('customerstatement', 'content'))->render();
$pdf = new \Thujohn\Pdf\Pdf();
$bytes_written = File::put($pdfFileName, $pdf->load($view, 'A4', 'portrait')->output());
if ($bytes_written === false) {
throw new \Exception("Unable to produce pdf statement");
}
$customerstatement->pdffile = $outputName;
$customerstatement->save();
return $outputName;
}
The below codings are written in the blade file for showing the records in the pdf.
<tbody>
<tr>
<td colspan="7"><b>{{ $customerstatement->customer->card_id }} - {{ $customerstatement->customer->fullName }} </b>
</td>
</tr>
<tr>
<td colspan="7"><b>Beginning Balance : {{ $customerstatement->beginning_balance }}</b>
</td>
</tr>
#foreach ($customerstatement->statements as $statement)
<tr>
<td width="7em">{{{ $statement->date }}}</td>
<td width="15em">{{{ $statement->memo }}}</td>
<td width="6em" align="center">{{{ $statement->debit }}}</td>
<td width="6em" align="center">{{{ $statement->credit}}}</td>
<td width="6em" align="center">{{{ $statement->closing_balance}}}</td>
</tr>
#endforeach
</tbody>
<tr>
<td colspan="7"><b>
{{ $customerstatement->customer->card_id }} - {{ $customerstatement->customer->fullName }} </b></td>
</tr>
<tr><td colspan="7"><b>Beginning Balance : {{ $customerstatement->beginning_balance }}</b></td></tr>
<?php $i=0 ?>
#foreach ($customerstatement->statements as $statement)
<?php $i++ ?>
<tr>
<td width="7em">{{{ date("d-m-Y", strtotime($statement->date) )}}}</td>
<td width="15em">{{{ $statement->memo }}}</td>
<td width="6em" align="center">{{{ $statement->debit }}}</td>
<td width="6em" align="center">{{{ $statement->credit}}}</td>
<td width="6em" align="center">{{{ $statement->closing_balance}}}</td>
<td ><?php
if($i==25){ ?>
<?php $i=0 ?>
<div style="page-break-after: always;"></div>
<?php }
?></td>
</tr>