-
Notifications
You must be signed in to change notification settings - Fork 100
/
pdfannotate.js
366 lines (338 loc) · 10.6 KB
/
pdfannotate.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/**
* PDFAnnotate v1.0.1
* Author: Ravisha Heshan
*/
var PDFAnnotate = function (container_id, url, options = {}) {
this.number_of_pages = 0;
this.pages_rendered = 0;
this.active_tool = 1; // 1 - Free hand, 2 - Text, 3 - Arrow, 4 - Rectangle
this.fabricObjects = [];
this.fabricObjectsData = [];
this.color = '#212121';
this.borderColor = '#000000';
this.borderSize = 1;
this.font_size = 16;
this.active_canvas = 0;
this.container_id = container_id;
this.url = url;
this.pageImageCompression = options.pageImageCompression ?
options.pageImageCompression.toUpperCase() :
'NONE';
this.textBoxText = 'Sample Text';
this.format;
this.orientation;
var inst = this;
var loadingTask = pdfjsLib.getDocument(this.url);
loadingTask.promise.then(
function (pdf) {
var scale = options.scale ? options.scale : 1.3;
inst.number_of_pages = pdf.numPages;
for (var i = 1; i <= pdf.numPages; i++) {
pdf.getPage(i).then(function (page) {
if (typeof inst.format === 'undefined' ||
typeof inst.orientation === 'undefined') {
var originalViewport = page.getViewport({ scale: 1 });
inst.format = [originalViewport.width, originalViewport.height];
inst.orientation =
originalViewport.width > originalViewport.height ?
'landscape' :
'portrait';
}
var viewport = page.getViewport({ scale: scale });
var canvas = document.createElement('canvas');
document.getElementById(inst.container_id).appendChild(canvas);
canvas.className = 'pdf-canvas';
canvas.height = viewport.height;
canvas.width = viewport.width;
context = canvas.getContext('2d');
var renderContext = {
canvasContext: context,
viewport: viewport,
};
var renderTask = page.render(renderContext);
renderTask.promise.then(function () {
$('.pdf-canvas').each(function (index, el) {
$(el).attr('id', 'page-' + (index + 1) + '-canvas');
});
inst.pages_rendered++;
if (inst.pages_rendered == inst.number_of_pages) inst.initFabric();
});
});
}
},
function (reason) {
console.error(reason);
}
);
this.initFabric = function () {
var inst = this;
let canvases = $('#' + inst.container_id + ' canvas');
canvases.each(function (index, el) {
var background = el.toDataURL('image/png');
var fabricObj = new fabric.Canvas(el.id, {
freeDrawingBrush: {
width: 1,
color: inst.color,
},
});
inst.fabricObjects.push(fabricObj);
if (typeof options.onPageUpdated == 'function') {
fabricObj.on('object:added', function () {
var oldValue = Object.assign({}, inst.fabricObjectsData[index]);
inst.fabricObjectsData[index] = fabricObj.toJSON();
options.onPageUpdated(
index + 1,
oldValue,
inst.fabricObjectsData[index]
);
});
}
fabricObj.setBackgroundImage(
background,
fabricObj.renderAll.bind(fabricObj)
);
$(fabricObj.upperCanvasEl).click(function (event) {
inst.active_canvas = index;
inst.fabricClickHandler(event, fabricObj);
});
fabricObj.on('after:render', function () {
inst.fabricObjectsData[index] = fabricObj.toJSON();
fabricObj.off('after:render');
});
if (index === canvases.length - 1 && typeof options.ready === 'function') {
options.ready();
}
});
};
this.fabricClickHandler = function (event, fabricObj) {
var inst = this;
var toolObj;
if (inst.active_tool == 2) {
toolObj = new fabric.IText(inst.textBoxText, {
left: event.clientX - fabricObj.upperCanvasEl.getBoundingClientRect().left,
top: event.clientY - fabricObj.upperCanvasEl.getBoundingClientRect().top,
fill: inst.color,
fontSize: inst.font_size,
selectable: true,
});
} else if (inst.active_tool == 4) {
toolObj = new fabric.Rect({
left: event.clientX - fabricObj.upperCanvasEl.getBoundingClientRect().left,
top: event.clientY - fabricObj.upperCanvasEl.getBoundingClientRect().top,
width: 100,
height: 100,
fill: inst.color,
stroke: inst.borderColor,
strokeSize: inst.borderSize,
});
}
if (toolObj) {
fabricObj.add(toolObj);
}
};
};
PDFAnnotate.prototype.enableSelector = function () {
var inst = this;
inst.active_tool = 0;
if (inst.fabricObjects.length > 0) {
$.each(inst.fabricObjects, function (index, fabricObj) {
fabricObj.isDrawingMode = false;
});
}
};
PDFAnnotate.prototype.enablePencil = function () {
var inst = this;
inst.active_tool = 1;
if (inst.fabricObjects.length > 0) {
$.each(inst.fabricObjects, function (index, fabricObj) {
fabricObj.isDrawingMode = true;
});
}
};
PDFAnnotate.prototype.enableAddText = function (text) {
var inst = this;
inst.active_tool = 2;
if (typeof text === 'string') {
inst.textBoxText = text;
}
if (inst.fabricObjects.length > 0) {
$.each(inst.fabricObjects, function (index, fabricObj) {
fabricObj.isDrawingMode = false;
});
}
};
PDFAnnotate.prototype.enableRectangle = function () {
var inst = this;
var fabricObj = inst.fabricObjects[inst.active_canvas];
inst.active_tool = 4;
if (inst.fabricObjects.length > 0) {
$.each(inst.fabricObjects, function (index, fabricObj) {
fabricObj.isDrawingMode = false;
});
}
};
PDFAnnotate.prototype.enableAddArrow = function (onDrawnCallback = null) {
var inst = this;
inst.active_tool = 3;
if (inst.fabricObjects.length > 0) {
$.each(inst.fabricObjects, function (index, fabricObj) {
fabricObj.isDrawingMode = false;
new Arrow(fabricObj, inst.color, function () {
inst.active_tool = 0;
if (typeof onDrawnCallback === 'function') {
onDrawnCallback();
}
});
});
}
};
PDFAnnotate.prototype.addImageToCanvas = function () {
var inst = this;
var fabricObj = inst.fabricObjects[inst.active_canvas];
if (fabricObj) {
var inputElement = document.createElement('input');
inputElement.type = 'file';
inputElement.accept = '.jpg,.jpeg,.png,.PNG,.JPG,.JPEG';
inputElement.onchange = function () {
var reader = new FileReader();
reader.addEventListener(
'load',
function () {
inputElement.remove();
var image = new Image();
image.onload = function () {
fabricObj.add(new fabric.Image(image));
};
image.src = this.result;
},
false
);
reader.readAsDataURL(inputElement.files[0]);
};
document.getElementsByTagName('body')[0].appendChild(inputElement);
inputElement.click();
}
};
PDFAnnotate.prototype.deleteSelectedObject = function () {
var inst = this;
var activeObject = inst.fabricObjects[inst.active_canvas].getActiveObject();
if (activeObject) {
if (confirm('Are you sure ?')) {
inst.fabricObjects[inst.active_canvas].remove(activeObject);
}
}
};
PDFAnnotate.prototype.savePdf = function (fileName) {
var inst = this;
var format = inst.format || 'a4';
var orientation = inst.orientation || 'portrait';
if (!inst.fabricObjects.length) return;
var doc = new jspdf.jsPDF({ format, orientation });
if (typeof fileName === 'undefined') {
fileName = `${new Date().getTime()}.pdf`;
}
inst.fabricObjects.forEach(function (fabricObj, index) {
if (index != 0) {
doc.addPage(format, orientation);
doc.setPage(index + 1);
}
doc.addImage(
fabricObj.toDataURL({
format: 'png',
}),
inst.pageImageCompression == 'NONE' ? 'PNG' : 'JPEG',
0,
0,
doc.internal.pageSize.getWidth(),
doc.internal.pageSize.getHeight(),
`page-${index + 1}`,
['FAST', 'MEDIUM', 'SLOW'].indexOf(inst.pageImageCompression) >= 0 ?
inst.pageImageCompression :
undefined
);
if (index === inst.fabricObjects.length - 1) {
doc.save(fileName);
}
});
};
PDFAnnotate.prototype.setBrushSize = function (size) {
var inst = this;
$.each(inst.fabricObjects, function (index, fabricObj) {
fabricObj.freeDrawingBrush.width = parseInt(size, 10) || 1;
});
};
PDFAnnotate.prototype.setColor = function (color) {
var inst = this;
inst.color = color;
$.each(inst.fabricObjects, function (index, fabricObj) {
fabricObj.freeDrawingBrush.color = color;
});
};
PDFAnnotate.prototype.setBorderColor = function (color) {
var inst = this;
inst.borderColor = color;
};
PDFAnnotate.prototype.setFontSize = function (size) {
this.font_size = size;
};
PDFAnnotate.prototype.setBorderSize = function (size) {
this.borderSize = size;
};
PDFAnnotate.prototype.clearActivePage = function () {
var inst = this;
var fabricObj = inst.fabricObjects[inst.active_canvas];
var bg = fabricObj.backgroundImage;
if (confirm('Are you sure?')) {
fabricObj.clear();
fabricObj.setBackgroundImage(bg, fabricObj.renderAll.bind(fabricObj));
}
};
PDFAnnotate.prototype.serializePdf = function (callback) {
var inst = this;
var pageAnnotations = [];
inst.fabricObjects.forEach(function (fabricObject) {
fabricObject.clone(function (fabricObjectCopy) {
fabricObjectCopy.setBackgroundImage(null);
fabricObjectCopy.setBackgroundColor('');
pageAnnotations.push(fabricObjectCopy);
if (pageAnnotations.length === inst.fabricObjects.length) {
var data = {
page_setup: {
format: inst.format,
orientation: inst.orientation,
},
pages: pageAnnotations,
};
callback(JSON.stringify(data));
}
});
});
};
PDFAnnotate.prototype.loadFromJSON = function (jsonData) {
var inst = this;
var { page_setup, pages } = jsonData;
if (typeof pages === 'undefined') {
pages = jsonData;
}
if (
typeof page_setup === 'object' &&
typeof page_setup.format === 'string' &&
typeof page_setup.orientation === 'string'
) {
inst.format = page_setup.format;
inst.orientation = page_setup.orientation;
}
$.each(inst.fabricObjects, function (index, fabricObj) {
if (pages.length > index) {
fabricObj.loadFromJSON(pages[index], function () {
inst.fabricObjectsData[index] = fabricObj.toJSON();
});
}
});
};
PDFAnnotate.prototype.setDefaultTextForTextBox = function (text) {
var inst = this;
if (typeof text === 'string') {
inst.textBoxText = text;
}
};