-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdf_remove_password.html
More file actions
417 lines (356 loc) · 13.9 KB
/
pdf_remove_password.html
File metadata and controls
417 lines (356 loc) · 13.9 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Remove Password from PDF</title>
<script src="https://cdn.jsdelivr.net/npm/pdf-lib@1.17.1/dist/pdf-lib.min.js"></script>
<style>
/* ===== LAYOUT ===== */
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
margin: 0;
padding: 0;
background: #f0f0f0;
color: #333;
min-height: 100vh;
display: flex;
flex-direction: column;
}
.container {
max-width: 800px;
margin: 0 auto;
padding: 20px;
background: #fff;
min-height: 100vh;
box-shadow: 0 0 20px rgba(0,0,0,0.1);
}
/* ===== HEADER ===== */
.header {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 20px;
margin: -20px -20px 30px -20px;
text-align: center;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
.header h1 {
margin: 0;
font-size: 24px;
font-weight: 600;
}
.header .subtitle {
margin: 5px 0 0 0;
font-size: 14px;
opacity: 0.9;
}
/* ===== FORM SECTION ===== */
.form-section {
background: #f8f9fa;
border: 1px solid #dee2e6;
border-radius: 8px;
padding: 20px;
margin-bottom: 20px;
}
.form-group {
margin-bottom: 15px;
}
.form-group:last-child {
margin-bottom: 0;
}
label {
display: block;
margin-bottom: 5px;
font-weight: 500;
color: #495057;
}
input[type="file"], input[type="password"] {
width: 100%;
padding: 10px;
border: 1px solid #dee2e6;
border-radius: 4px;
font-size: 14px;
background: white;
box-sizing: border-box;
}
input[type="file"]:focus, input[type="password"]:focus {
border-color: #007bff;
outline: none;
box-shadow: 0 0 0 2px rgba(0,123,255,0.25);
}
/* ===== BUTTONS ===== */
.btn {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
border: none;
padding: 12px 24px;
border-radius: 4px;
cursor: pointer;
font-size: 14px;
font-weight: 500;
transition: all 0.3s ease;
display: inline-block;
text-decoration: none;
}
.btn:hover {
transform: translateY(-1px);
box-shadow: 0 4px 8px rgba(0,0,0,0.2);
}
.btn:disabled {
background: #6c757d;
cursor: not-allowed;
transform: none;
box-shadow: none;
}
/* ===== MESSAGES ===== */
.message {
padding: 12px;
border-radius: 4px;
margin: 15px 0;
display: none;
}
.message.success {
background: #d4edda;
border: 1px solid #c3e6cb;
color: #155724;
}
.message.error {
background: #f8d7da;
border: 1px solid #f5c6cb;
color: #721c24;
}
.message.info {
background: #d1ecf1;
border: 1px solid #bee5eb;
color: #0c5460;
}
/* ===== LOADING ===== */
.loading {
display: none;
text-align: center;
padding: 20px;
color: #6c757d;
font-style: italic;
}
.loading-spinner {
display: inline-block;
width: 20px;
height: 20px;
border: 3px solid #f3f3f3;
border-top: 3px solid #667eea;
border-radius: 50%;
animation: spin 1s linear infinite;
margin-right: 10px;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
/* ===== NOTE SECTION ===== */
.note {
background: #fff3cd;
border: 1px solid #ffeaa7;
color: #856404;
padding: 15px;
border-radius: 4px;
margin-top: 20px;
font-size: 13px;
line-height: 1.5;
}
.note strong {
color: #533f03;
}
/* ===== RESPONSIVE ===== */
@media (max-width: 768px) {
.container {
margin: 0;
padding: 15px;
box-shadow: none;
}
.header {
margin: -15px -15px 20px -15px;
padding: 15px;
}
.header h1 {
font-size: 20px;
}
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>🔓 Remove Password from PDF</h1>
<div class="subtitle">Unlock password-protected PDFs in your browser</div>
</div>
<div class="form-section">
<div class="form-group">
<label for="pdfFile">Select Password-Protected PDF:</label>
<input type="file" id="pdfFile" accept=".pdf" />
</div>
<div class="form-group">
<label for="password">Enter PDF Password:</label>
<input type="password" id="password" placeholder="Enter the PDF password" />
</div>
<div class="form-group">
<button id="unlockBtn" class="btn" disabled>🔓 Unlock PDF</button>
</div>
</div>
<div id="loading" class="loading">
<div class="loading-spinner"></div>
Processing PDF...
</div>
<div id="result" class="message success"></div>
<div id="error" class="message error"></div>
<div id="info" class="message info"></div>
<div class="note">
<strong>Important Notes:</strong><br>
• This tool works only if the PDF is not encrypted with advanced security features<br>
• You must know the correct password to unlock the PDF<br>
• Client-side JavaScript libraries cannot remove passwords from all types of encrypted PDFs<br>
• Files are processed entirely in your browser - nothing is uploaded to any server<br>
• Some PDFs with owner passwords but no user passwords may not require a password input
</div>
</div>
<script>
// Check if pdf-lib loaded successfully, if not show error
document.addEventListener('DOMContentLoaded', function() {
if (typeof window.PDFLib === 'undefined') {
document.getElementById('error').innerHTML = `
<strong>⚠️ PDF Library Not Loaded</strong><br>
The PDF processing library could not be loaded. This may be due to network restrictions or ad blockers.<br>
Please try refreshing the page or disabling ad blockers.
`;
document.getElementById('error').style.display = 'block';
document.getElementById('unlockBtn').disabled = true;
document.getElementById('unlockBtn').textContent = 'PDF Library Not Available';
}
});
const PDFLib = window.PDFLib;
// DOM elements
const pdfFileInput = document.getElementById('pdfFile');
const passwordInput = document.getElementById('password');
const unlockBtn = document.getElementById('unlockBtn');
const loading = document.getElementById('loading');
const result = document.getElementById('result');
const error = document.getElementById('error');
const info = document.getElementById('info');
// State
let selectedFile = null;
// Event listeners
pdfFileInput.addEventListener('change', handleFileSelect);
passwordInput.addEventListener('input', updateUnlockButton);
passwordInput.addEventListener('keypress', handlePasswordKeyPress);
unlockBtn.addEventListener('click', unlockPDF);
function handleFileSelect(event) {
selectedFile = event.target.files[0];
updateUnlockButton();
clearMessages();
if (selectedFile) {
if (selectedFile.type !== 'application/pdf') {
showError('Please select a valid PDF file.');
selectedFile = null;
updateUnlockButton();
return;
}
showInfo(`Selected: ${selectedFile.name} (${formatFileSize(selectedFile.size)})`);
}
}
function handlePasswordKeyPress(event) {
if (event.key === 'Enter' && !unlockBtn.disabled) {
unlockPDF();
}
}
function updateUnlockButton() {
unlockBtn.disabled = !selectedFile || !passwordInput.value.trim();
}
async function unlockPDF() {
if (!selectedFile || !passwordInput.value.trim()) {
showError('Please select a PDF file and enter a password.');
return;
}
if (typeof window.PDFLib === 'undefined') {
showError('PDF processing library is not available. Please refresh the page and try again.');
return;
}
clearMessages();
showLoading(true);
unlockBtn.disabled = true;
try {
// Read the PDF file
const fileArrayBuffer = await selectedFile.arrayBuffer();
// Try to load the PDF with the provided password
let pdfDoc;
try {
pdfDoc = await PDFLib.PDFDocument.load(fileArrayBuffer, {
password: passwordInput.value
});
} catch (loadError) {
if (loadError.message.includes('password') || loadError.message.includes('encrypted')) {
throw new Error('Incorrect password or unsupported encryption method.');
}
throw loadError;
}
// Create a new PDF without password protection
const unlockedPdfBytes = await pdfDoc.save();
// Create download link
const blob = new Blob([unlockedPdfBytes], { type: 'application/pdf' });
const url = URL.createObjectURL(blob);
const originalName = selectedFile.name.replace('.pdf', '');
const downloadName = `${originalName}_unlocked.pdf`;
result.innerHTML = `
<strong>✅ PDF Successfully Unlocked!</strong><br>
<a href="${url}" download="${downloadName}" class="btn" style="margin-top: 10px; display: inline-block;">
📥 Download Unlocked PDF
</a>
`;
result.style.display = 'block';
// Clear form
pdfFileInput.value = '';
passwordInput.value = '';
selectedFile = null;
updateUnlockButton();
} catch (err) {
console.error('PDF unlock error:', err);
let errorMessage = 'Failed to unlock PDF. ';
if (err.message.includes('password') || err.message.includes('Incorrect password')) {
errorMessage += 'Please check that the password is correct.';
} else if (err.message.includes('encrypted') || err.message.includes('encryption')) {
errorMessage += 'This PDF uses an encryption method that cannot be processed by this tool.';
} else if (err.message.includes('Invalid PDF')) {
errorMessage += 'The selected file appears to be corrupted or is not a valid PDF.';
} else {
errorMessage += 'This may be due to unsupported encryption or a corrupted file.';
}
showError(errorMessage);
} finally {
showLoading(false);
updateUnlockButton();
}
}
function showLoading(show) {
loading.style.display = show ? 'block' : 'none';
}
function showError(message) {
error.textContent = message;
error.style.display = 'block';
}
function showInfo(message) {
info.textContent = message;
info.style.display = 'block';
}
function clearMessages() {
result.style.display = 'none';
error.style.display = 'none';
info.style.display = 'none';
}
function formatFileSize(bytes) {
if (bytes === 0) return '0 Bytes';
const k = 1024;
const sizes = ['Bytes', 'KB', 'MB', 'GB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
}
</script>
</body>
</html>