Repository navigation
Expand file tree
/
Copy pathpdf-metadata.html
More file actions
357 lines (343 loc) · 11.3 KB
/
Copy pathpdf-metadata.html
File metadata and controls
357 lines (343 loc) · 11.3 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>PDF Metadata Viewer</title>
<style>
* {
box-sizing: border-box;
}
body {
font-family: Helvetica, -apple-system, BlinkMacSystemFont, "Segoe UI",
Arial, sans-serif;
line-height: 1.6;
max-width: 980px;
margin: 0 auto;
padding: 20px;
color: #24292e;
background: #ffffff;
}
h1 {
border-bottom: 1px solid #eaecef;
padding-bottom: 0.3em;
margin-top: 0;
font-size: 32px;
font-weight: 600;
}
.controls {
display: flex;
gap: 8px;
align-items: center;
margin-bottom: 16px;
flex-wrap: wrap;
justify-content: space-between;
}
input[type="file"] {
font-size: 16px;
font-family: Helvetica, -apple-system, BlinkMacSystemFont, "Segoe UI",
Arial, sans-serif;
color: transparent;
width: auto;
display: none;
}
input[type="file"]::file-selector-button {
margin-right: 0;
padding: 6px 12px;
background: #fafbfc;
color: #24292e;
border: 1px solid rgba(27, 31, 35, 0.15);
border-radius: 6px;
cursor: pointer;
font-size: 14px;
font-family: Helvetica, -apple-system, BlinkMacSystemFont, "Segoe UI",
Arial, sans-serif;
}
input[type="file"]::file-selector-button:hover {
background: #f3f4f6;
}
.btn {
background: #fafbfc;
color: #24292e;
padding: 8px 12px;
border-radius: 6px;
border: 1px solid rgba(27, 31, 35, 0.15);
cursor: pointer;
font-size: 14px;
line-height: 20px;
text-decoration: none;
font-family: Helvetica, -apple-system, BlinkMacSystemFont, "Segoe UI",
Arial, sans-serif;
}
.btn:hover {
background: #f3f4f6;
}
.btn.primary {
background: #0366d6;
color: #fff;
border: 1px solid rgba(27, 31, 35, 0.15);
}
.btn.primary:hover {
background: #0256c5;
}
.btn.secondary {
background: #fafbfc;
color: #24292e;
border: 1px solid rgba(27, 31, 35, 0.15);
}
.btn.secondary:hover {
background: #f3f4f6;
}
.btn:disabled {
opacity: 0.5;
cursor: not-allowed;
}
.btn.secondary:disabled:hover {
background: #fafbfc;
}
.btn.primary:disabled:hover {
background: #0366d6;
}
.panel {
background: #f6f8fa;
padding: 16px;
border-radius: 6px;
border: 1px solid #e1e4e8;
margin-bottom: 16px;
}
.panel h2 {
margin-top: 0;
margin-bottom: 12px;
font-size: 20px;
font-weight: 600;
color: #24292e;
}
.panel pre {
background: #ffffff;
padding: 12px;
overflow-x: auto;
border-radius: 6px;
border: 1px solid #e1e4e8;
font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo,
monospace;
font-size: 14px;
color: #24292e;
margin: 0;
min-height: 250px;
}
.actions {
margin-top: 12px;
display: flex;
gap: 8px;
}
label.btn {
display: inline-block;
margin: 0;
}
.back-link {
display: inline-block;
margin-bottom: 16px;
color: #0366d6;
text-decoration: none;
font-size: 14px;
}
.back-link:hover {
text-decoration: underline;
}
.info {
font-size: 13px;
color: #586069;
margin-bottom: 12px;
}
</style>
<script type="module">
// Import PDF.js library
import * as pdfjsLib from "https://cdn.jsdelivr.net/npm/pdfjs-dist@5.4.449/build/pdf.min.mjs";
// Configure PDF.js worker
pdfjsLib.GlobalWorkerOptions.workerSrc =
"https://cdn.jsdelivr.net/npm/pdfjs-dist@5.4.449/build/pdf.worker.mjs";
const fileInput = document.getElementById("fileInput");
const metadataPanel = document.getElementById("metadataPanel");
const metadataOutput = document.getElementById("metadataOutput");
const copyBtn = document.getElementById("copyBtn");
const downloadBtn = document.getElementById("downloadBtn");
const clearBtn = document.getElementById("clearBtn");
fileInput.addEventListener("change", async () => {
const file = fileInput.files[0];
if (!file) return;
if (file.type !== "application/pdf") {
alert("Please select a PDF file.");
return;
}
const reader = new FileReader();
reader.onload = async function (e) {
const arrayBuffer = e.target.result;
try {
const pdfDoc = await pdfjsLib.getDocument({
data: arrayBuffer,
wasmUrl: "https://cdn.jsdelivr.net/npm/pdfjs-dist@5.4.449/wasm/",
}).promise;
const meta = await pdfDoc.getMetadata();
const pageCount = pdfDoc.numPages;
const pdfInfo = pdfDoc.pdfInfo || {};
// Get file size in a readable format
const 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 (
Math.round((bytes / Math.pow(k, i)) * 100) / 100 +
" " +
sizes[i]
);
};
// Get permissions
const permissions = await pdfDoc.getPermissions();
const permissionFlags = {
printing: permissions
? permissions.includes(2) || permissions.includes(4)
: "Unknown",
modifying: permissions ? permissions.includes(8) : "Unknown",
copying: permissions ? permissions.includes(16) : "Unknown",
annotating: permissions ? permissions.includes(32) : "Unknown",
};
// Get page dimensions from first page
let pageDimensions = "N/A";
try {
const firstPage = await pdfDoc.getPage(1);
const viewport = firstPage.getViewport({
scale: 1,
});
pageDimensions = `${Math.round(viewport.width)} x ${Math.round(
viewport.height
)} pts`;
} catch (err) {
console.error("Could not get page dimensions:", err);
}
// Check for bookmarks/outline
let hasBookmarks = false;
try {
const outline = await pdfDoc.getOutline();
hasBookmarks = outline && outline.length > 0;
} catch (err) {
console.error("Could not check bookmarks:", err);
}
// Check for JavaScript
let hasJavaScript = false;
try {
const js = await pdfDoc.getJavaScript();
hasJavaScript = js && js.length > 0;
} catch (err) {
console.error("Could not check JavaScript:", err);
}
// Check for attachments
let hasAttachments = false;
let attachmentCount = 0;
try {
const attachments = await pdfDoc.getAttachments();
if (attachments) {
attachmentCount = Object.keys(attachments).length;
hasAttachments = attachmentCount > 0;
}
} catch (err) {
console.error("Could not check attachments:", err);
}
const info = {
fileName: file.name,
fileSize: formatFileSize(file.size),
fileSizeBytes: file.size,
pdfVersion: pdfInfo.PDFFormatVersion || "N/A",
pageCount,
pageDimensions: pageDimensions,
title: meta.info.Title || "N/A",
author: meta.info.Author || "N/A",
subject: meta.info.Subject || "N/A",
keywords: meta.info.Keywords || "N/A",
creator: meta.info.Creator || "N/A",
producer: meta.info.Producer || "N/A",
creationDate: meta.info.CreationDate || "N/A",
modDate: meta.info.ModDate || "N/A",
copyright: meta.info.Copyright || "N/A",
language: meta.info.Language || "N/A",
trapped: meta.info.Trapped || "N/A",
linearized: pdfDoc.loadingParams?.disableRange
? false
: pdfInfo.IsLinearized || false,
security: {
encrypted: meta.info.IsEncrypted || false,
permissions: permissions
? permissionFlags
: "No restrictions or unknown",
},
features: {
hasBookmarks,
hasJavaScript,
hasAttachments,
attachmentCount: hasAttachments ? attachmentCount : 0,
},
};
metadataOutput.textContent = JSON.stringify(info, null, 2);
downloadBtn.disabled = false;
copyBtn.disabled = false;
} catch (err) {
alert("Error reading PDF: " + err.message);
}
};
reader.readAsArrayBuffer(file);
});
copyBtn.addEventListener("click", () => {
const text = metadataOutput.textContent;
if (!text) return;
navigator.clipboard.writeText(text).catch((err) => {
alert("Could not copy: " + err);
});
});
downloadBtn.addEventListener("click", () => {
const data = metadataOutput.textContent;
const blob = new Blob([data], { type: "application/json" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "metadata.json";
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
});
clearBtn.addEventListener("click", () => {
metadataOutput.textContent = "";
fileInput.value = "";
downloadBtn.disabled = true;
copyBtn.disabled = true;
});
</script>
</head>
<body>
<a href="index.html" class="back-link">← Back to Browser Tools</a>
<h1>PDF Metadata Viewer</h1>
<div class="controls">
<div>
<label for="fileInput" class="btn primary">Load PDF</label>
<input type="file" id="fileInput" accept="application/pdf" />
</div>
<button class="btn secondary" id="clearBtn">Clear</button>
</div>
<div class="panel" id="metadataPanel">
<h2>Metadata</h2>
<pre id="metadataOutput"></pre>
<div class="info">
Upload a PDF file to extract and view its metadata information including
title, author, creation date, and more.
</div>
<div class="actions">
<button class="btn primary" id="downloadBtn" disabled>
Download JSON
</button>
<button class="btn primary" id="copyBtn" disabled>
Copy to Clipboard
</button>
</div>
</div>
</body>
</html>