-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
349 lines (295 loc) · 12.2 KB
/
script.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
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
document.getElementById('pdf-upload').addEventListener('change', handleFileUpload);
document.getElementById('split-button').addEventListener('click', splitPdf);
['top-percent', 'bottom-percent', 'left-percent', 'right-percent'].forEach(id => {
document.getElementById(id).addEventListener('input', updateCropSquare);
});
const cropSquare = document.getElementById('crop-square');
const handles = document.querySelectorAll('.handle');
const canvasContainer = document.getElementById('canvas-container');
const canvas = document.getElementById('pdf-canvas');
let isDragging = false;
let isResizing = false;
let resizeDir = '';
let initialMouseX, initialMouseY, initialWidth, initialHeight, initialLeft, initialTop;
// Hide canvas container and crop square initially
canvasContainer.style.display = 'none';
cropSquare.style.display = 'none';
async function handleFileUpload(event) {
const file = event.target.files[0];
if (file && file.type === 'application/pdf') {
const reader = new FileReader();
reader.onload = async function(e) {
const typedarray = new Uint8Array(e.target.result);
const pdf = await pdfjsLib.getDocument({ data: typedarray }).promise;
processPDF(pdf);
displayFirstPage(pdf);
canvasContainer.style.display = 'block';
cropSquare.style.display = 'block';
correctNegativeValues(); // Correct negative values after displaying the first page
};
reader.readAsArrayBuffer(file);
}
}
async function processPDF(pdf) {
const pagePromises = [];
for (let i = 1; i <= pdf.numPages; i++) {
pagePromises.push(pdf.getPage(i).then(page => page.getViewport({ scale: 1 })));
}
const viewports = await Promise.all(pagePromises);
const sizes = viewports.map(viewport => ({ width: viewport.width, height: viewport.height }));
displaySizes(sizes);
}
function displaySizes(sizes) {
const outputDiv = document.getElementById('output');
outputDiv.innerHTML = ''; // Clear previous output
const allSameSize = sizes.every(size => size.width === sizes[0].width && size.height === sizes[0].height);
if (allSameSize) {
outputDiv.textContent = `All pages have the same size: ${sizes[0].width} x ${sizes[0].height} pixels.`;
} else {
const table = document.createElement('table');
table.border = 1;
const headerRow = document.createElement('tr');
headerRow.innerHTML = '<th>Page Number</th><th>Width (px)</th><th>Height (px)</th>';
table.appendChild(headerRow);
sizes.forEach((size, index) => {
const row = document.createElement('tr');
row.innerHTML = `<td>${index + 1}</td><td>${size.width}</td><td>${size.height}</td>`;
table.appendChild(row);
});
outputDiv.appendChild(table);
}
}
async function splitPdf() {
const pdfFile = document.getElementById('pdf-upload').files[0];
const progressContainer = document.getElementById('progress-container');
const progressBar = document.getElementById('progress-bar');
const progressPercentage = document.getElementById('progress-percentage');
const topPercent = parseFloat(document.getElementById('top-percent').value) / 100;
const bottomPercent = parseFloat(document.getElementById('bottom-percent').value) / 100;
const leftPercent = parseFloat(document.getElementById('left-percent').value) / 100;
const rightPercent = parseFloat(document.getElementById('right-percent').value) / 100;
if (!pdfFile) {
alert('Please upload a PDF file.');
return;
}
progressContainer.style.display = 'flex';
progressBar.value = 0;
progressPercentage.textContent = '0%';
const arrayBuffer = await pdfFile.arrayBuffer();
const pdfDoc = await PDFLib.PDFDocument.load(arrayBuffer);
const numPages = pdfDoc.getPageCount();
const newPdfDoc = await PDFLib.PDFDocument.create();
for (let i = 0; i < numPages; i++) {
const [originalPage] = await newPdfDoc.copyPages(pdfDoc, [i]);
const { width, height } = originalPage.getSize();
const newWidth = width * (1 - leftPercent - rightPercent);
const newHeight = height * (1 - topPercent - bottomPercent);
const cropLeft = width * leftPercent;
const cropBottom = height * bottomPercent;
originalPage.setCropBox(cropLeft, cropBottom, newWidth, newHeight);
newPdfDoc.addPage(originalPage);
// Update progress
const progress = Math.round(((i + 1) / numPages) * 100);
progressBar.value = progress;
progressPercentage.textContent = `${progress}%`;
}
const newPdfBytes = await newPdfDoc.save();
const blob = new Blob([newPdfBytes], { type: 'application/pdf' });
const url = URL.createObjectURL(blob);
// Display new PDF in an iframe
const resultDiv = document.getElementById('result');
resultDiv.innerHTML = `<iframe src="${url}" width="100%" height="500px"></iframe>`;
// Hide progress bar after completion
progressContainer.style.display = 'none';
}
async function displayFirstPage(pdf) {
const page = await pdf.getPage(1);
const viewport = page.getViewport({ scale: 1 });
const context = canvas.getContext('2d');
canvas.width = viewport.width;
canvas.height = viewport.height;
const renderContext = {
canvasContext: context,
viewport: viewport
};
await page.render(renderContext).promise();
// Place the crop square in the center with default 0% crop ratios
const width = canvas.width;
const height = canvas.height;
const topPercent = -0;
const bottomPercent = -0;
const leftPercent = -0;
const rightPercent = -0;
cropSquare.style.width = `${width * (1 - leftPercent - rightPercent)}px`;
cropSquare.style.height = `${height * (1 - topPercent - bottomPercent)}px`;
cropSquare.style.left = `${width * leftPercent}px`;
cropSquare.style.top = `${height * topPercent}px`;
updateInputsFromSquare();
}
function updateCropSquare() {
let topPercent = parseFloat(document.getElementById('top-percent').value) / 100;
let bottomPercent = parseFloat(document.getElementById('bottom-percent').value) / 100;
let leftPercent = parseFloat(document.getElementById('left-percent').value) / 100;
let rightPercent = parseFloat(document.getElementById('right-percent').value) / 100;
topPercent = Math.max(0, topPercent);
bottomPercent = Math.max(0, bottomPercent);
leftPercent = Math.max(0, leftPercent);
rightPercent = Math.max(0, rightPercent);
const width = canvas.width;
const height = canvas.height;
cropSquare.style.width = `${width * (1 - leftPercent - rightPercent)}px`;
cropSquare.style.height = `${height * (1 - topPercent - bottomPercent)}px`;
cropSquare.style.left = `${width * leftPercent}px`;
cropSquare.style.top = `${height * topPercent}px`;
document.getElementById('top-percent').value = (topPercent * 100).toFixed(2);
document.getElementById('bottom-percent').value = (bottomPercent * 100).toFixed(2);
document.getElementById('left-percent').value = (leftPercent * 100).toFixed(2);
document.getElementById('right-percent').value = (rightPercent * 100).toFixed(2);
}
const mouseMoveHandler = (e) => {
if (isDragging) {
const deltaX = e.clientX - initialMouseX;
const deltaY = e.clientY - initialMouseY;
let newLeft = initialLeft + deltaX;
let newTop = initialTop + deltaY;
// Keep square within the canvas
newLeft = Math.max(0, Math.min(newLeft, canvas.width - cropSquare.offsetWidth));
newTop = Math.max(0, Math.min(newTop, canvas.height - cropSquare.offsetHeight));
cropSquare.style.left = newLeft + 'px';
cropSquare.style.top = newTop + 'px';
updateInputsFromSquare();
} else if (isResizing) {
const deltaX = e.clientX - initialMouseX;
const deltaY = e.clientY - initialMouseY;
let newWidth = initialWidth;
let newHeight = initialHeight;
let newLeft = initialLeft;
let newTop = initialTop;
switch (resizeDir) {
case 'top-left':
newWidth = initialWidth - deltaX;
newHeight = initialHeight - deltaY;
newLeft = initialLeft + deltaX;
newTop = initialTop + deltaY;
break;
case 'top-right':
newWidth = initialWidth + deltaX;
newHeight = initialHeight - deltaY;
newTop = initialTop + deltaY;
break;
case 'bottom-left':
newWidth = initialWidth - deltaX;
newHeight = initialHeight + deltaY;
newLeft = initialLeft + deltaX;
break;
case 'bottom-right':
newWidth = initialWidth + deltaX;
newHeight = initialHeight + deltaY;
break;
case 'middle-top':
newHeight = initialHeight - deltaY;
newTop = initialTop + deltaY;
break;
case 'middle-right':
newWidth = initialWidth + deltaX;
break;
case 'middle-bottom':
newHeight = initialHeight + deltaY;
break;
case 'middle-left':
newWidth = initialWidth - deltaX;
newLeft = initialLeft + deltaX;
break;
}
// Constrain the resizing to the canvas
if (newLeft < 0) {
newWidth = initialWidth + initialLeft;
newLeft = 0;
}
if (newTop < 0) {
newHeight = initialHeight + initialTop;
newTop = 0;
}
if (newWidth + newLeft > canvas.width) {
newWidth = canvas.width - newLeft;
}
if (newHeight + newTop > canvas.height) {
newHeight = canvas.height - newTop;
}
if (newWidth > 10) {
cropSquare.style.width = newWidth + 'px';
cropSquare.style.left = newLeft + 'px';
}
if (newHeight > 10) {
cropSquare.style.height = newHeight + 'px';
cropSquare.style.top = newTop + 'px';
}
updateInputsFromSquare();
}
};
const mouseUpHandler = () => {
isDragging = false;
isResizing = false;
cropSquare.style.cursor = 'default';
document.removeEventListener('mousemove', mouseMoveHandler);
document.removeEventListener('mouseup', mouseUpHandler);
};
cropSquare.addEventListener('mousedown', (e) => {
initialMouseX = e.clientX;
initialMouseY = e.clientY;
initialLeft = cropSquare.offsetLeft;
initialTop = cropSquare.offsetTop;
initialWidth = cropSquare.offsetWidth;
initialHeight = cropSquare.offsetHeight;
isDragging = true;
cropSquare.style.cursor = 'move';
document.addEventListener('mousemove', mouseMoveHandler);
document.addEventListener('mouseup', mouseUpHandler);
});
handles.forEach(handle => {
handle.addEventListener('mousedown', (e) => {
initialMouseX = e.clientX;
initialMouseY = e.clientY;
initialLeft = cropSquare.offsetLeft;
initialTop = cropSquare.offsetTop;
initialWidth = cropSquare.offsetWidth;
initialHeight = cropSquare.offsetHeight;
isResizing = true;
resizeDir = e.target.className.split(' ')[1];
cropSquare.style.cursor = window.getComputedStyle(e.target).cursor;
e.stopPropagation();
document.addEventListener('mousemove', mouseMoveHandler);
document.addEventListener('mouseup', mouseUpHandler);
});
});
function updateInputsFromSquare() {
const width = canvas.width;
const height = canvas.height;
let leftPercent = (cropSquare.offsetLeft / width) * 100;
let topPercent = (cropSquare.offsetTop / height) * 100;
let rightPercent = 100 - ((cropSquare.offsetLeft + cropSquare.offsetWidth) / width) * 100;
let bottomPercent = 100 - ((cropSquare.offsetTop + cropSquare.offsetHeight) / height) * 100;
leftPercent = Math.max(0, leftPercent);
topPercent = Math.max(0, topPercent);
rightPercent = Math.max(0, rightPercent);
bottomPercent = Math.max(0, bottomPercent);
document.getElementById('top-percent').value = topPercent.toFixed(2);
document.getElementById('bottom-percent').value = bottomPercent.toFixed(2);
document.getElementById('left-percent').value = leftPercent.toFixed(2);
document.getElementById('right-percent').value = rightPercent.toFixed(2);
}
function correctNegativeValues() {
let topPercent = parseFloat(document.getElementById('top-percent').value);
let bottomPercent = parseFloat(document.getElementById('bottom-percent').value);
let leftPercent = parseFloat(document.getElementById('left-percent').value);
let rightPercent = parseFloat(document.getElementById('right-percent').value);
topPercent = Math.max(0, topPercent);
bottomPercent = Math.max(0, bottomPercent);
leftPercent = Math.max(0, leftPercent);
rightPercent = Math.max(0, rightPercent);
document.getElementById('top-percent').value = topPercent.toFixed(2);
document.getElementById('bottom-percent').value = bottomPercent.toFixed(2);
document.getElementById('left-percent').value = leftPercent.toFixed(2);
document.getElementById('right-percent').value = rightPercent.toFixed(2);
updateCropSquare();
}