-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdfViewer.php
More file actions
276 lines (238 loc) · 9.74 KB
/
Copy pathpdfViewer.php
File metadata and controls
276 lines (238 loc) · 9.74 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<?php
session_start();
$is_logged_in = isset($_SESSION['username']);
$userType = $is_logged_in ? $_SESSION['userType'] : null;
if (!$is_logged_in) {
header("Location: login.php");
exit();
}
if (isset($_GET['logout'])) {
session_unset();
session_destroy();
header("Location: index.php");
exit();
}
if (isset($_GET['pdf_filename'])) {
$pdf_filename = $_GET['pdf_filename'];
// Sanitize the filename to prevent directory traversal
$pdf_filename = basename($pdf_filename);
// Construct the full path to the PDF file (assumes PDFs are in the 'pdf' folder)
$pdf_path = 'pdf/' . $pdf_filename;
// Debug: output the full path (this will help you see if the path is correct)
// This should output something like 'pdf/book1.pdf'
} else {
echo "Error: No PDF file specified.";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" />
<title>PDF Viewer</title>
<link rel="stylesheet" href="css/pdfViewerStyle.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.7.1/css/all.min.css" integrity="sha512-5Hs3dF2AEPkpNAR7UiOHba+lRSJNeM2ECkwxUIxC1Q/FLycGTbNapWXB4tP889k5T5Ju8fs4b1P5z/iB4nMfSQ==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<!-- Ensure pdf.js is loaded first -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.16.105/pdf.min.js"></script>
<script>
document.addEventListener('DOMContentLoaded', function() {
// Replace this with the correct PHP dynamic value if needed
const pdf_filename = '<?php echo isset($pdf_filename) ? $pdf_filename : ''; ?>';
if (!pdf_filename) {
alert('Error: No PDF file specified.');
return;
}
const url = 'pdf/' + pdf_filename;
let pdfDoc = null,
pageNum = 1,
pageIsRendering = false,
pageNumIsPending = null;
const scale = 1.5,
canvas = document.querySelector('#pdf-render'),
ctx = canvas.getContext('2d');
// Render page function
const renderPage = num => {
pageIsRendering = true;
console.log("Rendering page: " + num);
pdfDoc.getPage(num).then(page => {
const viewport = page.getViewport({ scale });
canvas.height = viewport.height;
canvas.width = viewport.width;
const renderCtx = {
canvasContext: ctx,
viewport
};
page.render(renderCtx).promise.then(() => {
pageIsRendering = false;
if (pageNumIsPending !== null) {
renderPage(pageNumIsPending);
pageNumIsPending = null;
}
});
// Update the current page number display
document.querySelector('#page-num').textContent = num;
}).catch(err => {
console.error("Error rendering page: " + err.message);
});
};
// Queue page rendering
const queueRenderPage = num => {
if (pageIsRendering) {
pageNumIsPending = num;
} else {
renderPage(num);
}
};
// Show the previous page
const showPrevPage = () => {
if (pageNum <= 1) return;
pageNum--;
queueRenderPage(pageNum);
};
// Show the next page
const showNextPage = () => {
if (pageNum >= pdfDoc.numPages) return;
pageNum++;
queueRenderPage(pageNum);
};
// Fetch the PDF document and render the first page
pdfjsLib.getDocument(url).promise.then(pdfDoc_ => {
pdfDoc = pdfDoc_;
document.querySelector('#page-count').textContent = pdfDoc.numPages;
renderPage(pageNum);
}).catch(err => {
console.error("Error loading PDF: " + err.message);
const div = document.createElement('div');
div.className = 'error';
div.appendChild(document.createTextNode(err.message));
document.querySelector('body').insertBefore(div, canvas);
});
// Event listeners for page navigation
document.querySelector('#prev-page').addEventListener('click', showPrevPage);
document.querySelector('#next-page').addEventListener('click', showNextPage);
});
</script>
</head>
<body>
<header class="header">
<div class="logo">
<a href="index.php"><i class="fa-solid fa-book"></i> Knowledge Nest</a>
</div>
<nav class="nav-bar">
<ul class="nav__links">
<li><a href="index.php">Home</a></li>
<li><a href="contact.php">Contact</a></li>
<li><a href="books.php">Books</a></li>
<?php if ($is_logged_in): ?>
<?php if ($userType === 'user'): ?>
<li><a href="myAccount.php">My Account</a></li>
<?php elseif ($userType === 'admin'): ?>
<li><a href="dashboard.php">Admin Dashboard</a></li>
<?php endif; ?>
<?php endif; ?>
</ul>
</nav>
<div class="login">
<?php if ($is_logged_in): ?>
<form method="GET" action="index.php">
<button type="submit" name="logout">
<i class="fa-solid fa-sign-out-alt"></i><b class="logout-text">Logout</b>
</button>
</form>
<?php else: ?>
<a href="login.php" class="login-icon">
<button><i class="fa-solid fa-user"></i><b class="login-text">Login</b></button>
</a>
<?php endif; ?>
</div>
<div class="toggle-btn">
<i class="fa-solid fa-bars"></i>
</div>
<div class="dropdown-menu">
<ul>
<li><a href="index.php">Home</a></li>
<li><a href="contact.php">Contact</a></li>
<li><a href="books.php">Books</a></li>
<?php if ($is_logged_in): ?>
<?php if ($userType === 'user'): ?>
<li><a href="account.php">My Account</a></li>
<?php elseif ($userType === 'admin'): ?>
<li><a href="dashboard.php">Admin Dashboard</a></li>
<?php endif; ?>
<li>
<form method="GET" action="index.php">
<button type="submit" name="logout">
<i class="fa-solid fa-sign-out-alt"></i><b class="logout-text">Logout</b>
</button>
</form>
</li>
<?php else: ?>
<li>
<a href="login.php" class="login-icon">
<button><i class="fa-solid fa-user"></i><b>Login</b></button>
</a>
</li>
<?php endif; ?>
</ul>
</div>
</header>
<div class="top-bar">
<button class="btn" id="prev-page">
<i class="fas fa-arrow-circle-left"></i> Prev Page
</button>
<span class="page-info">
Page <span id="page-num"></span> of <span id="page-count"></span>
</span>
<button class="btn" id="next-page">
Next Page <i class="fas fa-arrow-circle-right"></i>
</button>
</div>
<!-- Canvas to render the PDF -->
<canvas id="pdf-render"></canvas>
<footer>
<div class="footer-container">
<!-- Logo Section -->
<div class="footer-logo-section">
<img src="css/Image/KnowledgeNest-noBK.png" alt="Harvard Shield" class="footer-logo">
</div>
<!-- Links and License Section -->
<div class="footer-content">
<div class="footer-links">
<!-- First Column -->
<div class="link-column">
<p>GIVING TO THE LIBRARY</p>
<p>OFFICE OF THE PROVOST</p>
<p>HOLLIS</p>
<p>HOLLIS FOR ARCHIVAL DISCOVERY</p>
<p>DATABASES</p>
</div>
<!-- Second Column -->
<div class="link-column">
<p>NEWSLETTERS/SOCIAL</p>
<p>STAFF PORTAL</p>
<p>LIBRARY ACCESSIBILITY</p>
<p>REPORT A PROBLEM</p>
</div>
<!-- Third Column -->
<div class="link-column">
<div class="footer-policy-links">
<a href="#">Accessibility</a>
<a href="#">Privacy</a>
</div>
</div>
</div>
<!-- License Section -->
<p class="footer-license">
Creative Commons Attribution 4.0 International License. Except where otherwise noted,
this work is subject to a <a href="#">Creative Commons Attribution 4.0 International License</a>
which allows anyone to share and adapt our material as long as proper attribution is given.
For details and exceptions, see the <a href="#">Harvard Library Copyright Policy</a>
©2024 Presidents and Fellows of Harvard College.
</p>
</div>
</div>
</footer>
</body>
</html>