-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
482 lines (432 loc) · 14.6 KB
/
Copy pathindex.html
File metadata and controls
482 lines (432 loc) · 14.6 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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale = 1.0, maximum-scale = 1.0, user-scalable=no">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.1.266/pdf.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.7/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-LN+7fdVzj6u52u30Kp6M/trliBMCMKTyK833zpbD+pXdCLuTusPj697FH4R/5mcr" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.13.1/font/bootstrap-icons.min.css">
<style type="text/css">
#pdf-main-container {
margin: 20px auto;
}
#pdf-loader {
text-align: center;
color: #999999;
font-size: 13px;
line-height: 100px;
}
#pdf-contents {
}
#canvas-container{
width: 100%;
text-align: center;
margin: auto;
}
canvas {
border: 1px solid rgba(0, 0, 0, 0.2);
box-sizing: border-box;
}
#page-loader {
height: 100px;
line-height: 100px;
text-align: center;
color: #999999;
font-size: 13px;
}
.hidden {
display: none;
}
.uploader {
display: block;
clear: both;
margin: 0 auto;
width: 100%;
max-width: 600px;
label {
float: left;
clear: both;
width: 100%;
padding: 2rem 1.5rem;
text-align: center;
background: #fff;
border-radius: 7px;
border: 3px solid #eee;
transition: all .2s ease;
user-select: none;
}
#messages {
border-radius: 7px;
background: #ced4da;
}
input[type="file"] {
display: none;
}
</style>
</head>
<body>
<h2 style="text-align:center;">PDF Redactor JS</h2>
<div id="response" class="bg-light text-center my-3 hidden">
<div class="row">
<div class="col">
<strong id="messages"></strong>
<button type="button" class="btn btn-outline-primary" aria-label="Close" onclick="window.location.reload();"><i class="bi bi-x"></i></button>
</div>
</div>
</div>
<div id="upload-container" class="container text-center">
<div class="row">
<form id="file-upload-form" class="uploader">
<input id="file-upload" type="file" name="fileUpload" accept="application/pdf" />
<label for="file-upload" id="file-drag">
<div id="start">
<i class="fa fa-download" aria-hidden="true"></i>
<div>Select a file or drag here</div>
<span id="file-upload-btn" class="btn btn-primary">Select a file</span>
</div>
</label>
</form>
</div>
</div>
<div id="controls-container" class="container text-center hidden">
<div class="row justify-content-md-center">
<div class="col">
<div class="btn-group" role="group">
<button id="pdf-undo" class="btn btn-outline-primary" onclick="undoAction()"><i class="bi bi-arrow-counterclockwise"></i> Undo</button>
<button id="pdf-redo" class="btn btn-outline-primary" ><i class="bi bi-arrow-clockwise"></i> Redo</button>
</div>
</div>
<div class="col">
<button id="pdf-prev" class="btn btn-dark"><i class="bi bi-arrow-left"></i></button>
</div>
<div class="col">
<span id="page-count-container">Page <span id="pdf-current-page"></span> of <span id="pdf-total-pages"></span></span>
</div>
<div class="col">
<button id="pdf-next" class="btn btn-dark"><i class="bi bi-arrow-right"></i></button>
</div>
<div class="col">
<button id="download-image" class="btn btn-primary"><i class="bi bi-file-earmark-arrow-down"></i> Download PDF</button>
</div>
</div>
</div>
<div id="pdf-main-container">
<div id="pdf-contents">
<div id="pdf-loader">Loading document ...</div>
<canvas id="pdf-canvas"></canvas>
<div id="page-loader">Loading page ...</div>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.debug.js"
integrity="sha384-NaWTHo/8YCBYJ59830LTz/P4aQZK1sS0SneOgAvhsIl3zBu8r9RevNg5lHCHAuQ/"
crossorigin="anonymous"></script>
<script>
console.log("Upload Initialised");
var fileSelect = document.getElementById('file-upload'),
fileDrag = document.getElementById('file-drag'),
submitButton = document.getElementById('submit-button');
fileSelect.addEventListener('change', fileSelectHandler, false);
// Is XHR2 available?
var xhr = new XMLHttpRequest();
if (xhr.upload) {
// File Drop
fileDrag.addEventListener('dragover', fileDragHover, false);
fileDrag.addEventListener('dragleave', fileDragHover, false);
fileDrag.addEventListener('drop', fileSelectHandler, false);
}
function fileDragHover(e) {
var fileDrag = document.getElementById('file-drag');
e.stopPropagation();
e.preventDefault();
fileDrag.className = (e.type === 'dragover' ? 'hover' : 'modal-body file-upload');
}
function fileSelectHandler(e) {
// Fetch FileList object
var files = e.target.files || e.dataTransfer.files;
// Cancel event and hover styling
fileDragHover(e);
// Process File object
file = files[0]
document.getElementById('messages').innerHTML = encodeURI(file.name);
document.getElementById('response').classList.remove("hidden");
document.getElementById('upload-container').classList.add("hidden");
document.getElementById('controls-container').classList.remove("hidden");
showPDF(URL.createObjectURL(file));
}
pdfjsLib.GlobalWorkerOptions.workerSrc = "https://cdn.jsdelivr.net/npm/pdfjs-dist@2.1.266/build/pdf.worker.min.js";
var __PDF_DOC,
__CURRENT_PAGE,
__TOTAL_PAGES,
__PAGE_RENDERING_IN_PROGRESS = 0,
__CANVAS = $('#pdf-canvas').get(0),
__CANVAS_CTX = __CANVAS.getContext('2d'),
renderContext, allPages=[];
const img = new Image;
var buff = [], storedRects=[]; //so far we only deal with single page editor, if multipaged. we will need to update
//to update the redo and undo structure, with multi buffs, or we just commit the change, and forget about re-editing
// Upon click this should should trigger click on the #file-to-upload file input element
function cleanCanvas(){
__CANVAS_CTX.clearRect(0, 0, __CANVAS_CTX.canvas.width, __CANVAS_CTX.canvas.height);
img.src = __CANVAS.toDataURL();
}
// Previous action on the PDF
function undoAction(){
if(storedRects.length>0){
buff.push(storedRects.pop());
}
};
function preloadAllPages() {
allPages = [];
for (let i = 1 ; i <= __TOTAL_PAGES; i++) {
__PDF_DOC.getPage(i).then(function (page) {
const width = window.screen.availWidth - 50;
// As the canvas is of a fixed width we need to set the scale of the viewport accordingly
const scale_required = width / page.getViewport(1).width;
// Get viewport of the page at required scale //the scale is the % in the pdf file
const viewport = page.getViewport(scale_required);
const canv = document.createElement('canvas');
const canv_con = canv.getContext('2d');
// Set canvas width
// Set canvas height
canv.width = width;
canv.height = viewport.height;
const renderContext = {
canvasContext: canv_con,
viewport: viewport
};
// Render the page contents in the canvas
page.render(renderContext).then(function () {
allPages.push(canv);
});
});
}
}
// Next action on the PDF
$("#pdf-redo").on('click', function () {
if(buff.length>0){
storedRects.push(buff.pop());
}
});
// Previous page of the PDF
$("#pdf-prev").on('click', function () {
if (__CURRENT_PAGE != 1){
cleanCanvas();
showPage(--__CURRENT_PAGE, true);
}
});
// Next page of the PDF
$("#pdf-next").on('click', function () {
if (__CURRENT_PAGE != __TOTAL_PAGES){
cleanCanvas();
showPage(++__CURRENT_PAGE, true);
}
});
// Download button
$("#download-image").on('click', function () {
let width = __CANVAS.width;
let height = __CANVAS.height;
//set the height and width for the output pdf file
if(width > height){
pdf = new jsPDF('l', 'px', [width, height]);
}
else{
pdf = new jsPDF('p', 'px', [height, width]);
}
width = pdf.internal.pageSize.getWidth();
height = pdf.internal.pageSize.getHeight();
for (let i = 0; i < allPages.length; i++) {
pdf.addImage(allPages[i], 'PNG', 0, 0, width, height);
if (i < (allPages.length - 1)) {
pdf.addPage(); // this adds a new page
}
}
let filename = document.getElementById('messages').innerHTML.replace('.pdf', '-redacted.pdf')
pdf.save(filename);
});
function showPDF(pdf_url) {
pdfjsLib.getDocument(pdf_url).promise.then(function (pdf_doc) {
__PDF_DOC = pdf_doc;
__TOTAL_PAGES = __PDF_DOC.numPages;
$("#pdf-total-pages").text(__TOTAL_PAGES);
// Show the first page
showPage(1, false);
preloadAllPages();
}).catch(function (error) {
// If error re-show the upload button
alert(error.message);
});
}
function hideCanvas(){
// Disable Prev & Next buttons while page is being loaded
$("#pdf-next, #pdf-prev, #pdf-redo, #pdf-undo").attr('disabled', 'disabled');
// While page is being rendered hide the canvas and show a loading message
$("#pdf-canvas").hide();
$("#pdf-loader").hide();
$("#page-count-container").hide();
$("#download-image").hide();
$("#page-loader").hide();
}
function showCanvas() {
// Re-enable Prev & Next buttons
$('.pdf-buttons').show();
$("#pdf-next, #pdf-prev, #pdf-redo, #pdf-undo").removeAttr('disabled');
// Show the canvas and hide the page loader
$("#pdf-canvas").show();
$("#pdf-loader").hide();
$("#page-count-container").show();
$("#download-image").show();
$("#page-loader").hide();
}
function showPage(page_no, prev) {
storedRects=[];
buff = [];
__PAGE_RENDERING_IN_PROGRESS = 1;
__CURRENT_PAGE = page_no;
// Update current page in HTML
$("#page-loader").show();
$("#pdf-current-page").text(page_no);
// Fetch the page
__PDF_DOC.getPage(page_no).then(function (page) {
const width = window.screen.availWidth-50;
// As the canvas is of a fixed width we need to set the scale of the viewport accordingly
var scale_required = width / page.getViewport(1).width;
// Get viewport of the page at required scale //the scale is the % in the pdf file
var viewport = page.getViewport(scale_required);
// Set canvas width
// Set canvas height
__CANVAS.width = width;
__CANVAS.height = viewport.height;
renderContext = {
canvasContext: __CANVAS_CTX,
viewport: viewport
};
// Render the page contents in the canvas
page.render(renderContext).then(function () {
__PAGE_RENDERING_IN_PROGRESS = 0;
if (prev) {
img.src = allPages[page_no - 1].toDataURL();
} else {
img.src = __CANVAS.toDataURL();
}
})
});
}
$(document).ready(function selection() {
"use strict";
hideCanvas();
requestAnimationFrame(mainLoop);
const canvas = document.getElementById("pdf-canvas");
const ctx = canvas.getContext("2d");
const baseImage = img;
const rect = (() => {
var x1, y1, x2, y2;
var show = false;
function fix() {
rect.x = Math.min(x1, x2);
rect.y = Math.min(y1, y2);
rect.w = Math.max(x1, x2) - Math.min(x1, x2);
rect.h = Math.max(y1, y2) - Math.min(y1, y2);
}
function draw(ctx) {
ctx.fillRect(this.x, this.y, this.w, this.h);
}
const rect = {x: 0, y: 0, w: 0, h: 0, draw};
const API = {
restart(point) {
x2 = x1 = point.x;
y2 = y1 = point.y;
fix();
show = true;
},
update(point) {
x2 = point.x;
y2 = point.y;
fix();
show = true;
},
toRect() {
show = false;
return Object.assign({}, rect);
},
draw(ctx) {
if (show) {
rect.draw(ctx)
}
},
show: false,
}
return API;
})();
const mouse = {
button: false,
x: 0,
y: 0,
down: false,
up: false,
element: null,
event(e) {
var rectCanv = canvas.getBoundingClientRect();
const m = mouse;
m.x = (e.clientX - rectCanv.left) / (rectCanv.right - rectCanv.left) * canvas.width;
m.y = (e.clientY - rectCanv.top) / (rectCanv.bottom - rectCanv.top) * canvas.height;
const prevButton = m.button;
m.button = e.type === "mousedown" ? true : e.type === "mouseup" ? false : mouse.button;
if (!prevButton && m.button) {
m.down = true;
}
if (prevButton && !m.button) {
m.up = true;
}
},
start(element) {
mouse.element = element;
"down,up,move".split(",").forEach(name => document.addEventListener("mouse" + name, mouse.event));
}
}
mouse.start(canvas);
function draw() {
ctx.drawImage(baseImage, 0, 0, ctx.canvas.width, ctx.canvas.height);
ctx.lineWidth = 1;
ctx.strokeStyle = "black";
storedRects.forEach(rect => rect.draw(ctx));
ctx.strokeStyle = "red";
rect.draw(ctx);
}
function mainLoop() {
var refresh = true;
if(__TOTAL_PAGES == allPages.length)
showCanvas();
if (refresh || mouse.down || mouse.up || mouse.button) {
refresh = false;
if (mouse.down) {
mouse.down = false;
rect.restart(mouse);
} else if (mouse.button) {
rect.update(mouse);
} else if (mouse.up) {
mouse.up = false;
rect.update(mouse);
let tempRect = rect.toRect();
const m = mouse;
if(isFinite(tempRect.x) && isFinite(tempRect.y) && isFinite(tempRect.w) && isFinite(tempRect.h)
&& tempRect.w!=0 && tempRect.h != 0
&& m.x >0 && m.x<__CANVAS.width && m.y>0 && m.y<__CANVAS.height) {//only if the mouse land in the canvas,ok
storedRects.push(tempRect);
buff = [] //once the editor start to edit, the editor commits to the change so far, so redo is emptied out
const canv = document.createElement('canvas');
const canv_con = canv.getContext('2d');
canv.width = __CANVAS_CTX.canvas.width;
canv.height = __CANVAS_CTX.canvas.height;
canv_con.drawImage(__CANVAS, 0, 0, __CANVAS_CTX.canvas.width, __CANVAS_CTX.canvas.height);
allPages[__CURRENT_PAGE - 1] = canv;
}
}
draw();
}
requestAnimationFrame(mainLoop)
}
})
</script>
</body>
</html>