-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
92 lines (86 loc) · 3.56 KB
/
index.php
File metadata and controls
92 lines (86 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
require __DIR__ . '/vendor/autoload.php';
$errors = [];
$result_html = "";
if($_SERVER["REQUEST_METHOD"] === "POST") {
$file = $_FILES["file"];
$originalName = $_FILES['file']['name'];
$fileNameWithoutExt = pathinfo($originalName, PATHINFO_FILENAME);
if($file['name'] !== ''){
$allowedMimeTypes = [
'application/vnd.ms-excel',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
];
$allowedExtensions = ['xls', 'xlsx'];
$fileExtension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mimeType = finfo_file($finfo, $file['tmp_name']);
finfo_close($finfo);
if (in_array($mimeType, $allowedMimeTypes) && in_array($fileExtension, $allowedExtensions)) {
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($file['tmp_name']);
$data = $spreadsheet->getActiveSheet()->toArray();
foreach ($data as $key1 => $row) {
if($key1 == 0){
$result_html .= "<thead>";
}else if($key1 == 1) {
$result_html .= "<tbody>";
}
$result_html .= "<tr>";
foreach ($row as $key2 => $column) {
if($key1 == 0) {
$result_html .= "<th>" . $column . "</th>";
}else {
$result_html .= "<td>" . $column . "</td>";
}
}
$result_html .= "</tr>";
if($key1 == 0){
$result_html .= "</thead>";
}else if($key1 == count($data) - 1) {
$result_html .= "</tbody>";
}
}
$mpdf = new \Mpdf\Mpdf(['tempDir' => __DIR__ . '/tmp/']);
$html = "<table class='table table-responsive'>". $result_html . "</table>";
$mpdf->WriteHTML($html);
$mpdf->Output($fileNameWithoutExt . '.pdf', 'D');
} else {
array_push($errors, "Invalid Excel File");
}
}else {
array_push($errors, "Please, select valid file");
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Excel to Pdf</title>
<link rel="stylesheet" href="./public/style.css" />
<link rel="stylesheet" href="./vendor/twbs/bootstrap/dist/css/bootstrap.min.css" />
</head>
<body>
<div class="container">
<h1 class="main_head">upload excel and download well formated pdf file</h1>
<?php if(count($errors) > 0): ?>
<?php foreach($errors as $error): ?>
<h2 style="color: #f00;"><?= $error ?></h2>
<?php endforeach ?>
<?php endif ?>
<form action="./" method="post" enctype="multipart/form-data">
<div class="form-control">
<label for="file">File</label>
<input type="file" class="form-control" name="file" accept=".xls,.xlsx" />
</div>
<button class="btn btn-primary" type="submit">Submit</button>
</form>
<?php if(strlen($result_html) > 0): ?>
<table class="table table-responsive">
<?= $result_html ?>
</table>
<?php endif ?>
</div>
</body>
</html>