I'm trying to write code that converts html blade to PDF using tcpdf in laravel.
This is the controller code which i use to convert:
public function htmlToPDF()
{
$view=view('PdfDemo');
$html_content =$view->render();
PDF::SetTitle('Sample PDF');
PDF::AddPage();
PDF::writeHTML($html_content, true, false, true, false, '');
PDF::Output('SamplePDF.pdf');
}
When I put static data on my view(PdfDemo) everything runs successfully, but the problem is when I put dynamic data with some variables in the blade as the following:
<table>
<?php $i=0?>
@if(count($names))
@foreach($names as $n)
<tr><td>
<label>Name{{$i}}:</label>{{$n}}</td>
<tr><td><hr></td></tr>
<?php $index = $index+1; ?>
@endforeach
@endif
</table>
Here $names is the result of select statement in other function of controller, then I pass it to the blade like this:
return view('PdfDemo',compact('names'));
In this case the blade appear exactly as I want, but when I try to convert to PDF it shows error, $name not defined.