Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
sample-pdf-download
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Joshua Arosco
sample-pdf-download
Commits
761cc9a0
Commit
761cc9a0
authored
Sep 26, 2019
by
Joshua Arosco
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Create save and download functionality on sample PDF download
parent
fb7117b8
Changes
9
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
211 additions
and
123 deletions
+211
-123
.gitignore
.gitignore
+1
-1
app/Http/Controllers/Frontend/PdfDownloadController.php
app/Http/Controllers/Frontend/PdfDownloadController.php
+81
-31
database/seeds/ElementsDensityDataSeeder.php
database/seeds/ElementsDensityDataSeeder.php
+14
-6
database/seeds/PetPopularitySeeder.php
database/seeds/PetPopularitySeeder.php
+17
-8
public/css/pages/frontend/pdf_download.css
public/css/pages/frontend/pdf_download.css
+2
-2
resources/views/domPdf/sample.blade.php
resources/views/domPdf/sample.blade.php
+2
-9
resources/views/frontend/_includes/bar_chart.blade.php
resources/views/frontend/_includes/bar_chart.blade.php
+37
-0
resources/views/frontend/_includes/line_chart.blade.php
resources/views/frontend/_includes/line_chart.blade.php
+48
-0
resources/views/frontend/pdf_download.blade.php
resources/views/frontend/pdf_download.blade.php
+9
-66
No files found.
.gitignore
View file @
761cc9a0
/node_modules
/public/
hot
/public/
pdf
/public/storage
/storage/*.key
/vendor
...
...
app/Http/Controllers/Frontend/PdfDownloadController.php
View file @
761cc9a0
...
...
@@ -7,7 +7,8 @@ use App\Http\Controllers\Controller;
use
App\Models\DummyData
as
DummyDataModel
;
use
App\Models\ElementsDensity
as
ElementsDensityModel
;
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
{
...
...
@@ -21,56 +22,105 @@ class PdfDownloadController extends Controller
public
function
index
(){
$this
->
data
[
'elements'
]
=
$this
->
get_elements_data
();
$this
->
data
[
'pet_popularity'
]
=
$this
->
get_pet_popularity
();
return
view
(
'frontend.pdf_download'
,
$this
->
data
);
}
public
function
download
(
Request
$request
){
$this
->
data
[
'bar_chart'
]
=
$request
->
bar_chart
;
$this
->
data
[
'line_chart'
]
=
$request
->
line_chart
;
$this
->
data
[
'dummy_data'
]
=
$this
->
dummyDataModel
->
first
();
$this
->
createFolder
();
$files
=
[];
foreach
(
$request
->
bar_chart
as
$key
=>
$value
)
{
$this
->
data
[
'bar_chart'
]
=
$request
->
bar_chart
[
$key
];
$this
->
data
[
'line_chart'
]
=
$request
->
line_chart
[
$key
];
$pdf
=
new
PDF
();
$pdf
=
App
::
make
(
'dompdf.wrapper'
);
//set the html view to load
$pdf
->
loadView
(
'domPdf.sample'
,
$this
->
data
);
$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
// 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
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
(){
//the code below depends on how you will represent your data based on your data structure
$elements
=
$this
->
elementsDensityModel
->
all
();
$elements_data
=
[];
foreach
(
$this
->
getMonths
(
'elements'
)
as
$index
=>
$month
)
{
$start
=
date
(
'Y-m-d'
,
strtotime
(
$index
));
$end
=
date
(
'Y-m-t'
,
strtotime
(
$index
));
$small_data
=
[];
$elements
=
$this
->
elementsDensityModel
->
whereBetween
(
'created_at'
,[
$start
,
$end
])
->
get
();
$info
=
[
'Element'
,
'Density'
,
[
'role'
=>
'style'
]];
array_push
(
$elements_data
,
$info
);
array_push
(
$small_data
,
$info
);
$color_setting
=
[
'copper'
=>
'#ffb1c1'
,
'silver'
=>
'#9ad0f5'
,
'gold'
=>
'#ffe6aa'
,
'platinum'
=>
'#a5dfdf'
,
];
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
]]);
}
return
json_encode
(
$elements_data
);
array_push
(
$elements_data
,[
'date'
=>
$index
,
'elements'
=>
json_encode
(
$small_data
)]);
}
return
$elements_data
;
}
private
function
get_pet_popularity
(){
//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
=
[];
foreach
(
$this
->
getMonths
(
'popularity'
)
as
$index
=>
$month
)
{
$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
(
$popularity_data
,[
$value
,(
int
)
$dogs
[
$value
],(
int
)
$cats
[
$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
);
}
}
database/seeds/ElementsDensityDataSeeder.php
View file @
761cc9a0
...
...
@@ -19,12 +19,20 @@ class ElementsDensityDataSeeder extends Seeder
'gold'
,
'platinum'
];
$start_date
=
'2019-01-01'
;
$end_date
=
date
(
'Y-m-d'
);
while
(
strtotime
(
$start_date
)
<=
strtotime
(
$end_date
))
{
echo
"
$start_date
\n
"
;
foreach
(
$elements
as
$value
)
{
$new_elements
=
new
ElementsDensityModel
;
$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
)));
}
}
}
database/seeds/PetPopularitySeeder.php
View file @
761cc9a0
...
...
@@ -18,13 +18,22 @@ class PetPopularitySeeder extends Seeder
'Cats'
,
];
$start_date
=
'2019-01-01'
;
$end_date
=
date
(
'Y-m-d'
);
while
(
strtotime
(
$start_date
)
<=
strtotime
(
$end_date
))
{
echo
"
$start_date
\n
"
;
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
)));
}
}
}
public/css/pages/frontend/pdf_download.css
View file @
761cc9a0
...
...
@@ -48,11 +48,11 @@ html, body {
.m-b-md
{
margin-bottom
:
30px
;
}
#bar_chart_div
{
/*
#bar_chart_div{
visibility: hidden;
height: 0px;
}
#line_chart_div{
visibility: hidden;
height: 0px;
}
\ No newline at end of file
}*/
resources/views/domPdf/sample.blade.php
View file @
761cc9a0
...
...
@@ -51,12 +51,5 @@
<h3>
Vertical Line Chart
</h3>
<img
src=
"{{$line_chart}}"
class=
"linechart"
>
</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>
</html>
resources/views/frontend/_includes/bar_chart.blade.php
0 → 100644
View file @
761cc9a0
<style
type=
"text/css"
>
@foreach
(
$
elements
as
$
index
=>
$
data
)
#ba
r_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>
resources/views/frontend/_includes/line_chart.blade.php
0 → 100644
View file @
761cc9a0
<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>
resources/views/frontend/pdf_download.blade.php
View file @
761cc9a0
...
...
@@ -17,69 +17,8 @@
<link
rel=
"stylesheet"
href=
"{{asset('css/bootstrap.min.css')}}"
/>
<link
rel=
"stylesheet"
href=
"{{asset('css/pages/frontend/pdf_download.css')}}"
/>
<script
type=
"text/javascript"
>
google
.
charts
.
load
(
"
current
"
,
{
packages
:[
'
corechart
'
]});
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>
@include('frontend._includes.bar_chart')
@include('frontend._includes.line_chart')
</head>
<body>
<div
class=
"flex-center position-ref full-height"
>
...
...
@@ -104,12 +43,16 @@
<div
class=
"links"
>
<button
type=
"submit"
form=
"myForm"
>
Click me to download sample pdf
</button>
</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"
>
<input
type=
"hidden"
name=
"_token"
value=
"{{csrf_token()}}"
>
</div>
<div
id=
"bar_chart_div"
></div>
<div
id=
"line_chart_div"
></div>
@foreach($elements as $index => $data)
<div
id=
"bar_chart_div_{!!$index!!}"
></div>
@endforeach
@foreach($pet_popularity as $index => $data)
<div
id=
"line_chart_div_{!!$index!!}"
></div>
@endforeach
</form>
</div>
</div>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment