-
Notifications
You must be signed in to change notification settings - Fork 1
/
pdfcheck.js
310 lines (274 loc) · 9.6 KB
/
pdfcheck.js
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
(function pdfCheck() {
// Utility functions for UI updates and event handling
const ui = {
showFiles: function (files) {
const label = document.querySelector("#dropzone label");
const fileName =
files.length > 1
? document
.querySelector('input[type="file"]')
.getAttribute("data-multiple-caption")
.replace("{count}", files.length)
: files[0].name;
label.textContent = fileName;
},
toggleButtonDisplay: function (display = "none") {
const button = document.querySelector('#dropzone button[type="submit"]');
button.style.display = display;
},
updateDragState: function (isDragging) {
const form = document.querySelector("#dropzone");
form.classList.toggle("is-dragover", isDragging);
},
clearReport: function () {
document.getElementById("report").textContent = "";
},
addFlag: function (className, markup) {
const tempNode = document.createElement("p");
tempNode.className = `flag ${className}`;
tempNode.innerHTML = markup;
document.getElementById("report").appendChild(tempNode);
},
};
// Core functionalities
const fileOps = {
processStart: function (e) {
ui.showFiles(e.target.files);
},
submitStart: function (e) {
e.preventDefault();
this.processFiles(
e.target.form.querySelector('input[type="file"]').files,
);
},
fileDrop: function (e) {
e.preventDefault();
const droppedFiles = e.dataTransfer.files;
ui.toggleButtonDisplay("none");
ui.showFiles(droppedFiles);
this.processFiles(droppedFiles);
},
processFiles: function (files) {
ui.clearReport();
this.readMultiFiles(files);
},
readMultiFiles: function (files) {
const reader = new FileReader();
const readFile = (index) => {
if (index >= files.length) return;
const file = files[index];
reader.onload = (e) => {
this.runCheck(file, e.target.result, index + 1);
readFile(index + 1);
};
reader.readAsText(file);
};
readFile(0);
},
runCheck: function (file, fileData, fileNumber) {
let valid;
buildHeading(file, fileNumber);
valid = validatePDF(fileData);
if (valid === true) {
findCreatorTool(fileData);
findProducer(fileData);
findTitle(fileData);
findTags(fileData);
findLang(fileData);
findMark(fileData);
findUA(fileData);
}
},
};
// Event listeners setup
function setupEventListeners() {
const form = document.querySelector("#dropzone");
const input = form.querySelector('input[type="file"]');
const button = form.querySelector('button[type="submit"]');
input.addEventListener("change", fileOps.processStart);
button.addEventListener("click", fileOps.submitStart.bind(fileOps));
if (isAdvancedUpload()) {
["dragover", "dragenter"].forEach((event) => {
form.addEventListener(
event,
(e) => {
e.preventDefault();
ui.updateDragState(true);
},
false,
);
});
["dragleave", "dragend", "drop"].forEach((event) => {
form.addEventListener(
event,
(e) => {
e.preventDefault();
ui.updateDragState(false);
},
false,
);
});
form.addEventListener("drop", fileOps.fileDrop.bind(fileOps));
}
// Firefox focus bug fix for file input
input.addEventListener("focus", () => input.classList.add("has-focus"));
input.addEventListener("blur", () => input.classList.remove("has-focus"));
}
// Utility function to check for advanced upload features
function isAdvancedUpload() {
const div = document.createElement("div");
return (
("draggable" in div || ("ondragstart" in div && "ondrop" in div)) &&
"FormData" in window &&
"FileReader" in window
);
}
// Initial setup call
setupEventListeners();
// Check if valid PDF file (read first 8 bytes, match regex)
function validatePDF(fileData) {
// Match PDF version from PDF-1.0 through PDF-3.9
const regexHeader = /%PDF-(1\.[0-9]|2\.[0-9]|3\.[0-9])/;
const dataHeader = fileData.substr(0, 8);
const matchHeader = regexHeader.exec(dataHeader);
if (!matchHeader) {
const markup = "<strong>Not a valid PDF file</strong>";
ui.addFlag("default", markup);
return false;
}
const markup = `<span>PDF Version:</span> <strong>${matchHeader[1]}</strong>`;
ui.addFlag("default", markup);
return true;
}
// Check if StructTreeRoot is set and count tags
function findTags(fileData) {
const regexTree = /StructTreeRoot\s(\d*)\s(\d*)/;
const matchTree = regexTree.exec(fileData);
let markup;
if (matchTree) {
markup = `<span><a href="#help-tagged">Tagged</a></span> <strong>Yes (${matchTree[1]} tags)</strong>`;
ui.addFlag("success", markup);
} else {
markup =
'<span><a href="#help-tagged">Tagged</a></span> <strong>No</strong>';
ui.addFlag("failure", markup);
}
}
// Check if Lang is set, display value if set
function findLang(fileData) {
const regexLang = /Lang(?:<|\()(.*?)(?:>|\))/;
const matchLang = regexLang.exec(fileData);
let markup;
if (matchLang && matchLang[1]) {
let languageCode = matchLang[1];
// Check and decode hex string format for language code
if (/^[\da-fA-F]+$/.test(languageCode)) {
languageCode = decodeHex(languageCode);
}
markup = `<span><a href="#help-language">Language</a></span> <strong>${languageCode}</strong>`;
ui.addFlag("success", markup);
} else {
markup =
'<span><a href="#help-language">Language</a></span> <strong>not set</strong>';
ui.addFlag("failure", markup);
}
}
// Helper function to decode hex string to ASCII
function decodeHex(hexString) {
const hexCodePairs = hexString.match(/.{1,2}/g);
return hexCodePairs
.map((pair) => String.fromCharCode(parseInt(pair, 16)))
.join("");
}
// Check MarkInfo exists and whether true or false
function findMark(fileData) {
const regexMarked = /<<\/Marked (true|false)/;
const matchMarked = regexMarked.exec(fileData);
let markup;
if (matchMarked) {
const isMarked = matchMarked[1] === "true";
markup = `<span><a href="#help-marked">Marked</a></span> <strong>${isMarked ? "True" : "False"}</strong>`;
ui.addFlag(isMarked ? "success" : "warning", markup);
} else {
markup =
'<span><a href="#help-marked">Marked</a></span> <strong>No</strong>';
ui.addFlag("failure", markup);
}
}
// Check for PDF/UA identifier
function findUA(fileData) {
const regexPDFUA = /<pdfaSchema:prefix>pdfuaid<\/pdfaSchema:prefix>/;
const matchPDFUA = regexPDFUA.exec(fileData);
let markup;
if (matchPDFUA) {
markup = `<span><a href="#help-pdfua">PDF/UA identifier</a></span> <strong>Yes</strong>`;
ui.addFlag("success", markup);
} else {
markup = `<span><a href="#help-pdfua">PDF/UA identifier</a></span> <strong>Not set</strong>`;
ui.addFlag("warning", markup);
}
}
// Check for DisplayDocTitle and dc:title
function findTitle(fileData) {
const regexTitle =
/<dc:title>[\s\S]*?<rdf:Alt>([\s\S]*?)<\/rdf:Alt>[\s\S]*?<\/dc:title>/;
const matchTitle = regexTitle.exec(fileData);
let markup;
if (matchTitle && matchTitle[1].trim()) {
const isNotEmptyTag = !/<rdf:li xml:lang="x-default"\/>/.test(
matchTitle[1],
);
markup = `<span><a href="#help-title"">Document Title</a></span> <strong>${isNotEmptyTag ? matchTitle[1].trim() : "Empty"}</strong>`;
ui.addFlag(isNotEmptyTag ? "default" : "warning", markup);
} else {
markup =
'<span><a href="#help-title"">Document Title</a></span> <strong>Not set</strong>';
ui.addFlag("failure", markup);
}
}
// Check for xmp:CreatorTool
function findCreatorTool(fileData) {
const regexTitle =
/<xmp:CreatorTool>([\s\S]*?)<\/xmp:CreatorTool>/;
const matchTitle = regexTitle.exec(fileData);
let markup;
if (matchTitle && matchTitle[1].trim()) {
markup = `<span>Creator Tool</span> <strong>${matchTitle[1].trim()}</strong>`;
ui.addFlag("default", markup);
} else {
markup =
'<span>Creator Tool</span> <strong>Not set</strong>';
ui.addFlag("warning", markup);
}
}
// Check for pdf:Producer
function findProducer(fileData) {
const regexTitle =
/<pdf:Producer>([\s\S]*?)<\/pdf:Producer>/;
const matchTitle = regexTitle.exec(fileData);
let markup;
if (matchTitle && matchTitle[1].trim()) {
markup = `<span>Producer</span> <strong>${matchTitle[1].trim()}</strong>`;
ui.addFlag("default", markup);
} else {
markup =
'<span>Producer</span> <strong>Not set</strong>';
ui.addFlag("warning", markup);
}
}
// Build file heading - ex: 1. document.pdf [PDF - 236 KB]
function buildHeading(file, fileNumber) {
const fileExt = file.name.split(".").pop().toUpperCase();
let fileSize = file.size / 1024; // KB as default unit
let fileSizeSuffix = "KB";
if (fileSize > 1024) {
fileSize = fileSize / 1024; // Convert KB to MB
fileSizeSuffix = "MB";
}
fileSize =
fileSizeSuffix === "MB" ? fileSize.toFixed(1) : Math.ceil(fileSize);
const fileLabel = `[${fileExt} - ${fileSize}${fileSizeSuffix}]`;
const markup = `${fileNumber}. ${file.name} <small>${fileLabel}</small>`;
ui.addFlag("title", markup);
}
})();