I have two views, first view is use to collect user input ( user select site and date) the second view is pdf view(will show in browser). The problem now is when user select the input(site and date) from first view and click on a button called "convert button", controller and second view(PDF) cannot get the data.
try many times still cannot get the filter data to display on pdf view
<select name="sites" id="sites" required="required" style="border:1px solid black; margin-left:10px; width:200px; padding:3px; font-size:16px; text-align-last:center;" >
<option value="" selected="" disabled > </option>
<option value="BBSR">ABC</option>
<option value="BPSR">BBC</option>
<option value="BKSR">BBE</option>
<option value="FCSR">QWE</option>
</select>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label style="margin-left:20px; font-size:16px;">Start Date : </label>
<input type = "text" name="startdate" id = "datepicker-8" style="border:1px solid black; width:270px;margin-left:47px; text-align:center; font-size:16px;" placeholder="Select Start Date" autocomplete="off" required="required" />
</div>
</div>
<div class='col-md-6'>
<div class="form-group">
<label style="margin-left:20px;font-size:16px;">End Date : </label>
<input type = "text" name="enddate" id = "datepicker-9" style="width:270px; margin-left:53px; text-align:center; font-size:16px;" placeholder=" End Date" disabled />
</div>
</div>
</div>
button :
<div class="button" style="bottom:-25px">
<button class="btn btn-success" id="convertBtn" name="convertBtn" type="button" style="width:150px; margin-left:60px; font-size:16px;" onclick="location.href = '/home/weeklybilling/pdf';" > Convert to PDF</button>
</div>
</div>
ajax:
$('#convertBtn').click(function() {
var e = document.getElementById("sites");
var site = e.options[e.selectedIndex].text;
var startdate = $('#datepicker-8').val();
var enddate = $('#datepicker-9').val();
var _token =$('input[name="_token"]').val();
if(site != '' && startdate != '' && enddate != '' )
{
$.ajax({
paging: false,
searching: false,
processing: true,
retrieve: true,
serverSide: true,
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
url:"{{ route('weeklybilling.weeklypdf') }}",
type:"GET",
dataType:"json",
data:{
site:site,
startdate:startdate,
enddate:enddate,
_token:_token,
},
success:function(data){
},
error: function(data){
alert('Error');
}
})
} else
{
alert("Please select 'Site' & 'Date'. ");
}
});
Route
Route::get('/home/weeklybilling','WeeklyBillingController@Index');
Route::get('/home/weeklybilling/pdf','WeeklyBillingController@weeklybill')->name('weeklybilling.weeklybill');
controller
public function weeklypdf(Request $request)
{
$bills = DB::table('weekly_data')
->where('site', $request->site)
->whereBetween('report_date',[$request->startdate ,$request->enddate])
->get();
//dd($bills); get null
$pdf = \App::make('dompdf.wrapper');
$pdf =PDF::loadView('weeklypdf',compact('bills'));
$pdf ->setPaper('a4','landscape');
return $pdf->stream('weeklyreport.pdf');
}
Anyone know how to know how to get input data from first view and pass it to second view(pdf)? There is no datatable in pdf view**
If you face the same problem as me. Here is the thing u can try.
$.ajax({
type: "POST",
url: url,
data: params,
xhrFields: {
responseType: 'blob' // to avoid binary data being mangled on charset conversion
},
success: function(blob, status, xhr) {
// check for a filename
var filename = "";
var disposition = xhr.getResponseHeader('Content-Disposition');
if (disposition && disposition.indexOf('attachment') !== -1) {
var filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
var matches = filenameRegex.exec(disposition);
if (matches != null && matches[1]) filename = matches[1].replace(/['"]/g, '');
}
if (typeof window.navigator.msSaveBlob !== 'undefined') {
// IE workaround for "HTML7007: One or more blob URLs were revoked by closing the blob for which they were created. These URLs will no longer resolve as the data backing the URL has been freed."
window.navigator.msSaveBlob(blob, filename);
} else {
var URL = window.URL || window.webkitURL;
var downloadUrl = URL.createObjectURL(blob);
if (filename) {
// use HTML5 a[download] attribute to specify filename
var a = document.createElement("a");
// safari doesn't support this yet
if (typeof a.download === 'undefined') {
window.location.href = downloadUrl;
} else {
a.href = downloadUrl;
a.download = filename;
document.body.appendChild(a);
a.click();
}
} else {
window.location.href = downloadUrl;
}
setTimeout(function () { URL.revokeObjectURL(downloadUrl); }, 100); // cleanup
}
}
});
It helps me to pass data to pdf view but my problem now is it cannot be downloaded.