-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_lambda.html
More file actions
266 lines (232 loc) · 8.64 KB
/
Copy pathtest_lambda.html
File metadata and controls
266 lines (232 loc) · 8.64 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
<!DOCTYPE html>
<html>
<head>
<title>PDF to Image Converter - Test</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
max-width: 1200px;
margin: 0 auto;
padding: 20px;
background-color: #f5f5f5;
}
.container {
background-color: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.input-group {
margin-bottom: 20px;
}
input[type="text"], input[type="file"] {
width: 100%;
padding: 10px;
margin: 5px 0;
border: 1px solid #ddd;
border-radius: 4px;
box-sizing: border-box;
}
.status {
margin: 10px 0;
padding: 10px;
border-radius: 4px;
}
.status.success {
background-color: #e8f5e9;
color: #2e7d32;
}
.status.error {
background-color: #ffebee;
color: #c62828;
}
.image-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
gap: 20px;
margin-top: 20px;
}
.image-container {
position: relative;
background-color: white;
padding: 10px;
border-radius: 4px;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}
.image-container img {
width: 100%;
height: auto;
border-radius: 2px;
}
.page-number {
position: absolute;
top: 10px;
right: 10px;
background-color: rgba(0, 0, 0, 0.7);
color: white;
padding: 4px 8px;
border-radius: 4px;
font-size: 12px;
}
button {
background-color: #2196f3;
color: white;
border: none;
padding: 10px 20px;
border-radius: 4px;
cursor: pointer;
font-size: 14px;
}
button:disabled {
background-color: #bdbdbd;
cursor: not-allowed;
}
button:hover:not(:disabled) {
background-color: #1976d2;
}
.loading {
display: none;
margin: 10px 0;
color: #666;
}
.loading.visible {
display: block;
}
</style>
</head>
<body>
<div class="container">
<h1>PDF to Image Converter - Test</h1>
<div class="input-group">
<label for="lambdaUrl">Lambda Function URL:</label>
<input type="text" id="lambdaUrl" placeholder="https://..." />
</div>
<div class="input-group">
<label for="pdfInput">Select PDF file:</label>
<input type="file" id="pdfInput" accept=".pdf" disabled />
</div>
<div id="loading" class="loading">Processing...</div>
<div id="status"></div>
<div id="imageGrid" class="image-grid"></div>
</div>
<script>
const lambdaUrlInput = document.getElementById('lambdaUrl');
const pdfInput = document.getElementById('pdfInput');
const loading = document.getElementById('loading');
const status = document.getElementById('status');
const imageGrid = document.getElementById('imageGrid');
// Load saved Lambda URL from localStorage
const savedLambdaUrl = localStorage.getItem('lambdaFunctionUrl');
if (savedLambdaUrl) {
lambdaUrlInput.value = savedLambdaUrl;
pdfInput.disabled = false;
}
function showStatus(message, type = 'success') {
status.className = `status ${type}`;
status.textContent = message;
}
function showLoading(show) {
loading.className = show ? 'loading visible' : 'loading';
pdfInput.disabled = show;
}
function normalizeUrl(url) {
return url.trim().replace(/\/+$/, '');
}
lambdaUrlInput.addEventListener('input', () => {
const url = normalizeUrl(lambdaUrlInput.value);
lambdaUrlInput.value = url; // Update input with normalized URL
pdfInput.disabled = !url;
if (url) {
localStorage.setItem('lambdaFunctionUrl', url);
} else {
localStorage.removeItem('lambdaFunctionUrl');
}
});
async function convertPdf(file) {
try {
showLoading(true);
imageGrid.innerHTML = '';
// Step 1: Get upload URL
showStatus('Getting upload URL...');
const baseUrl = normalizeUrl(lambdaUrlInput.value);
const urlResponse = await fetch(`${baseUrl}/upload_url`, {
method: 'GET',
mode: 'cors',
headers: {
'Accept': 'application/json'
}
});
if (!urlResponse.ok) {
const errorText = await urlResponse.text();
throw new Error(`Failed to get upload URL: ${errorText}`);
}
const { uploadUrl, fileId } = await urlResponse.json();
// Step 2: Upload PDF
showStatus('Uploading PDF file...');
const uploadResponse = await fetch(uploadUrl, {
method: 'PUT',
body: file,
mode: 'cors',
headers: {
'Content-Type': 'application/pdf'
},
credentials: 'omit'
});
if (!uploadResponse.ok) {
let errorMessage;
try {
const text = await uploadResponse.text();
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(text, "text/xml");
const message = xmlDoc.querySelector("Message");
errorMessage = message ? message.textContent : text;
} catch (e) {
errorMessage = await uploadResponse.text();
}
throw new Error(`Failed to upload PDF: ${errorMessage}`);
}
// Step 3: Convert PDF to images
showStatus('Converting PDF to images...');
const convertResponse = await fetch(`${baseUrl}/process/${fileId}`, {
method: 'GET',
mode: 'cors',
headers: {
'Accept': 'application/json'
}
});
if (!convertResponse.ok) {
const errorText = await convertResponse.text();
throw new Error(`Failed to convert PDF: ${errorText}`);
}
const { imageUrls } = await convertResponse.json();
// Display images in grid
showStatus(`Successfully converted ${imageUrls.length} pages`);
imageUrls.forEach((url, index) => {
const container = document.createElement('div');
container.className = 'image-container';
const img = document.createElement('img');
img.src = url;
img.alt = `Page ${index + 1}`;
const pageNumber = document.createElement('div');
pageNumber.className = 'page-number';
pageNumber.textContent = `Page ${index + 1}`;
container.appendChild(img);
container.appendChild(pageNumber);
imageGrid.appendChild(container);
});
} catch (error) {
console.error('Error:', error);
showStatus(error.message, 'error');
} finally {
showLoading(false);
}
}
pdfInput.addEventListener('change', (e) => {
const file = e.target.files[0];
if (file) {
convertPdf(file);
}
});
</script>
</body>
</html>