-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdfHandler.js
271 lines (238 loc) · 9.61 KB
/
pdfHandler.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
import { redrawPaths, getUUID, setUUID, getPaths, startAnimation, stopAnimation } from './drawingHandler.js';
import { createImageElement } from './htmlHandler.js';
import { updateButtons, updateSlider } from './UIHandler.js';
const pdfjsLib = window['pdfjs-dist/build/pdf'];
pdfjsLib.GlobalWorkerOptions.workerSrc = 'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.9.359/pdf.worker.min.js';
const base_url = "https://pr.nkmr.io";
export let pdfDoc = null;
export let currentPageNum = 1;
export let paths = {};
export let pdfScale = 1;
let isRendering = false;
let uploaded_files = null;
const pdfSVG = document.getElementById('pdf-svg');
const zoomSlider = document.getElementById('zoomSlider');
zoomSlider.addEventListener('input', () => {
pdfScale = 1 / parseFloat(zoomSlider.value);
renderPage(currentPageNum);
});
export function handleFileSelect(event) {
uploaded_files = event.target.files[0];
if (uploaded_files && uploaded_files.type !== 'application/pdf') {
alert('Please upload a PDF file.');
return;
}
let fileReader = new FileReader();
fileReader.onload = function() {
let typedarray = new Uint8Array(this.result);
pdfjsLib.getDocument({
data: typedarray,
cMapUrl: 'https://cdn.jsdelivr.net/npm/pdfjs-dist@2.9.359/cmaps/',
cMapPacked: true,
//textLayerMode: 2,
}).promise.then(pdf => {
pdfDoc = pdf;
paths = {}; // pathsをクリア
renderPage(1);
// ボタンの有効/無効を設定
document.getElementById('file-select-button').style = 'display: none';
updateButtons();
}).catch(error => {
console.error("Error loading PDF: ", error);
});
};
fileReader.readAsArrayBuffer(uploaded_files);
const pdfContainer = document.getElementById('pdf-container');
pdfContainer.style.border = '1px solid #000';
}
export function loadPDF(url) {
const loadingTask = pdfjsLib.getDocument({
url: url,
cMapUrl: 'https://cdn.jsdelivr.net/npm/pdfjs-dist@2.9.359/cmaps/',
cMapPacked: true
});
loadingTask.promise.then(pdf => {
pdfDoc = pdf;
pdfScale = 1 / parseFloat(document.getElementById('zoomSlider').value);
renderPage(1);
// ボタンの有効/無効を設定
}).catch(error => {
console.error('Error loading PDF:', error);
});
}
export async function sharePDF() {
// すでにUUIDがある場合は共有されているので不要
let uuid = getUUID();
if(uuid){
alert('already shared');
}
if(!uploaded_files){
alert('Please upload a PDF file first.');
return;
}
/*
const pdfFile = document.getElementById('pdf-upload').files[0];
if (!pdfFile) {
alert('Please upload a PDF file first.');
return;
}
*/
// UUIDの生成とPDFのアップロード
const formData = new FormData();
//formData.append('file', pdfFile);
formData.append('file', uploaded_files);
try {
const response = await fetch('api.php', {
method: 'POST',
body: formData
});
const data = await response.text();
console.log('PDF upload response (raw):', data);
try {
const jsonData = JSON.parse(data);
console.log('PDF upload response (parsed):', jsonData);
if (jsonData.uuid) {
setUUID(jsonData.uuid);
// JSONのアップロード
const annotationData = JSON.stringify(getPaths());
const jsonResponse = await fetch('api.php', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
uuid: getUUID(),
data: annotationData,
}),
});
const jsonResult = await jsonResponse.json();
console.log('JSON upload response:', jsonResult);
if (jsonResult.status === 'success') {
const url = `${base_url}/${jsonData.uuid}`;
try {
await navigator.clipboard.writeText(url);
history.pushState("", "", url);
alert('Shareable URL copied to clipboard: ' + url);
} catch (error) {
console.error('Failed to copy URL to clipboard: ', error);
alert('Failed to copy URL to clipboard: ' + url);
}
} else {
console.error(jsonResult.message);
alert('Failed to save JSON: ' + jsonResult.message);
}
} else {
console.error(jsonData.message);
alert('Failed to upload PDF: ' + jsonData.message);
}
} catch (jsonError) {
console.error('Failed to parse JSON:', jsonError);
alert('Failed to upload PDF: Response is not valid JSON.');
}
} catch (error) {
console.error('Error:', error);
alert('Failed to share PDF: ' + error.message);
}
}
function validatePaths(){
//console.log("validate paths");
if(paths == null) return;
//console.log("path length = " + Object.keys(paths).length);
for (let pageNum in paths) {
//console.log(paths[pageNum]);
//console.log(paths[pageNum].length);
for(let pathNum=0; pathNum<paths[pageNum].length; pathNum++){
if(paths[pageNum][pathNum] != null && paths[pageNum][pathNum].type === 'path'){
for(let i=1; i<paths[pageNum][pathNum].points.length; i++){
if(paths[pageNum][pathNum].points[i].x == null || paths[pageNum][pathNum].points[i].y == null){
paths[pageNum][pathNum].points[i].x = paths[pageNum][pathNum].points[i-1].x;
paths[pageNum][pathNum].points[i].y = paths[pageNum][pathNum].points[i-1].y;
}
}
}
}
}
}
export function loadAnnotations(annotations) {
paths = JSON.parse(annotations);
//console.log(paths);
validatePaths();
if (pdfDoc) {
renderPage(currentPageNum);
}
}
export function renderPage(num) {
currentPageNum = num;
console.log("render " + num);
if (isRendering) return;
isRendering = true;
pdfDoc.getPage(num).then(page => {
let viewport = page.getViewport({ scale: pdfScale });
let headerHeight = 50; // Adjust for margin
pdfScale = Math.min((window.innerWidth - 20) / viewport.width, (window.innerHeight - headerHeight - 20) / viewport.height);
// viewport = page.getViewport({ scale: scale });
// while (pdfSVG.firstChild) {
// pdfSVG.removeChild(pdfSVG.firstChild);
// }
// pdfSVG.setAttribute('width', viewport.width);
// pdfSVG.setAttribute('height', viewport.height);
// Increase the scale for higher resolution rendering
let rateHighRes = 2;
if(viewport.width < 500) rateHighRes = 4;
let highResScale = pdfScale * rateHighRes; // Example: render at double resolution
viewport = page.getViewport({ scale: highResScale });
while (pdfSVG.firstChild) {
pdfSVG.removeChild(pdfSVG.firstChild);
}
pdfSVG.setAttribute('width', viewport.width / rateHighRes); // Adjust to match the display scale
pdfSVG.setAttribute('height', viewport.height / rateHighRes);
// Create a canvas to render the PDF page
let canvas = document.createElement('canvas');
canvas.width = viewport.width;
canvas.height = viewport.height;
let context = canvas.getContext('2d');
let renderContext = {
canvasContext: context,
viewport: viewport
};
page.render(renderContext).promise.then(() => {
// Create an image element and set its href to the canvas data URL
pdfSVG.appendChild(createImageElement(viewport.width / rateHighRes, viewport.height / rateHighRes, canvas.toDataURL()));
isRendering = false;
if(document.getElementById('autoplay').checked){
stopAnimation();
playSlider.value = 0;
if(paths[currentPageNum]){
playSlider.max = paths[currentPageNum].length;
startAnimation();
}
} else {
stopAnimation();
if (paths[currentPageNum]) {
redrawPaths(paths[currentPageNum].length);
}
}
updateButtons(); // ボタンの有効/無効を設定
if(!document.getElementById('autoplay').checked){
updateSlider(paths[currentPageNum].length, paths[currentPageNum].length);
}
}).catch(error => {
console.error("Error rendering page: ", error);
isRendering = false;
});
}).catch(error => {
console.error("Error rendering page: ", error);
isRendering = false;
});
}
export function changePage(offset) {
if (isRendering) return;
// 端っこの場合は無視するよ
if (offset < 0 && currentPageNum == 1) return;
if (offset > 0 && currentPageNum == pdfDoc.numPages) return;
currentPageNum += offset;
if (currentPageNum < 1) currentPageNum = 1;
if (currentPageNum > pdfDoc.numPages) currentPageNum = pdfDoc.numPages;
pdfScale = 1 / parseFloat(document.getElementById('zoomSlider').value);
renderPage(currentPageNum);
}