-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
48 lines (39 loc) · 1.49 KB
/
script.js
File metadata and controls
48 lines (39 loc) · 1.49 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
function formatArray(arr, perLine = 9) {
const lines = [];
for (let i = 0; i < arr.length; i += perLine) {
const chunk = arr.slice(i, i + perLine)
.map(e => `"${e}"`) // add quotes
.join(", ");
lines.push(" " + chunk);
}
return "[\n" + lines.join(",\n") + "\n]";
}
const uploadInput = document.getElementById('pdf-upload');
const output = document.getElementById('output');
uploadInput.addEventListener('change', async (event) => {
const file = event.target.files[0];
if (!file) return;
try {
const arrayBuffer = await file.arrayBuffer();
const pdf = await pdfjsLib.getDocument({ data: arrayBuffer }).promise;
const page = await pdf.getPage(1);
const textContent = await page.getTextContent();
const textItems = textContent.items.map(item => item.str);
const slicedItems = textItems.slice(188);
if (slicedItems.length === 81) {
const [removedItem] = slicedItems.splice(79, 1);
slicedItems.splice(9, 0, removedItem);
}
console.log(slicedItems)
output.textContent = formatArray(slicedItems);
} catch (err) {
output.textContent = 'Error reading PDF: ' + err.message;
}
});
const copyBtn = document.getElementById('copyBtn');
copyBtn.addEventListener('click', () => {
output.select();
output.setSelectionRange(0, 99999);
navigator.clipboard.writeText(output.value)
.catch(err => alert('Failed to copy: ' + err));
});