-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheasyPDF.js
110 lines (98 loc) · 2.81 KB
/
easyPDF.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
function easyPDF(_base64, _title) {
var re = /(?<=\^PDF\^\^base64\^)(.*)(?<==)/;
_base64 = re.exec(_base64)[0]
// HTML definition of dialog elements
var dialog = '<div id="pdfDialog" title="'+_title+'">'+
'<label>Page: </label><label id="pageNum"></label><label> of </label><label id="pageLength"></label>'+
'<canvas id="pdfview"></canvas>'+
'</div>';
$('#pdfDialog').iziModal('close')
$("div[id=pdfDialog]").remove();
$(document.body).append(dialog);
// We need the javascript object of the canvas, not the jQuery reference
var canvas = document.getElementById('pdfview');
// Init page count
var page = 1;
// Dialog definition
// TODO: finish converting from popup to iziModal
$( "#pdfDialog" ).iziModal({
// Moves controls to top of dialog
open: function (event, ui) {
$(this).before($(this).parent().find('.ui-dialog-buttonpane'));
},
width: ($(window).width() / 2),
modal: false,
position: {
my: "top",
at: "top",
of: window,
collision: "none"
},
buttons: {
"Back": {
click: function () {
RenderPDF(-1)
},
text: 'Previous',
},
"Next": {
click: function () {
RenderPDF(1)
},
text: 'Next',
},
"Confirm": {
click: function () {
$(this).dialog("close");
$("#pdfDialog").remove()
},
text: 'Close',
}
}
});
// Init page number and the document
$('#pageNum').text(page);
RenderPDF(0);
// PDF.js control
function RenderPDF(pageNumber) {
var pdfData = atob(_base64);
pdfjsLib.disableWorker = true;
// Get current global page number, defaults to 1
displayNum = parseInt($('#pageNum').html())
pageNumber = parseInt(pageNumber)
var loadingTask = pdfjsLib.getDocument({data: pdfData});
loadingTask.promise.then(function(pdf) {
// Gets total page length of pdf
size = pdf.numPages;
$('#pageLength').text(size);
// Handling for changing pages
if(pageNumber == 1) {
pageNumber = displayNum + 1;
}
if(pageNumber == -1) {
pageNumber = displayNum - 1;
}
if(pageNumber == 0) {
pageNumber = 1;
}
// If the requested page is outside the document bounds
if(pageNumber > size || pageNumber < 1) {
throw "bad page number";
}
// Changes the cheeky global to our valid new page number
$('#pageNum').text(pageNumber)
pdf.getPage(pageNumber).then(function(page) {
var scale = 2.0;
var viewport = page.getViewport(scale);
var context = canvas.getContext('2d');
canvas.height = viewport.height;
canvas.width = viewport.width;
var renderContext = {
canvasContext: context,
viewport: viewport
};
page.render(renderContext);
});
}).catch(e => {});
}
}