Commit 761cc9a0 authored by Joshua Arosco's avatar Joshua Arosco

Create save and download functionality on sample PDF download

parent fb7117b8
/node_modules /node_modules
/public/hot /public/pdf
/public/storage /public/storage
/storage/*.key /storage/*.key
/vendor /vendor
......
...@@ -7,7 +7,8 @@ use App\Http\Controllers\Controller; ...@@ -7,7 +7,8 @@ use App\Http\Controllers\Controller;
use App\Models\DummyData as DummyDataModel; use App\Models\DummyData as DummyDataModel;
use App\Models\ElementsDensity as ElementsDensityModel; use App\Models\ElementsDensity as ElementsDensityModel;
use App\Models\PetPopularity as PetPopularityModel; use App\Models\PetPopularity as PetPopularityModel;
use PDF, Carbon, App, DB, Str; use Illuminate\Http\Response;
use PDF, Carbon, App, DB, Str, ZipArchive, File;
class PdfDownloadController extends Controller class PdfDownloadController extends Controller
{ {
...@@ -21,56 +22,105 @@ class PdfDownloadController extends Controller ...@@ -21,56 +22,105 @@ class PdfDownloadController extends Controller
public function index(){ public function index(){
$this->data['elements'] = $this->get_elements_data(); $this->data['elements'] = $this->get_elements_data();
$this->data['pet_popularity'] = $this->get_pet_popularity(); $this->data['pet_popularity'] = $this->get_pet_popularity();
return view('frontend.pdf_download',$this->data); return view('frontend.pdf_download',$this->data);
} }
public function download(Request $request){ public function download(Request $request){
$this->data['bar_chart'] = $request->bar_chart; $this->createFolder();
$this->data['line_chart'] = $request->line_chart; $files = [];
$this->data['dummy_data'] = $this->dummyDataModel->first(); foreach ($request->bar_chart as $key => $value) {
$pdf = App::make('dompdf.wrapper'); $this->data['bar_chart'] = $request->bar_chart[$key];
$this->data['line_chart'] = $request->line_chart[$key];
//set the html view to load $pdf = new PDF();
$pdf->loadView('domPdf.sample',$this->data); $pdf = App::make('dompdf.wrapper');
//set the html view to load
$pdf->loadView('domPdf.sample',$this->data)->save( public_path('pdf/sample-pdf-'.$key.'-'.date('Ymd').'.pdf') );
$filePath = public_path('pdf/sample-pdf-'.$key.'-'.date('Ymd').'.pdf');
// $pdf->download('sample-pdf-'.date('Ymd').'.pdf');
array_push($files,$filePath);
}
$zipname = 'sample-pdf-download-'.date('Ymd').'.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($files as $key => $file) {
$zip->addFile($file,'sample-pdf-'.$key.'-'.date('Ymd').'.pdf');
}
$zip->close();
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipname);
header('Content-Length: ' . filesize($zipname));
readfile($zipname);
// set the pdf file name // set the pdf file name
// use $pdf->download to download the pdf or use $pdf->stream to display the pdf on your browser // use $pdf->download to download the pdf or use $pdf->stream to display the pdf on your browser
return $pdf->download('sample-pdf-'.date('Ymd').'.pdf');
// return $pdf->stream('sample-pdf-'.date('Ymd').'.pdf'); // return $pdf->stream('sample-pdf-'.date('Ymd').'.pdf');
return redirect()->back(); return redirect()->back();
} }
private function getMonths($type){
switch ($type) {
case 'elements':
return $this->elementsDensityModel->orderBy('created_at','desc')
->get()
->groupBy(function (ElementsDensityModel $item) {
return $item->created_at->format('Y-m');
});
break;
default:
return $this->petPopularityModel->orderBy('created_at','desc')
->get()
->groupBy(function (PetPopularityModel $item) {
return $item->created_at->format('Y-m');
});
break;
}
}
private function get_elements_data(){ private function get_elements_data(){
//the code below depends on how you will represent your data based on your data structure //the code below depends on how you will represent your data based on your data structure
$elements = $this->elementsDensityModel->all();
$elements_data = []; $elements_data = [];
foreach ($this->getMonths('elements') as $index => $month) {
$start = date('Y-m-d',strtotime($index));
$end = date('Y-m-t',strtotime($index));
$info = ['Element', 'Density', [ 'role' => 'style' ]]; $small_data = [];
array_push($elements_data,$info); $elements = $this->elementsDensityModel->whereBetween('created_at',[$start,$end])->get();
$info = ['Element', 'Density', [ 'role' => 'style' ]];
$color_setting = [ array_push($small_data,$info);
'copper' => '#ffb1c1', $color_setting = [
'silver' => '#9ad0f5', 'copper' => '#ffb1c1',
'gold' => '#ffe6aa', 'silver' => '#9ad0f5',
'platinum' => '#a5dfdf', 'gold' => '#ffe6aa',
]; 'platinum' => '#a5dfdf',
];
foreach ($elements as $key => $value) { foreach ($elements as $key => $value) {
array_push($elements_data,[ Str::title($value->elements), (int)$value->density, $color_setting[$value->elements]]); array_push($small_data,[ Str::title($value->elements), (int)$value->density, $color_setting[$value->elements]]);
}
array_push($elements_data,[ 'date' => $index,'elements' => json_encode($small_data)]);
} }
return $elements_data;
return json_encode($elements_data);
} }
private function get_pet_popularity(){ private function get_pet_popularity(){
//the code below depends on how you will represent your data based on your data structure //the code below depends on how you will represent your data based on your data structure
$dogs = $this->petPopularityModel->where('pet_name','Dogs')->pluck('popularity')->toArray();
$cats = $this->petPopularityModel->where('pet_name','Cats')->pluck('popularity')->toArray();
$popularity_data = []; $popularity_data = [];
foreach(range(0,(count($dogs)-1 > count($cats)-1? count($dogs)-1 : count($cats)-1 )) as $value){ foreach ($this->getMonths('popularity') as $index => $month) {
array_push($popularity_data,[$value,(int)$dogs[$value],(int)$cats[$value]]); $start = date('Y-m-d',strtotime($index));
$end = date('Y-m-t',strtotime($index));
$small_data = [];
$dogs = $this->petPopularityModel->where('pet_name','Dogs')->whereBetween('created_at',[$start,$end])->pluck('popularity')->toArray();
$cats = $this->petPopularityModel->where('pet_name','Cats')->whereBetween('created_at',[$start,$end])->pluck('popularity')->toArray();
foreach(range(0,(count($dogs)-1 > count($cats)-1? count($dogs)-1 : count($cats)-1 )) as $value){
array_push($small_data,[$value,(int)$dogs[$value],(int)$cats[$value]]);
}
array_push($popularity_data,[ 'date' => $index, 'popularity' => json_encode($small_data)]);
} }
return json_encode($popularity_data); return $popularity_data;
}
private function createFolder(){
$path = public_path('pdf');
File::makeDirectory($path, $mode = 0777, true, true);
} }
} }
...@@ -19,12 +19,20 @@ class ElementsDensityDataSeeder extends Seeder ...@@ -19,12 +19,20 @@ class ElementsDensityDataSeeder extends Seeder
'gold', 'gold',
'platinum' 'platinum'
]; ];
$start_date = '2019-01-01';
$end_date = date('Y-m-d');
foreach ($elements as $value) { while (strtotime($start_date) <= strtotime($end_date)) {
$new_elements = new ElementsDensityModel; echo "$start_date\n";
$new_elements->elements = $value; foreach ($elements as $value) {
$new_elements->density = rand(10,50); $new_elements = new ElementsDensityModel;
$new_elements->save(); $new_elements->elements = $value;
} $new_elements->density = rand(10,50);
$new_elements->created_at = $start_date;
$new_elements->updated_at = $start_date;
$new_elements->save();
}
$start_date = date ("Y-m-d", strtotime("+1 month", strtotime($start_date)));
}
} }
} }
...@@ -18,13 +18,22 @@ class PetPopularitySeeder extends Seeder ...@@ -18,13 +18,22 @@ class PetPopularitySeeder extends Seeder
'Cats', 'Cats',
]; ];
foreach (range(0,5) as $value) { $start_date = '2019-01-01';
foreach ($pets as $pet) { $end_date = date('Y-m-d');
$new_data = new PetPopularityModel;
$new_data->pet_name = $pet; while (strtotime($start_date) <= strtotime($end_date)) {
$new_data->popularity = rand($value,50); echo "$start_date\n";
$new_data->save(); foreach (range(0,5) as $value) {
} foreach ($pets as $pet) {
} $new_data = new PetPopularityModel;
$new_data->pet_name = $pet;
$new_data->popularity = rand($value,50);
$new_data->created_at = $start_date;
$new_data->updated_at = $start_date;
$new_data->save();
}
}
$start_date = date ("Y-m-d", strtotime("+1 month", strtotime($start_date)));
}
} }
} }
...@@ -48,11 +48,11 @@ html, body { ...@@ -48,11 +48,11 @@ html, body {
.m-b-md { .m-b-md {
margin-bottom: 30px; margin-bottom: 30px;
} }
#bar_chart_div{ /*#bar_chart_div{
visibility: hidden; visibility: hidden;
height: 0px; height: 0px;
} }
#line_chart_div{ #line_chart_div{
visibility: hidden; visibility: hidden;
height: 0px; height: 0px;
} }*/
\ No newline at end of file
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
/*sample css*/ /*sample css*/
.heading{ .heading{
text-align: center; text-align: center;
margin-bottom: 100px; margin-bottom: 100px;
} }
.author{ .author{
color: #cecece; color: #cecece;
...@@ -51,12 +51,5 @@ ...@@ -51,12 +51,5 @@
<h3>Vertical Line Chart</h3> <h3>Vertical Line Chart</h3>
<img src="{{$line_chart}}" class="linechart"> <img src="{{$line_chart}}" class="linechart">
</div> </div>
<!-- <div class="heading">
<h1>" {{$dummy_data->title}} "</h1>
<span class="author">- {{$dummy_data->author}} | {{date('F d, Y h:i A',strtotime($dummy_data->created_at))}}</span>
</div>
<div class="body">
<p>{{$dummy_data->content}}</p>
</div> -->
</body> </body>
</html> </html>
\ No newline at end of file
<style type="text/css">
@foreach($elements as $index => $data)
#bar_chart_div_{!!$index!!}{
visibility: hidden;
height: 0px;
}
@endforeach
</style>
<script type="text/javascript">
@foreach($elements as $index => $data)
//barchart
google.charts.load("current", {packages:['corechart']});
google.charts.setOnLoadCallback(barChart{!!$index!!});
var bar_chart_{!!$index!!};
function barChart{!!$index!!}() {
var data = google.visualization.arrayToDataTable({!!$data['elements']!!});
var options = {
title: "{!!date('F Y',strtotime($data['date']))!!}",
bar: {groupWidth: '100px'},
legend: 'none',
};
var bar_chart_div_{!!$index!!} = document.getElementById('bar_chart_div_{!!$index!!}');
var chart = new google.visualization.BarChart(bar_chart_div_{!!$index!!});
google.visualization.events.addListener(chart, 'ready', function () {
bar_chart_{!!$index!!} = chart.getImageURI();
$(".chart_div").append('<input type="hidden" name="bar_chart[]" value="' + bar_chart_{!!$index!!} + '">');
});
chart.draw(data, options);
}
@endforeach
</script>
<style type="text/css">
@foreach($pet_popularity as $index => $data)
#line_chart_div_{!!$index!!}{
visibility: hidden;
height: 0px;
}
@endforeach
</style>
<script type="text/javascript">
@foreach($pet_popularity as $index => $data)
//linechart
google.charts.load('current', {packages: ['corechart', 'line']});
google.charts.setOnLoadCallback(lineChart{!!$index!!});
var line_chart_{!!$index!!};
function lineChart{!!$index!!}() {
var data = new google.visualization.DataTable();
data.addColumn('number', 'X');
data.addColumn('number', 'Dogs');
data.addColumn('number', 'Cats');
data.addRows({!!$data['popularity']!!});
var options = {
hAxis: {
title: '{!!date('F Y',strtotime($data['date']))!!}'
},
vAxis: {
title: 'Pet Popularity'
},
colors: ['#ff6384', '#36a2eb'],
curveType: 'function',
legend: { position: 'bottom' },
orientation: 'vertical',
};
var line_chart_div_{!!$index!!} = document.getElementById('line_chart_div_{!!$index!!}');
var chart = new google.visualization.LineChart(line_chart_div_{!!$index!!});
google.visualization.events.addListener(chart, 'ready', function () {
line_chart_{!!$index!!} = chart.getImageURI();
$(".chart_div").append('<input type="hidden" name="line_chart[]" value="' + line_chart_{!!$index!!} + '">');
});
chart.draw(data, options);
}
@endforeach
</script>
...@@ -17,69 +17,8 @@ ...@@ -17,69 +17,8 @@
<link rel="stylesheet" href="{{asset('css/bootstrap.min.css')}}" /> <link rel="stylesheet" href="{{asset('css/bootstrap.min.css')}}" />
<link rel="stylesheet" href="{{asset('css/pages/frontend/pdf_download.css')}}" /> <link rel="stylesheet" href="{{asset('css/pages/frontend/pdf_download.css')}}" />
<script type="text/javascript"> @include('frontend._includes.bar_chart')
google.charts.load("current", {packages:['corechart']}); @include('frontend._includes.line_chart')
google.charts.setOnLoadCallback(barChart);
var bar_chart;
var line_chart;
function barChart() {
var data = google.visualization.arrayToDataTable({!!$elements!!});
var options = {
title: "",
bar: {groupWidth: '100px'},
legend: 'none',
};
var bar_chart_div = document.getElementById('bar_chart_div');
var chart = new google.visualization.BarChart(bar_chart_div);
// Wait for the chart to finish drawing before calling the getImageURI() method.
google.visualization.events.addListener(chart, 'ready', function () {
bar_chart = chart.getImageURI();
$(".chart_div").append('<input type="hidden" name="bar_chart" value="' + bar_chart + '">');
// console.log(bar_chart);
});
chart.draw(data, options);
}
google.charts.load('current', {packages: ['corechart', 'line']});
google.charts.setOnLoadCallback(lineChart);
function lineChart() {
var data = new google.visualization.DataTable();
data.addColumn('number', 'X');
data.addColumn('number', 'Dogs');
data.addColumn('number', 'Cats');
data.addRows({!!$pet_popularity!!});
var options = {
// hAxis: {
// title: 'Time'
// },
// vAxis: {
// title: 'Popularity'
// },
colors: ['#ff6384', '#36a2eb'],
curveType: 'function',
legend: { position: 'bottom' },
orientation: 'vertical',
};
var line_chart_div = document.getElementById('line_chart_div');
var chart = new google.visualization.LineChart(line_chart_div);
google.visualization.events.addListener(chart, 'ready', function () {
line_chart = chart.getImageURI();
$(".chart_div").append('<input type="hidden" name="line_chart" value="' + line_chart + '">');
});
chart.draw(data, options);
}
</script>
</head> </head>
<body> <body>
<div class="flex-center position-ref full-height"> <div class="flex-center position-ref full-height">
...@@ -104,12 +43,16 @@ ...@@ -104,12 +43,16 @@
<div class="links"> <div class="links">
<button type="submit" form="myForm">Click me to download sample pdf</button> <button type="submit" form="myForm">Click me to download sample pdf</button>
</div> </div>
<form id='myForm' action="{{route('pdf_download.download')}}" method="POST"> <form id='myForm' action="{{route('pdf_download.download')}}" method="POST" style="">
<div class="chart_div"> <div class="chart_div">
<input type="hidden" name="_token" value="{{csrf_token()}}"> <input type="hidden" name="_token" value="{{csrf_token()}}">
</div> </div>
<div id="bar_chart_div"></div> @foreach($elements as $index => $data)
<div id="line_chart_div"></div> <div id="bar_chart_div_{!!$index!!}"></div>
@endforeach
@foreach($pet_popularity as $index => $data)
<div id="line_chart_div_{!!$index!!}"></div>
@endforeach
</form> </form>
</div> </div>
</div> </div>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment