Update index.html
Browse files- index.html +278 -272
index.html
CHANGED
|
@@ -94,304 +94,310 @@
|
|
| 94 |
</main>
|
| 95 |
</div>
|
| 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 |
-
file
|
| 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 |
-
} else if (hasPDFs) {
|
| 175 |
-
modeText = "الوضع الحالي: دمج ملفات PDF في ملف واحد (بدون تعديل محتوى الصفحات).";
|
| 176 |
-
} else if (hasImages) {
|
| 177 |
-
modeText = "الوضع الحالي: تحويل صور إلى ملف PDF واحد بحجم A4 لكل صفحة.";
|
| 178 |
-
} else {
|
| 179 |
-
modeText = "لا توج�� ملفات مدعومة في القائمة.";
|
| 180 |
-
}
|
| 181 |
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
<div class="mode-label">${modeText}</div>
|
| 188 |
-
<ul class="file-list-ul">
|
| 189 |
-
${files
|
| 190 |
-
.map(
|
| 191 |
-
(f, i) => `
|
| 192 |
-
<li>
|
| 193 |
-
<span class="index">${i + 1}</span>
|
| 194 |
-
<span class="name" title="${f.name}">${f.name}</span>
|
| 195 |
-
<span class="size">${(f.size / 1024).toFixed(1)} كيلوبايت</span>
|
| 196 |
-
<div class="row-actions">
|
| 197 |
-
<button class="move-btn" data-index="${i}" data-dir="up" title="نقل لأعلى">↑</button>
|
| 198 |
-
<button class="move-btn" data-index="${i}" data-dir="down" title="نقل لأسفل">↓</button>
|
| 199 |
-
<button class="delete-btn" data-index="${i}" title="حذف الملف">×</button>
|
| 200 |
-
</div>
|
| 201 |
-
</li>`
|
| 202 |
-
)
|
| 203 |
-
.join("")}
|
| 204 |
-
</ul>
|
| 205 |
-
`;
|
| 206 |
-
|
| 207 |
-
if (files.length > MAX_RECOMMENDED_FILES) {
|
| 208 |
-
setStatus(
|
| 209 |
-
"تنبيه: عدد الملفات كبير، قد تستغرق عملية الدمج وقتًا أطول على بعض الأجهزة.",
|
| 210 |
-
"warning"
|
| 211 |
-
);
|
| 212 |
-
}
|
| 213 |
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
}
|
| 226 |
-
});
|
| 227 |
-
});
|
| 228 |
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
const
|
| 233 |
-
const
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
if (dir === "up" && index > 0) {
|
| 237 |
-
[selectedFiles[index - 1], selectedFiles[index]] =
|
| 238 |
-
[selectedFiles[index], selectedFiles[index - 1]];
|
| 239 |
-
} else if (dir === "down" && index < selectedFiles.length - 1) {
|
| 240 |
-
[selectedFiles[index + 1], selectedFiles[index]] =
|
| 241 |
-
[selectedFiles[index], selectedFiles[index + 1]];
|
| 242 |
-
}
|
| 243 |
-
renderFileList(selectedFiles);
|
| 244 |
-
});
|
| 245 |
-
});
|
| 246 |
-
}
|
| 247 |
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
const a = document.createElement("a");
|
| 252 |
-
a.href = url;
|
| 253 |
-
a.download = filename;
|
| 254 |
-
document.body.appendChild(a);
|
| 255 |
-
a.click();
|
| 256 |
-
a.remove();
|
| 257 |
-
URL.revokeObjectURL(url);
|
| 258 |
-
}
|
| 259 |
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
if (!newFilesRaw.length) return;
|
| 264 |
|
| 265 |
-
const
|
| 266 |
-
const map = new Map();
|
| 267 |
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
filesInput.value = "";
|
| 277 |
-
});
|
| 278 |
|
| 279 |
-
// مسح الكل
|
| 280 |
-
clearBtn.addEventListener("click", () => {
|
| 281 |
selectedFiles = [];
|
| 282 |
renderFileList([]);
|
| 283 |
-
setStatus("تم مسح جميع الملفات المختارة.", "ok");
|
| 284 |
filesInput.value = "";
|
| 285 |
-
|
|
|
|
|
|
|
| 286 |
showProgress(false);
|
| 287 |
-
}
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
return;
|
| 296 |
-
}
|
| 297 |
-
|
| 298 |
-
const unsupported = files.filter((f) => !isImage(f) && !isPDF(f));
|
| 299 |
-
if (unsupported.length) {
|
| 300 |
-
setStatus("يوجد ملفات غير مدعومة. يرجى حذفها من القائمة.", "error");
|
| 301 |
-
return;
|
| 302 |
-
}
|
| 303 |
-
|
| 304 |
-
const { hasImages, hasPDFs } = getFilesInfo(files);
|
| 305 |
-
if (!hasImages && !hasPDFs) {
|
| 306 |
-
setStatus("لا توجد ملفات مدعومة للدمج.", "error");
|
| 307 |
-
return;
|
| 308 |
-
}
|
| 309 |
-
|
| 310 |
-
renderFileList(files);
|
| 311 |
-
|
| 312 |
-
try {
|
| 313 |
-
setStatus("جاري معالجة الملفات...", "loading");
|
| 314 |
-
showProgress(true);
|
| 315 |
-
mergeBtn.disabled = true;
|
| 316 |
-
mergeBtn.classList.add("disabled");
|
| 317 |
-
filesInput.disabled = true;
|
| 318 |
-
clearBtn.disabled = true;
|
| 319 |
-
|
| 320 |
-
const pdfDoc = await PDFLib.PDFDocument.create();
|
| 321 |
-
const totalSteps = files.length;
|
| 322 |
-
let currentStep = 0;
|
| 323 |
-
|
| 324 |
-
const pageWidth = 595.28; // A4
|
| 325 |
-
const pageHeight = 841.89; // A4
|
| 326 |
-
|
| 327 |
-
for (const file of files) {
|
| 328 |
-
const bytes = await file.arrayBuffer();
|
| 329 |
-
|
| 330 |
-
if (isPDF(file)) {
|
| 331 |
-
const donorPdf = await PDFLib.PDFDocument.load(bytes);
|
| 332 |
-
const pages = await pdfDoc.copyPages(
|
| 333 |
-
donorPdf,
|
| 334 |
-
donorPdf.getPageIndices()
|
| 335 |
-
);
|
| 336 |
-
pages.forEach((p) => pdfDoc.addPage(p));
|
| 337 |
-
} else if (isImage(file)) {
|
| 338 |
-
const lower = file.name.toLowerCase();
|
| 339 |
-
let image;
|
| 340 |
-
if (
|
| 341 |
-
file.type === "image/jpeg" ||
|
| 342 |
-
file.type === "image/jpg" ||
|
| 343 |
-
lower.endsWith(".jpg") ||
|
| 344 |
-
lower.endsWith(".jpeg")
|
| 345 |
-
) {
|
| 346 |
-
image = await pdfDoc.embedJpg(bytes);
|
| 347 |
-
} else {
|
| 348 |
-
image = await pdfDoc.embedPng(bytes);
|
| 349 |
-
}
|
| 350 |
-
|
| 351 |
-
const imgWidth = image.width;
|
| 352 |
-
const imgHeight = image.height;
|
| 353 |
-
const scale = Math.min(pageWidth / imgWidth, pageHeight / imgHeight);
|
| 354 |
-
const drawWidth = imgWidth * scale;
|
| 355 |
-
const drawHeight = imgHeight * scale;
|
| 356 |
-
const x = (pageWidth - drawWidth) / 2;
|
| 357 |
-
const y = (pageHeight - drawHeight) / 2;
|
| 358 |
-
|
| 359 |
-
const page = pdfDoc.addPage([pageWidth, pageHeight]);
|
| 360 |
-
page.drawImage(image, { x, y, width: drawWidth, height: drawHeight });
|
| 361 |
-
}
|
| 362 |
-
|
| 363 |
-
currentStep += 1;
|
| 364 |
-
setProgress(currentStep, totalSteps);
|
| 365 |
-
}
|
| 366 |
|
| 367 |
-
const pdfBytes = await pdfDoc.save();
|
| 368 |
-
|
| 369 |
-
let defaultName = "merged.pdf";
|
| 370 |
-
if (hasImages && !hasPDFs) defaultName = "merged-images.pdf";
|
| 371 |
-
else if (!hasImages && hasPDFs) defaultName = "merged-pdfs.pdf";
|
| 372 |
-
else if (hasImages && hasPDFs) defaultName = "merged-mixed.pdf";
|
| 373 |
-
|
| 374 |
-
const outName =
|
| 375 |
-
(outputNameInput.value || defaultName).trim() || defaultName;
|
| 376 |
-
|
| 377 |
-
downloadPdf(pdfBytes, outName);
|
| 378 |
-
setStatus("تم إنشاء ملف PDF النهائي بنجاح.", "ok");
|
| 379 |
-
showProgress(false);
|
| 380 |
-
|
| 381 |
-
selectedFiles = [];
|
| 382 |
-
renderFileList([]);
|
| 383 |
-
filesInput.value = "";
|
| 384 |
-
} catch (err) {
|
| 385 |
-
console.error(err);
|
| 386 |
-
setStatus("حدث خطأ أثناء المعالجة. تأكد من الملفات وحاول مرة أخرى.", "error");
|
| 387 |
-
showProgress(false);
|
| 388 |
-
} finally {
|
| 389 |
-
mergeBtn.disabled = false;
|
| 390 |
-
mergeBtn.classList.remove("disabled");
|
| 391 |
-
filesInput.disabled = false;
|
| 392 |
-
clearBtn.disabled = false;
|
| 393 |
-
}
|
| 394 |
-
});
|
| 395 |
-
</script>
|
| 396 |
</body>
|
| 397 |
</html>
|
|
|
|
| 94 |
</main>
|
| 95 |
</div>
|
| 96 |
|
| 97 |
+
<script>
|
| 98 |
+
const filesInput = document.getElementById("files");
|
| 99 |
+
const mergeBtn = document.getElementById("mergeBtn");
|
| 100 |
+
const clearBtn = document.getElementById("clearBtn");
|
| 101 |
+
const statusDiv = document.getElementById("status");
|
| 102 |
+
const fileListDiv = document.getElementById("fileList");
|
| 103 |
+
const outputNameInput = document.getElementById("outputName");
|
| 104 |
+
const progressDiv = document.getElementById("progress");
|
| 105 |
+
const progressText = document.getElementById("progressText");
|
| 106 |
+
const progressFill = document.getElementById("progressFill");
|
| 107 |
+
|
| 108 |
+
let selectedFiles = [];
|
| 109 |
+
const MAX_RECOMMENDED_FILES = 200;
|
| 110 |
+
|
| 111 |
+
function getTodayDateString() {
|
| 112 |
+
const d = new Date();
|
| 113 |
+
const year = d.getFullYear();
|
| 114 |
+
const month = String(d.getMonth() + 1).padStart(2, "0");
|
| 115 |
+
const day = String(d.getDate()).padStart(2, "0");
|
| 116 |
+
return `${year}-${month}-${day}`;
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
function setStatus(msg, type = "") {
|
| 120 |
+
statusDiv.textContent = msg;
|
| 121 |
+
statusDiv.className = "status" + (type ? " " + type : "");
|
| 122 |
+
if (!msg) statusDiv.className = "status";
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
function showProgress(show) {
|
| 126 |
+
if (show) {
|
| 127 |
+
progressDiv.classList.remove("hidden");
|
| 128 |
+
progressFill.style.width = "0%";
|
| 129 |
+
progressText.textContent = "جاري المعالجة...";
|
| 130 |
+
} else {
|
| 131 |
+
progressDiv.classList.add("hidden");
|
| 132 |
}
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
function setProgress(current, total, label = "معالجة الملفات") {
|
| 136 |
+
if (!total || total < 1) total = 1;
|
| 137 |
+
const percent = Math.floor((current / total) * 100);
|
| 138 |
+
progressFill.style.width = percent + "%";
|
| 139 |
+
progressText.textContent = `${label} (${current} من ${total}) - ${percent}%`;
|
| 140 |
+
}
|
| 141 |
+
|
| 142 |
+
function isImage(file) {
|
| 143 |
+
const name = file.name.toLowerCase();
|
| 144 |
+
return (
|
| 145 |
+
file.type.startsWith("image/") ||
|
| 146 |
+
name.endsWith(".jpg") ||
|
| 147 |
+
name.endsWith(".jpeg") ||
|
| 148 |
+
name.endsWith(".png")
|
| 149 |
+
);
|
| 150 |
+
}
|
| 151 |
+
|
| 152 |
+
function isPDF(file) {
|
| 153 |
+
const name = file.name.toLowerCase();
|
| 154 |
+
return (
|
| 155 |
+
file.type === "application/pdf" ||
|
| 156 |
+
name.endsWith(".pdf")
|
| 157 |
+
);
|
| 158 |
+
}
|
| 159 |
+
|
| 160 |
+
function getFilesInfo(files) {
|
| 161 |
+
let hasImages = false;
|
| 162 |
+
let hasPDFs = false;
|
| 163 |
+
files.forEach((f) => {
|
| 164 |
+
if (isImage(f)) hasImages = true;
|
| 165 |
+
else if (isPDF(f)) hasPDFs = true;
|
| 166 |
+
});
|
| 167 |
+
return { hasImages, hasPDFs };
|
| 168 |
+
}
|
| 169 |
+
|
| 170 |
+
function renderFileList(files) {
|
| 171 |
+
if (!files.length) {
|
| 172 |
+
fileListDiv.classList.add("hidden");
|
| 173 |
+
fileListDiv.innerHTML = "";
|
| 174 |
+
return;
|
| 175 |
}
|
| 176 |
|
| 177 |
+
fileListDiv.classList.remove("hidden");
|
| 178 |
+
const { hasImages, hasPDFs } = getFilesInfo(files);
|
| 179 |
+
let modeText = "";
|
| 180 |
+
if (hasImages && hasPDFs) {
|
| 181 |
+
modeText = "الوضع الحالي: دمج صور + ملفات PDF في ملف واحد، مع صفحات A4 ثابتة.";
|
| 182 |
+
} else if (hasPDFs) {
|
| 183 |
+
modeText = "الوضع الحالي: دمج ملفات PDF في ملف واحد (بدون تعديل محتوى الصفحات).";
|
| 184 |
+
} else if (hasImages) {
|
| 185 |
+
modeText = "الوضع الحالي: تحويل صور إلى ملف PDF واحد بحجم A4 لكل صفحة.";
|
| 186 |
+
} else {
|
| 187 |
+
modeText = "لا توجد ملفات مدعومة في القائمة.";
|
| 188 |
}
|
| 189 |
|
| 190 |
+
fileListDiv.innerHTML = `
|
| 191 |
+
<div class="file-list-header">
|
| 192 |
+
<span>الملفات المختارة: ${files.length}</span>
|
| 193 |
+
<span class="file-note">رتّب الملفات، أو احذف أي ملف قبل الدمج.</span>
|
| 194 |
+
</div>
|
| 195 |
+
<div class="mode-label">${modeText}</div>
|
| 196 |
+
<ul class="file-list-ul">
|
| 197 |
+
${files
|
| 198 |
+
.map(
|
| 199 |
+
(f, i) => `
|
| 200 |
+
<li>
|
| 201 |
+
<span class="index">${i + 1}</span>
|
| 202 |
+
<span class="name" title="${f.name}">${f.name}</span>
|
| 203 |
+
<span class="size">${(f.size / 1024).toFixed(1)} كيلوبايت</span>
|
| 204 |
+
<div class="row-actions">
|
| 205 |
+
<button class="move-btn" data-index="${i}" data-dir="up" title="نقل لأعلى">↑</button>
|
| 206 |
+
<button class="move-btn" data-index="${i}" data-dir="down" title="نقل لأسفل">↓</button>
|
| 207 |
+
<button class="delete-btn" data-index="${i}" title="حذف الملف">×</button>
|
| 208 |
+
</div>
|
| 209 |
+
</li>`
|
| 210 |
+
)
|
| 211 |
+
.join("")}
|
| 212 |
+
</ul>
|
| 213 |
+
`;
|
| 214 |
+
|
| 215 |
+
if (files.length > MAX_RECOMMENDED_FILES) {
|
| 216 |
+
setStatus(
|
| 217 |
+
"تنبيه: عدد الملفات كبير، قد تستغرق عملية الدمج وقتًا أطول على بعض الأجهزة.",
|
| 218 |
+
"warning"
|
| 219 |
);
|
| 220 |
}
|
| 221 |
|
| 222 |
+
// حذف
|
| 223 |
+
fileListDiv.querySelectorAll(".delete-btn").forEach((btn) => {
|
| 224 |
+
btn.addEventListener("click", (e) => {
|
| 225 |
+
const index = parseInt(e.currentTarget.dataset.index, 10);
|
| 226 |
+
if (!isNaN(index)) {
|
| 227 |
+
selectedFiles.splice(index, 1);
|
| 228 |
+
renderFileList(selectedFiles);
|
| 229 |
+
if (!selectedFiles.length) {
|
| 230 |
+
setStatus("");
|
| 231 |
+
showProgress(false);
|
| 232 |
+
}
|
| 233 |
+
}
|
| 234 |
+
});
|
| 235 |
+
});
|
| 236 |
|
| 237 |
+
// ترتيب
|
| 238 |
+
fileListDiv.querySelectorAll(".move-btn").forEach((btn) => {
|
| 239 |
+
btn.addEventListener("click", (e) => {
|
| 240 |
+
const index = parseInt(e.currentTarget.dataset.index, 10);
|
| 241 |
+
const dir = e.currentTarget.dataset.dir;
|
| 242 |
+
if (isNaN(index)) return;
|
| 243 |
+
|
| 244 |
+
if (dir === "up" && index > 0) {
|
| 245 |
+
[selectedFiles[index - 1], selectedFiles[index]] =
|
| 246 |
+
[selectedFiles[index], selectedFiles[index - 1]];
|
| 247 |
+
} else if (dir === "down" && index < selectedFiles.length - 1) {
|
| 248 |
+
[selectedFiles[index + 1], selectedFiles[index]] =
|
| 249 |
+
[selectedFiles[index], selectedFiles[index + 1]];
|
| 250 |
+
}
|
| 251 |
+
renderFileList(selectedFiles);
|
| 252 |
});
|
| 253 |
+
});
|
| 254 |
+
}
|
| 255 |
+
|
| 256 |
+
function downloadPdf(bytes, filename) {
|
| 257 |
+
const blob = new Blob([bytes], { type: "application/pdf" });
|
| 258 |
+
const url = URL.createObjectURL(blob);
|
| 259 |
+
const a = document.createElement("a");
|
| 260 |
+
a.href = url;
|
| 261 |
+
a.download = filename;
|
| 262 |
+
document.body.appendChild(a);
|
| 263 |
+
a.click();
|
| 264 |
+
a.remove();
|
| 265 |
+
URL.revokeObjectURL(url);
|
| 266 |
+
}
|
| 267 |
+
|
| 268 |
+
// إضافة ملفات على دفعات
|
| 269 |
+
filesInput.addEventListener("change", () => {
|
| 270 |
+
const newFilesRaw = Array.from(filesInput.files || []);
|
| 271 |
+
if (!newFilesRaw.length) return;
|
| 272 |
+
|
| 273 |
+
const newFiles = newFilesRaw.filter((f) => isImage(f) || isPDF(f));
|
| 274 |
+
const map = new Map();
|
| 275 |
+
|
| 276 |
+
[...selectedFiles, ...newFiles].forEach((f) => {
|
| 277 |
+
const key = `${f.name}|${f.size}|${f.lastModified}`;
|
| 278 |
+
if (!map.has(key)) map.set(key, f);
|
| 279 |
+
});
|
| 280 |
|
| 281 |
+
selectedFiles = Array.from(map.values());
|
| 282 |
+
renderFileList(selectedFiles);
|
| 283 |
+
setStatus("");
|
| 284 |
+
filesInput.value = "";
|
| 285 |
+
});
|
| 286 |
+
|
| 287 |
+
// مسح الكل
|
| 288 |
+
clearBtn.addEventListener("click", () => {
|
| 289 |
+
selectedFiles = [];
|
| 290 |
+
renderFileList([]);
|
| 291 |
+
setStatus("تم مسح جميع الملفات المختارة.", "ok");
|
| 292 |
+
filesInput.value = "";
|
| 293 |
+
outputNameInput.value = "";
|
| 294 |
+
showProgress(false);
|
| 295 |
+
});
|
| 296 |
+
|
| 297 |
+
// الدمج
|
| 298 |
+
mergeBtn.addEventListener("click", async () => {
|
| 299 |
+
const files = [...selectedFiles];
|
| 300 |
+
|
| 301 |
+
if (!files.length) {
|
| 302 |
+
setStatus("الرجاء اختيار الملفات أولاً.", "error");
|
| 303 |
+
return;
|
| 304 |
+
}
|
| 305 |
|
| 306 |
+
const unsupported = files.filter((f) => !isImage(f) && !isPDF(f));
|
| 307 |
+
if (unsupported.length) {
|
| 308 |
+
setStatus("يوجد ملفات غير مدعومة. يرجى حذفها من القائمة.", "error");
|
| 309 |
+
return;
|
| 310 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 311 |
|
| 312 |
+
const { hasImages, hasPDFs } = getFilesInfo(files);
|
| 313 |
+
if (!hasImages && !hasPDFs) {
|
| 314 |
+
setStatus("لا توجد ملفات مدعومة للدمج.", "error");
|
| 315 |
+
return;
|
| 316 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 317 |
|
| 318 |
+
renderFileList(files);
|
| 319 |
+
|
| 320 |
+
try {
|
| 321 |
+
setStatus("جاري معالجة الملفات...", "loading");
|
| 322 |
+
showProgress(true);
|
| 323 |
+
mergeBtn.disabled = true;
|
| 324 |
+
mergeBtn.classList.add("disabled");
|
| 325 |
+
filesInput.disabled = true;
|
| 326 |
+
clearBtn.disabled = true;
|
| 327 |
+
|
| 328 |
+
const pdfDoc = await PDFLib.PDFDocument.create();
|
| 329 |
+
const totalSteps = files.length;
|
| 330 |
+
let currentStep = 0;
|
| 331 |
+
|
| 332 |
+
const pageWidth = 595.28; // A4
|
| 333 |
+
const pageHeight = 841.89; // A4
|
| 334 |
+
|
| 335 |
+
for (const file of files) {
|
| 336 |
+
const bytes = await file.arrayBuffer();
|
| 337 |
+
|
| 338 |
+
if (isPDF(file)) {
|
| 339 |
+
const donorPdf = await PDFLib.PDFDocument.load(bytes);
|
| 340 |
+
const pages = await pdfDoc.copyPages(
|
| 341 |
+
donorPdf,
|
| 342 |
+
donorPdf.getPageIndices()
|
| 343 |
+
);
|
| 344 |
+
pages.forEach((p) => pdfDoc.addPage(p));
|
| 345 |
+
} else if (isImage(file)) {
|
| 346 |
+
const lower = file.name.toLowerCase();
|
| 347 |
+
let image;
|
| 348 |
+
if (
|
| 349 |
+
file.type === "image/jpeg" ||
|
| 350 |
+
file.type === "image/jpg" ||
|
| 351 |
+
lower.endsWith(".jpg") ||
|
| 352 |
+
lower.endsWith(".jpeg")
|
| 353 |
+
) {
|
| 354 |
+
image = await pdfDoc.embedJpg(bytes);
|
| 355 |
+
} else {
|
| 356 |
+
image = await pdfDoc.embedPng(bytes);
|
| 357 |
}
|
|
|
|
|
|
|
| 358 |
|
| 359 |
+
const imgWidth = image.width;
|
| 360 |
+
const imgHeight = image.height;
|
| 361 |
+
const scale = Math.min(pageWidth / imgWidth, pageHeight / imgHeight);
|
| 362 |
+
const drawWidth = imgWidth * scale;
|
| 363 |
+
const drawHeight = imgHeight * scale;
|
| 364 |
+
const x = (pageWidth - drawWidth) / 2;
|
| 365 |
+
const y = (pageHeight - drawHeight) / 2;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 366 |
|
| 367 |
+
const page = pdfDoc.addPage([pageWidth, pageHeight]);
|
| 368 |
+
page.drawImage(image, { x, y, width: drawWidth, height: drawHeight });
|
| 369 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 370 |
|
| 371 |
+
currentStep += 1;
|
| 372 |
+
setProgress(currentStep, totalSteps);
|
| 373 |
+
}
|
|
|
|
| 374 |
|
| 375 |
+
const pdfBytes = await pdfDoc.save();
|
|
|
|
| 376 |
|
| 377 |
+
// 👇 هنا الاسم الافتراضي = تاريخ اليوم
|
| 378 |
+
const defaultName = getTodayDateString() + ".pdf";
|
| 379 |
+
const outName =
|
| 380 |
+
(outputNameInput.value || defaultName).trim() || defaultName;
|
| 381 |
|
| 382 |
+
downloadPdf(pdfBytes, outName);
|
| 383 |
+
setStatus("تم إنشاء ملف PDF النهائي بنجاح.", "ok");
|
| 384 |
+
showProgress(false);
|
|
|
|
|
|
|
| 385 |
|
|
|
|
|
|
|
| 386 |
selectedFiles = [];
|
| 387 |
renderFileList([]);
|
|
|
|
| 388 |
filesInput.value = "";
|
| 389 |
+
} catch (err) {
|
| 390 |
+
console.error(err);
|
| 391 |
+
setStatus("حدث خطأ أثناء المعالجة. تأكد من الملفات وحاول مرة أخرى.", "error");
|
| 392 |
showProgress(false);
|
| 393 |
+
} finally {
|
| 394 |
+
mergeBtn.disabled = false;
|
| 395 |
+
mergeBtn.classList.remove("disabled");
|
| 396 |
+
filesInput.disabled = false;
|
| 397 |
+
clearBtn.disabled = false;
|
| 398 |
+
}
|
| 399 |
+
});
|
| 400 |
+
</script>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 401 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 402 |
</body>
|
| 403 |
</html>
|