-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
240 lines (193 loc) · 8.43 KB
/
index.html
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
<!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/3.5.141/pdf.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.5.141/pdf_viewer.min.css" />
<style type="text/css">
#upload-button {
width: 150px;
display: block;
margin: 20px auto;
}
#file-to-upload {
display: none;
}
#pdf-main-container {
width: 400px;
margin: 20px auto;
}
#pdf-loader {
display: none;
text-align: center;
color: #999999;
font-size: 13px;
line-height: 100px;
height: 100px;
}
#pdf-contents {
display: none;
}
#pdf-meta {
overflow: hidden;
margin: 0 0 20px 0;
}
#pdf-buttons {
float: left;
}
#page-count-container {
float: right;
}
#pdf-current-page {
display: inline;
}
#pdf-total-pages {
display: inline;
}
#pdf-canvas {
border: 1px solid rgba(0, 0, 0, 0.2);
box-sizing: border-box;
}
#page-loader {
height: 100px;
line-height: 100px;
text-align: center;
display: none;
color: #999999;
font-size: 13px;
}
</style>
</head>
<body>
<button id="upload-button">Select PDF</button>
<input type="file" id="file-to-upload" accept="application/pdf" />
<div id="pdf-main-container">
<div id="pdf-loader">Loading document ...</div>
<div id="pdf-contents">
<div id="pdf-meta">
<div id="pdf-buttons">
<button id="pdf-prev">Previous</button>
<button id="pdf-next">Next</button>
</div>
<div id="page-count-container">Page <div id="pdf-current-page"></div> of <div id="pdf-total-pages">
</div>
</div>
</div>
<canvas id="pdf-canvas" width="400"></canvas>
<div id="text-layer" class="textLayer"></div>
<div id="page-loader">Loading page ...</div>
</div>
</div>
<script>
var __PDF_DOC,
__CURRENT_PAGE,
__TOTAL_PAGES,
__PAGE_RENDERING_IN_PROGRESS = 0,
__CANVAS = $('#pdf-canvas').get(0),
__CANVAS_CTX = __CANVAS.getContext('2d'),
PDFJS = window['pdfjs-dist/build/pdf'];
PDFJS.GlobalWorkerOptions.workerSrc = 'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.5.141/pdf.worker.min.js';
function showPDF(pdf_url) {
$("#pdf-loader").show();
PDFJS.getDocument({ url: pdf_url }).promise.then(function (pdf_doc) {
__PDF_DOC = pdf_doc;
__TOTAL_PAGES = __PDF_DOC.numPages;
// Hide the pdf loader and show pdf container in HTML
$("#pdf-loader").hide();
$("#pdf-contents").show();
$("#pdf-total-pages").text(__TOTAL_PAGES);
// Show the first page
showPage(1);
}).catch(function (error) {
// If error re-show the upload button
$("#pdf-loader").hide();
$("#upload-button").show();
alert(error.message);
});
}
function showPage(page_no) {
__PAGE_RENDERING_IN_PROGRESS = 1;
__CURRENT_PAGE = page_no;
// Disable Prev & Next buttons while page is being loaded
$("#pdf-next, #pdf-prev").attr('disabled', 'disabled');
// While page is being rendered hide the canvas and show a loading message
$("#pdf-canvas").hide();
$("#page-loader").show();
// Update current page in HTML
$("#pdf-current-page").text(page_no);
// Fetch the page
__PDF_DOC.getPage(page_no).then(function (page) {
// Get viewport of the page at required scale
let viewport = page.getViewport({
scale: 1,
});
// As the canvas is of a fixed width we need to set the scale of the viewport accordingly
let scale = __CANVAS.width / viewport.width;
viewport = page.getViewport({
scale: scale
});
// Set canvas height
__CANVAS.height = viewport.height;
var renderContext = {
canvasContext: __CANVAS_CTX,
viewport: viewport
};
// Render the page contents in the canvas
page.render(renderContext).promise.then(function () {
__PAGE_RENDERING_IN_PROGRESS = 0;
// Re-enable Prev & Next buttons
$("#pdf-next, #pdf-prev").removeAttr('disabled');
// Show the canvas and hide the page loader
$("#pdf-canvas").show();
$("#page-loader").hide();
// Return the text contents of the page after the pdf has been rendered in the canvas
return page.getTextContent();
}).then(function (textContent) {
// Get canvas offset
var canvas_offset = $("#pdf-canvas").offset();
// Clear HTML for text layer
$("#text-layer").html('');
// Assign the CSS created to the text-layer element
document.getElementById('text-layer').style.setProperty('--scale-factor', viewport.scale);
$("#text-layer").css({ left: canvas_offset.left + 'px', top: canvas_offset.top + 'px'});
// Pass the data to the method for rendering of text over the pdf canvas.
PDFJS.renderTextLayer({
textContentSource: textContent,
container: $("#text-layer").get(0),
viewport: viewport,
textDivs: []
});
});
});
}
// Upon click this should should trigger click on the #file-to-upload file input element
// This is better than showing the not-good-looking file input element
$("#upload-button").on('click', function () {
$("#file-to-upload").trigger('click');
});
// When user chooses a PDF file
$("#file-to-upload").on('change', function () {
// Validate whether PDF
if (['application/pdf'].indexOf($("#file-to-upload").get(0).files[0].type) == -1) {
alert('Error : Not a PDF');
return;
}
$("#upload-button").hide();
// Send the object url of the pdf
showPDF(URL.createObjectURL($("#file-to-upload").get(0).files[0]));
});
// Previous page of the PDF
$("#pdf-prev").on('click', function () {
if (__CURRENT_PAGE != 1)
showPage(--__CURRENT_PAGE);
});
// Next page of the PDF
$("#pdf-next").on('click', function () {
if (__CURRENT_PAGE != __TOTAL_PAGES)
showPage(++__CURRENT_PAGE);
});
</script>
</body>
</html>