Examups commited on
Commit
b7ea422
·
verified ·
1 Parent(s): 3591846

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +571 -19
index.html CHANGED
@@ -1,19 +1,571 @@
1
- <!doctype html>
2
- <html>
3
- <head>
4
- <meta charset="utf-8" />
5
- <meta name="viewport" content="width=device-width" />
6
- <title>My static Space</title>
7
- <link rel="stylesheet" href="style.css" />
8
- </head>
9
- <body>
10
- <div class="card">
11
- <h1>Welcome to your static Space!</h1>
12
- <p>You can modify this app directly by editing <i>index.html</i> in the Files and versions tab.</p>
13
- <p>
14
- Also don't forget to check the
15
- <a href="https://huggingface.co/docs/hub/spaces" target="_blank">Spaces documentation</a>.
16
- </p>
17
- </div>
18
- </body>
19
- </html>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ <!DOCTYPE html>
3
+ <html lang="en">
4
+ <head>
5
+ <meta charset="UTF-8">
6
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
7
+ <title>Advanced PDF Editor</title>
8
+ <script src="https://cdn.tailwindcss.com"></script>
9
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.11.338/pdf.min.js"></script>
10
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.11.338/pdf.worker.min.js"></script>
11
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.4.0/jspdf.umd.min.js"></script>
12
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.4.1/html2canvas.min.js"></script> <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
13
+ <style>
14
+ body {
15
+ font-family: 'Inter', sans-serif;
16
+ }
17
+ /* Custom scrollbar for a cleaner look */
18
+ ::-webkit-scrollbar { width: 8px; height: 8px; }
19
+ ::-webkit-scrollbar-track { background: #1e293b; }
20
+ ::-webkit-scrollbar-thumb { background: #475569; border-radius: 10px; }
21
+ ::-webkit-scrollbar-thumb:hover { background: #64748b; }
22
+ /* Custom styles for file inputs */
23
+ input[type="file"]::file-selector-button {
24
+ @apply bg-indigo-600 hover:bg-indigo-700 text-white font-semibold py-2 px-4 rounded-lg cursor-pointer transition-colors duration-200;
25
+ }
26
+ /* Custom select arrow */
27
+ select {
28
+ -webkit-appearance: none; -moz-appearance: none; appearance: none;
29
+ background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 24 24' stroke-width='2' stroke='%2394a3b8'%3E%3Cpath stroke-linecap='round' stroke-linejoin='round' d='M19.5 8.25l-7.5 7.5-7.5-7.5' /%3E%3C/svg%3E");
30
+ background-repeat: no-repeat; background-position: right 0.5rem center; background-size: 1.25em; padding-right: 2rem;
31
+ }
32
+ /* NEW: Styles for editable text layer */
33
+ .text-layer {
34
+ position: absolute;
35
+ top: 0; left: 0; right: 0; bottom: 0;
36
+ opacity: 0;
37
+ pointer-events: none;
38
+ transition: opacity 0.2s;
39
+ }
40
+ .text-layer.active {
41
+ opacity: 1;
42
+ pointer-events: auto;
43
+ }
44
+ .text-layer > div {
45
+ position: absolute;
46
+ transform-origin: 0 0;
47
+ color: transparent;
48
+ caret-color: black;
49
+ outline: 1px dashed rgba(67, 56, 202, 0);
50
+ transition: outline 0.2s;
51
+ white-space: pre;
52
+ overflow: hidden;
53
+ -webkit-user-select: text;
54
+ -moz-user-select: text;
55
+ -ms-user-select: text;
56
+ user-select: text;
57
+ }
58
+ .text-layer.active > div:hover {
59
+ outline: 1px dashed #c7d2fe;
60
+ }
61
+ .text-layer.active > div:focus {
62
+ outline: 2px solid #4f46e5;
63
+ background-color: rgba(199, 210, 254, 0.3);
64
+ }
65
+ </style>
66
+ </head>
67
+ <body class="bg-slate-900 text-slate-300">
68
+
69
+ <div id="loading-overlay" class="fixed inset-0 bg-black bg-opacity-70 z-50 flex-col items-center justify-center hidden">
70
+ <div class="animate-spin rounded-full h-16 w-16 border-t-4 border-b-4 border-indigo-500"></div>
71
+ <p id="loading-text" class="text-white text-xl mt-4">Loading...</p>
72
+ </div>
73
+
74
+ <div class="flex flex-col md:flex-row h-screen">
75
+ <aside id="sidebar" class="w-full md:w-80 lg:w-96 bg-slate-800 p-6 shadow-2xl overflow-y-auto transition-all duration-300">
76
+ <h1 class="text-2xl font-bold text-white mb-6">Advanced PDF Editor</h1>
77
+
78
+ <div class="mb-6">
79
+ <label for="pdf-upload" class="block text-sm font-medium text-slate-400 mb-2">1. Upload PDF</label>
80
+ <input type="file" id="pdf-upload" accept=".pdf" class="block w-full text-sm text-slate-400 file:mr-4 file:border-0">
81
+ </div>
82
+
83
+ <div id="controls-panel" class="space-y-6 hidden">
84
+ <div class="p-4 bg-slate-700/50 rounded-lg">
85
+ <h3 class="font-semibold text-white mb-3">Document Settings</h3>
86
+ <div>
87
+ <label for="document-name" class="block text-sm font-medium text-slate-400 mb-1">Document Name</label>
88
+ <input type="text" id="document-name" placeholder="edited-document" class="w-full bg-slate-600 text-white rounded-md border-slate-500 focus:ring-indigo-500 focus:border-indigo-500">
89
+ </div>
90
+ </div>
91
+
92
+ <div class="p-4 bg-slate-700/50 rounded-lg">
93
+ <h3 class="font-semibold text-white mb-3">Text Editing</h3>
94
+ <div class="space-y-4">
95
+ <p class="text-sm text-slate-400">Enable to edit text content directly on the pages.</p>
96
+ <button id="toggle-text-edit-btn" class="w-full bg-indigo-500 hover:bg-indigo-600 text-white font-bold py-2 px-4 rounded-lg transition-colors duration-200">Enable Text Editing</button>
97
+ </div>
98
+ </div>
99
+
100
+ <div class="p-4 bg-slate-700/50 rounded-lg">
101
+ <h3 class="font-semibold text-white mb-3">Image Watermark</h3>
102
+ <div class="space-y-4">
103
+ <div>
104
+ <label for="watermark-upload" class="block text-sm font-medium text-slate-400 mb-2">Upload Image</label>
105
+ <input type="file" id="watermark-upload" accept="image/png, image/jpeg" class="block w-full text-sm text-slate-400 file:mr-4 file:border-0">
106
+ </div>
107
+ <div>
108
+ <label for="watermark-opacity" class="block text-sm font-medium text-slate-400">Opacity: <span id="watermark-opacity-value">0.3</span></label>
109
+ <input type="range" id="watermark-opacity" min="0" max="1" step="0.05" value="0.3" class="w-full h-2 bg-slate-600 rounded-lg appearance-none cursor-pointer">
110
+ </div>
111
+ </div>
112
+ </div>
113
+
114
+ <div class="p-4 bg-slate-700/50 rounded-lg">
115
+ <h3 class="font-semibold text-white mb-3">Page Border</h3>
116
+ <div class="grid grid-cols-2 gap-4">
117
+ <div>
118
+ <label for="border-color" class="block text-sm font-medium text-slate-400 mb-1">Color</label>
119
+ <input type="color" id="border-color" value="#000000" class="w-full h-10 p-1 bg-slate-600 rounded-md cursor-pointer">
120
+ </div>
121
+ <div>
122
+ <label for="border-width" class="block text-sm font-medium text-slate-400 mb-1">Width (px)</label>
123
+ <input type="number" id="border-width" min="0" value="0" class="w-full bg-slate-600 text-white rounded-md border-slate-500 focus:ring-indigo-500 focus:border-indigo-500">
124
+ </div>
125
+ <div class="col-span-2">
126
+ <label for="border-position" class="block text-sm font-medium text-slate-400 mb-1">Position</label>
127
+ <select id="border-position" class="w-full bg-slate-600 text-white rounded-md border-slate-500 focus:ring-indigo-500 focus:border-indigo-500">
128
+ <option value="center">Center</option>
129
+ <option value="inside">Inside</option>
130
+ <option value="outside">Outside</option>
131
+ </select>
132
+ </div>
133
+ </div>
134
+ </div>
135
+
136
+ <div class="p-4 bg-slate-700/50 rounded-lg">
137
+ <h3 class="font-semibold text-white mb-3">Header & Footer</h3>
138
+ <div class="space-y-4">
139
+ <div>
140
+ <label for="header-text" class="block text-sm font-medium text-slate-400 mb-1">Header Text</label>
141
+ <input type="text" id="header-text" placeholder="e.g., Confidential" class="w-full bg-slate-600 text-white rounded-md border-slate-500 focus:ring-indigo-500 focus:border-indigo-500">
142
+ </div>
143
+ <div>
144
+ <label for="footer-text" class="block text-sm font-medium text-slate-400 mb-1">Footer Text</label>
145
+ <input type="text" id="footer-text" placeholder="e.g., Company Name" class="w-full bg-slate-600 text-white rounded-md border-slate-500 focus:ring-indigo-500 focus:border-indigo-500">
146
+ </div>
147
+ <div>
148
+ <label for="font-family" class="block text-sm font-medium text-slate-400 mb-1">Font</label>
149
+ <select id="font-family" class="w-full bg-slate-600 text-white rounded-md border-slate-500 focus:ring-indigo-500 focus:border-indigo-500">
150
+ <option>Arial</option>
151
+ <option>Helvetica</option>
152
+ <option>Times New Roman</option>
153
+ <option>Courier New</option>
154
+ <option>Verdana</option>
155
+ <option>Georgia</option>
156
+ </select>
157
+ </div>
158
+ <div class="flex items-center">
159
+ <input id="page-numbers" type="checkbox" checked class="h-4 w-4 rounded border-gray-300 text-indigo-600 focus:ring-indigo-600">
160
+ <label for="page-numbers" class="ml-3 block text-sm font-medium text-slate-400">Add Page Numbers (updates automatically)</label>
161
+ </div>
162
+ </div>
163
+ </div>
164
+
165
+ <div class="pt-4 space-y-3">
166
+ <button id="download-btn" class="w-full bg-indigo-600 hover:bg-indigo-700 text-white font-bold py-3 px-4 rounded-lg transition-colors duration-200 flex items-center justify-center">
167
+ <svg class="w-5 h-5 mr-2" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" d="M3 16.5v2.25A2.25 2.25 0 005.25 21h13.5A2.25 2.25 0 0021 18.75V16.5M16.5 12L12 16.5m0 0L7.5 12m4.5 4.5V3" /></svg>
168
+ Download Edited PDF
169
+ </button>
170
+ <button id="reset-btn" class="w-full bg-rose-600 hover:bg-rose-700 text-white font-bold py-3 px-4 rounded-lg transition-colors duration-200 flex items-center justify-center">
171
+ <svg class="w-5 h-5 mr-2" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" d="M16.023 9.348h4.992v-.001M2.985 19.644v-4.992m0 0h4.992m-4.993 0l3.181 3.183a8.25 8.25 0 0011.664 0l3.181-3.183m-4.991-2.695v4.992h-4.992m0 0l-3.181-3.183a8.25 8.25 0 0111.664 0l3.181 3.183" /></svg>
172
+ Reset All Edits
173
+ </button>
174
+ </div>
175
+ </div>
176
+ </aside>
177
+
178
+ <main id="pdf-viewer-container" class="flex-1 p-4 lg:p-8 overflow-y-auto">
179
+ <div id="pdf-viewer" class="flex flex-col items-center space-y-6">
180
+ <div id="placeholder" class="w-full h-full flex flex-col items-center justify-center text-slate-500">
181
+ <svg class="w-24 h-24 mb-4" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" d="M19.5 14.25v-2.625a3.375 3.375 0 00-3.375-3.375h-1.5A1.125 1.125 0 0113.5 7.125v-1.5a3.375 3.375 0 00-3.375-3.375H8.25m2.25 0H5.625c-.621 0-1.125.504-1.125 1.125v17.25c0 .621.504 1.125 1.125 1.125h12.75c.621 0 1.125-.504 1.125-1.125V11.25a9 9 0 00-9-9z" /></svg>
182
+ <h2 class="text-2xl font-semibold">Your PDF will appear here</h2>
183
+ <p class="mt-2">Upload a file using the panel on the left to get started.</p>
184
+ </div>
185
+ </div>
186
+ </main>
187
+ </div>
188
+
189
+ <script>
190
+ pdfjsLib.GlobalWorkerOptions.workerSrc = `https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.11.338/pdf.worker.min.js`;
191
+
192
+ // DOM Elements
193
+ const pdfUpload = document.getElementById('pdf-upload');
194
+ const pdfViewer = document.getElementById('pdf-viewer');
195
+ const placeholder = document.getElementById('placeholder');
196
+ const controlsPanel = document.getElementById('controls-panel');
197
+ const loadingOverlay = document.getElementById('loading-overlay');
198
+ const loadingText = document.getElementById('loading-text');
199
+
200
+ // Control Elements
201
+ const documentNameInput = document.getElementById('document-name');
202
+ const watermarkUpload = document.getElementById('watermark-upload');
203
+ const watermarkOpacitySlider = document.getElementById('watermark-opacity');
204
+ const watermarkOpacityValue = document.getElementById('watermark-opacity-value');
205
+ const borderColorPicker = document.getElementById('border-color');
206
+ const borderWidthInput = document.getElementById('border-width');
207
+ const borderPositionSelect = document.getElementById('border-position');
208
+ const headerTextInput = document.getElementById('header-text');
209
+ const footerTextInput = document.getElementById('footer-text');
210
+ const fontFamilySelect = document.getElementById('font-family');
211
+ const pageNumbersCheckbox = document.getElementById('page-numbers');
212
+ const downloadBtn = document.getElementById('download-btn');
213
+ const resetBtn = document.getElementById('reset-btn');
214
+ const toggleTextEditBtn = document.getElementById('toggle-text-edit-btn'); // NEW
215
+
216
+ // App State
217
+ let pdfDoc = null;
218
+ let originalCanvases = [];
219
+ let watermarkImage = null;
220
+ let deletedPageIndexes = new Set();
221
+ let textEditModeEnabled = false; // NEW
222
+
223
+ // --- PDF Loading and Rendering ---
224
+
225
+ pdfUpload.addEventListener('change', handlePdfUpload);
226
+
227
+ async function handlePdfUpload(event) {
228
+ const file = event.target.files[0];
229
+ if (!file || file.type !== 'application/pdf') {
230
+ alert('Please select a valid PDF file.');
231
+ return;
232
+ }
233
+ resetAll(false);
234
+ const fileReader = new FileReader();
235
+ fileReader.onload = async (e) => {
236
+ showLoading('Loading PDF...');
237
+ try {
238
+ const typedarray = new Uint8Array(e.target.result);
239
+ pdfDoc = await pdfjsLib.getDocument({ data: typedarray }).promise;
240
+ await renderAllPages();
241
+ controlsPanel.classList.remove('hidden');
242
+ placeholder.classList.add('hidden');
243
+ } catch (error) {
244
+ console.error('Error loading PDF:', error);
245
+ alert('Could not load the PDF. It might be corrupted or protected.');
246
+ } finally {
247
+ hideLoading();
248
+ }
249
+ };
250
+ fileReader.readAsArrayBuffer(file);
251
+ }
252
+
253
+ async function renderAllPages() {
254
+ pdfViewer.innerHTML = '';
255
+ originalCanvases = [];
256
+ deletedPageIndexes.clear();
257
+ const numPages = pdfDoc.numPages;
258
+ const scale = 1.5;
259
+
260
+ for (let i = 0; i < numPages; i++) {
261
+ showLoading(`Rendering page ${i + 1} of ${numPages}...`);
262
+ const page = await pdfDoc.getPage(i + 1);
263
+ const viewport = page.getViewport({ scale });
264
+
265
+ const pageContainer = document.createElement('div');
266
+ pageContainer.className = 'page-container relative bg-white rounded-lg shadow-lg overflow-hidden';
267
+ pageContainer.dataset.pageIndex = i;
268
+
269
+ const canvas = document.createElement('canvas');
270
+ const context = canvas.getContext('2d');
271
+ canvas.height = viewport.height;
272
+ canvas.width = viewport.width;
273
+
274
+ const deleteButton = document.createElement('button');
275
+ deleteButton.innerHTML = '&times;';
276
+ deleteButton.className = 'absolute top-2 right-2 w-8 h-8 bg-red-600 text-white text-xl font-bold rounded-full flex items-center justify-center hover:bg-red-700 transition-all z-20';
277
+ deleteButton.title = 'Delete Page';
278
+ deleteButton.onclick = () => deletePage(i);
279
+
280
+ const renderContext = { canvasContext: context, viewport: viewport };
281
+ await page.render(renderContext).promise;
282
+
283
+ originalCanvases.push(canvas.toDataURL());
284
+
285
+ // NEW: Create and render the text layer
286
+ const textLayerDiv = document.createElement('div');
287
+ textLayerDiv.className = 'text-layer';
288
+ await renderTextLayer(page, scale, textLayerDiv);
289
+
290
+ pageContainer.appendChild(deleteButton);
291
+ pageContainer.appendChild(canvas);
292
+ pageContainer.appendChild(textLayerDiv); // Add text layer on top
293
+ pdfViewer.appendChild(pageContainer);
294
+ }
295
+ }
296
+
297
+ // NEW: Function to render the editable text layer
298
+ async function renderTextLayer(page, scale, container) {
299
+ const textContent = await page.getTextContent();
300
+ const viewport = page.getViewport({ scale });
301
+ textContent.items.forEach(item => {
302
+ const textDiv = document.createElement('div');
303
+ textDiv.textContent = item.str;
304
+ textDiv.setAttribute('contenteditable', 'true');
305
+
306
+ const style = textDiv.style;
307
+ style.height = `${item.height}px`;
308
+ style.fontFamily = item.fontName;
309
+
310
+ // pdf.js gives a transform matrix [scaleX, skewY, skewX, scaleY, x, y]
311
+ // We need to convert it to a CSS matrix for proper positioning and rotation
312
+ const cssTransform = `matrix(${item.transform[0]}, ${item.transform[1]}, ${item.transform[2]}, ${item.transform[3]}, ${item.transform[4]}, ${viewport.height - item.transform[5]})`;
313
+ style.transform = cssTransform;
314
+
315
+ // Adjust font size based on the scaling in the transform matrix
316
+ const fontHeight = Math.sqrt((item.transform[2] * item.transform[2]) + (item.transform[3] * item.transform[3]));
317
+ style.fontSize = `${fontHeight}px`;
318
+
319
+ container.appendChild(textDiv);
320
+ });
321
+ }
322
+
323
+ // --- Event Listeners for Controls ---
324
+ const controls = [watermarkOpacitySlider, borderColorPicker, borderWidthInput, borderPositionSelect, headerTextInput, footerTextInput, pageNumbersCheckbox, fontFamilySelect];
325
+ controls.forEach(control => {
326
+ control.addEventListener('input', () => {
327
+ if (control.id === 'watermark-opacity') watermarkOpacityValue.textContent = control.value;
328
+ applyAllEdits();
329
+ });
330
+ });
331
+
332
+ watermarkUpload.addEventListener('change', (e) => {
333
+ const file = e.target.files[0];
334
+ if (!file) return;
335
+ const reader = new FileReader();
336
+ reader.onload = (e) => {
337
+ watermarkImage = new Image();
338
+ watermarkImage.onload = applyAllEdits;
339
+ watermarkImage.src = e.target.result;
340
+ };
341
+ reader.readAsDataURL(file);
342
+ });
343
+
344
+ resetBtn.addEventListener('click', () => resetAll(true));
345
+
346
+ // NEW: Listener for text edit toggle
347
+ toggleTextEditBtn.addEventListener('click', () => {
348
+ textEditModeEnabled = !textEditModeEnabled;
349
+ document.querySelectorAll('.text-layer').forEach(layer => {
350
+ layer.classList.toggle('active', textEditModeEnabled);
351
+ });
352
+ if (textEditModeEnabled) {
353
+ toggleTextEditBtn.textContent = 'Disable Text Editing';
354
+ toggleTextEditBtn.classList.replace('bg-indigo-500', 'bg-amber-600');
355
+ toggleTextEditBtn.classList.replace('hover:bg-indigo-600', 'hover:bg-amber-700');
356
+ } else {
357
+ toggleTextEditBtn.textContent = 'Enable Text Editing';
358
+ toggleTextEditBtn.classList.replace('bg-amber-600', 'bg-indigo-500');
359
+ toggleTextEditBtn.classList.replace('hover:bg-amber-700', 'hover:bg-indigo-600');
360
+ }
361
+ });
362
+
363
+ // --- Core Editing Logic ---
364
+
365
+ function deletePage(index) {
366
+ if (deletedPageIndexes.has(index)) return;
367
+ const visiblePages = pdfDoc.numPages - deletedPageIndexes.size;
368
+ if (visiblePages <= 1) {
369
+ alert("You cannot delete the last page.");
370
+ return;
371
+ }
372
+ deletedPageIndexes.add(index);
373
+ const pageContainer = document.querySelector(`.page-container[data-page-index='${index}']`);
374
+ if (pageContainer) pageContainer.style.display = 'none';
375
+ applyAllEdits();
376
+ }
377
+
378
+ function applyAllEdits() {
379
+ if (!pdfDoc) return;
380
+ const visiblePageContainers = Array.from(document.querySelectorAll('.page-container')).filter(c => c.style.display !== 'none');
381
+ const newTotalPages = visiblePageContainers.length;
382
+
383
+ visiblePageContainers.forEach((container, newPageIndex) => {
384
+ const originalIndex = parseInt(container.dataset.pageIndex);
385
+ const canvas = container.querySelector('canvas');
386
+ const context = canvas.getContext('2d');
387
+ const originalImage = new Image();
388
+ originalImage.onload = () => {
389
+ context.clearRect(0, 0, canvas.width, canvas.height);
390
+ context.drawImage(originalImage, 0, 0);
391
+ drawWatermark(context, canvas.width, canvas.height);
392
+ drawBorder(context, canvas.width, canvas.height);
393
+ drawHeaderText(context, canvas.width);
394
+ drawFooterText(context, canvas.width, canvas.height, newPageIndex + 1, newTotalPages);
395
+ };
396
+ originalImage.src = originalCanvases[originalIndex];
397
+ });
398
+ }
399
+
400
+ const getTextFont = () => `${'16px'} ${fontFamilySelect.value}`;
401
+
402
+ function drawHeaderText(ctx, width) {
403
+ const text = headerTextInput.value;
404
+ if (!text) return;
405
+ ctx.fillStyle = '#000000';
406
+ ctx.font = getTextFont();
407
+ ctx.textAlign = 'center';
408
+ ctx.fillText(text, width / 2, 30);
409
+ }
410
+
411
+ function drawFooterText(ctx, width, height, pageNum, totalPages) {
412
+ const userText = footerTextInput.value;
413
+ let fullText = userText;
414
+ if (pageNumbersCheckbox.checked) {
415
+ const pageNumText = `Page ${pageNum} of ${totalPages}`;
416
+ fullText = userText ? `${userText} | ${pageNumText}` : pageNumText;
417
+ }
418
+ if (!fullText) return;
419
+ ctx.fillStyle = '#000000';
420
+ ctx.font = getTextFont();
421
+ ctx.textAlign = 'center';
422
+ ctx.fillText(fullText, width / 2, height - 20);
423
+ }
424
+
425
+ function drawWatermark(ctx, width, height) {
426
+ if (!watermarkImage) return;
427
+ ctx.globalAlpha = parseFloat(watermarkOpacitySlider.value);
428
+ const canvasAspect = width / height;
429
+ const imgAspect = watermarkImage.width / watermarkImage.height;
430
+ let drawWidth, drawHeight;
431
+ if (canvasAspect > imgAspect) {
432
+ drawHeight = height * 0.6;
433
+ drawWidth = drawHeight * imgAspect;
434
+ } else {
435
+ drawWidth = width * 0.6;
436
+ drawHeight = drawWidth / imgAspect;
437
+ }
438
+ ctx.drawImage(watermarkImage, (width - drawWidth) / 2, (height - drawHeight) / 2, drawWidth, drawHeight);
439
+ ctx.globalAlpha = 1.0;
440
+ }
441
+
442
+ function drawBorder(ctx, width, height) {
443
+ const borderWidth = parseInt(borderWidthInput.value);
444
+ if (borderWidth <= 0) return;
445
+ ctx.strokeStyle = borderColorPicker.value;
446
+ ctx.lineWidth = borderWidth;
447
+ const position = borderPositionSelect.value;
448
+ ctx.beginPath();
449
+ if (position === 'inside') ctx.rect(borderWidth, borderWidth, width - borderWidth * 2, height - borderWidth * 2);
450
+ else if (position === 'outside') ctx.rect(0, 0, width, height);
451
+ else ctx.rect(borderWidth / 2, borderWidth / 2, width - borderWidth, height - borderWidth);
452
+ ctx.stroke();
453
+ }
454
+
455
+ // --- PDF Generation & Utility ---
456
+
457
+ downloadBtn.addEventListener('click', generatePdf);
458
+
459
+ // NEW: Function to burn text edits onto the canvas before download
460
+ async function burnTextEdits() {
461
+ showLoading('Applying text edits...');
462
+ const visibleContainers = Array.from(document.querySelectorAll('.page-container')).filter(c => c.style.display !== 'none');
463
+ for (const container of visibleContainers) {
464
+ const textLayer = container.querySelector('.text-layer');
465
+ const canvas = container.querySelector('canvas');
466
+ const ctx = canvas.getContext('2d');
467
+
468
+ // Use html2canvas to render the HTML text layer to a temporary canvas
469
+ const textCanvas = await html2canvas(textLayer, {
470
+ backgroundColor: null, // Transparent background
471
+ scale: 1, // Already scaled
472
+ width: canvas.width,
473
+ height: canvas.height,
474
+ logging: false
475
+ });
476
+
477
+ // Draw the rendered text on top of the main canvas
478
+ ctx.drawImage(textCanvas, 0, 0);
479
+ }
480
+ }
481
+
482
+ async function generatePdf() {
483
+ if (!pdfDoc) return;
484
+ showLoading('Generating PDF...');
485
+
486
+ // Turn off text edit mode visually to prevent outlines in the final PDF
487
+ const wasEditing = textEditModeEnabled;
488
+ if (wasEditing) toggleTextEditBtn.click();
489
+
490
+ applyAllEdits();
491
+
492
+ try {
493
+ // NEW: Wait for text edits to be burned onto the canvas
494
+ await burnTextEdits();
495
+
496
+ const { jsPDF } = window.jspdf;
497
+ const visibleCanvases = Array.from(document.querySelectorAll('.page-container')).filter(c => c.style.display !== 'none').map(c => c.querySelector('canvas'));
498
+ if (visibleCanvases.length === 0) {
499
+ alert("No pages to generate.");
500
+ hideLoading();
501
+ return;
502
+ }
503
+
504
+ const firstCanvas = visibleCanvases[0];
505
+ const orientation = firstCanvas.width > firstCanvas.height ? 'l' : 'p';
506
+ const pdf = new jsPDF(orientation, 'px', [firstCanvas.width, firstCanvas.height]);
507
+
508
+ visibleCanvases.forEach((canvas, index) => {
509
+ const imgData = canvas.toDataURL('image/jpeg', 0.9);
510
+ if (index > 0) pdf.addPage([canvas.width, canvas.height], orientation);
511
+ pdf.addImage(imgData, 'JPEG', 0, 0, canvas.width, canvas.height);
512
+ });
513
+
514
+ const docName = documentNameInput.value.trim() || 'edited-document';
515
+ pdf.save(`${docName}.pdf`);
516
+ } catch(error) {
517
+ console.error("Error generating PDF:", error);
518
+ alert("An error occurred while generating the PDF.");
519
+ } finally {
520
+ // Restore text edit mode if it was on
521
+ if (wasEditing) toggleTextEditBtn.click();
522
+ hideLoading();
523
+ }
524
+ }
525
+
526
+ function resetAll(redraw) {
527
+ // Reset text edit mode if enabled
528
+ if (textEditModeEnabled) {
529
+ toggleTextEditBtn.click();
530
+ }
531
+ // Remove text layers before resetting other things
532
+ document.querySelectorAll('.text-layer').forEach(layer => layer.remove());
533
+
534
+ document.querySelectorAll('.page-container').forEach(c => c.style.display = 'block');
535
+ deletedPageIndexes.clear();
536
+
537
+ documentNameInput.value = '';
538
+ watermarkUpload.value = '';
539
+ watermarkImage = null;
540
+ watermarkOpacitySlider.value = 0.3;
541
+ watermarkOpacityValue.textContent = '0.3';
542
+ borderColorPicker.value = '#000000';
543
+ borderWidthInput.value = 0;
544
+ borderPositionSelect.value = 'center';
545
+ headerTextInput.value = '';
546
+ footerTextInput.value = '';
547
+ fontFamilySelect.value = 'Arial';
548
+ pageNumbersCheckbox.checked = true;
549
+
550
+ if (redraw && pdfDoc) {
551
+ // If redrawing, re-render text layers as well.
552
+ renderAllPages();
553
+ } else if (redraw) {
554
+ applyAllEdits();
555
+ }
556
+ }
557
+
558
+ function showLoading(message) {
559
+ loadingText.textContent = message;
560
+ loadingOverlay.classList.remove('hidden');
561
+ loadingOverlay.classList.add('flex');
562
+ }
563
+
564
+ function hideLoading() {
565
+ loadingOverlay.classList.add('hidden');
566
+ loadingOverlay.classList.remove('flex');
567
+ }
568
+
569
+ </script>
570
+ </body>
571
+ </html>