stat2025 commited on
Commit
a506e58
·
verified ·
1 Parent(s): ca4188d

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +180 -18
index.html CHANGED
@@ -1,19 +1,181 @@
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
+ <!DOCTYPE html>
2
+ <html lang="ar" dir="rtl">
3
+ <head>
4
+ <meta charset="UTF-8" />
5
+ <title>دمج الملفات إلى PDF - stat2025</title>
6
+ <link rel="stylesheet" href="style.css" />
7
+ <!-- مكتبة PDF-LIB للمعالجة داخل المتصفح -->
8
+ <script src="https://unpkg.com/pdf-lib@1.17.1/dist/pdf-lib.min.js"></script>
9
+ </head>
10
+ <body>
11
+ <div class="container">
12
+ <h1>أداة دمج وتحويل الملفات إلى PDF</h1>
13
+ <p class="subtitle">
14
+ يتم تنفيذ جميع العمليات على جهازك مباشرة، ولا يتم تخزين أي ملف في الخادم.
15
+ </p>
16
+
17
+ <!-- اختيار نوع الملفات قبل الرفع -->
18
+ <div class="card">
19
+ <h2>① اختر نوع الملفات</h2>
20
+ <label class="option">
21
+ <input type="radio" name="fileType" value="images" checked />
22
+ تحويل مجموعة صور (JPG/PNG) إلى ملف PDF واحد
23
+ </label>
24
+ <label class="option">
25
+ <input type="radio" name="fileType" value="pdfs" />
26
+ دمج ملفات PDF متعددة في ملف PDF واحد
27
+ </label>
28
+ </div>
29
+
30
+ <!-- رفع الملفات -->
31
+ <div class="card">
32
+ <h2>② اختر الملفات</h2>
33
+ <input id="files" type="file" multiple />
34
+ <p class="hint">
35
+ للصور: اختر ملفات JPG أو PNG.
36
+ لملفات PDF: اختر ملفات PDF المراد دمجها.
37
+ </p>
38
+ </div>
39
+
40
+ <!-- زر التنفيذ -->
41
+ <button id="mergeBtn" class="btn-main">دمج وإنشاء ملف PDF</button>
42
+
43
+ <!-- منطقة الرسائل -->
44
+ <div id="status" class="status"></div>
45
+ </div>
46
+
47
+ <script>
48
+ const filesInput = document.getElementById("files");
49
+ const mergeBtn = document.getElementById("mergeBtn");
50
+ const statusDiv = document.getElementById("status");
51
+
52
+ function setStatus(msg, isError = false) {
53
+ statusDiv.textContent = msg;
54
+ statusDiv.className = "status " + (isError ? "error" : "ok");
55
+ }
56
+
57
+ function getSelectedType() {
58
+ const checked = document.querySelector('input[name="fileType"]:checked');
59
+ return checked ? checked.value : null;
60
+ }
61
+
62
+ function downloadPdf(bytes, filename) {
63
+ const blob = new Blob([bytes], { type: "application/pdf" });
64
+ const url = URL.createObjectURL(blob);
65
+ const a = document.createElement("a");
66
+ a.href = url;
67
+ a.download = filename;
68
+ document.body.appendChild(a);
69
+ a.click();
70
+ a.remove();
71
+ URL.revokeObjectURL(url);
72
+ }
73
+
74
+ mergeBtn.addEventListener("click", async () => {
75
+ const selectedType = getSelectedType();
76
+ const files = Array.from(filesInput.files || []);
77
+
78
+ if (!selectedType) {
79
+ setStatus("الرجاء اختيار نوع الملفات أولاً.", true);
80
+ return;
81
+ }
82
+
83
+ if (!files.length) {
84
+ setStatus("الرجاء اختيار الملفات المراد معالجتها.", true);
85
+ return;
86
+ }
87
+
88
+ // ترتيب حسب الاسم للحفاظ على التسلسل
89
+ files.sort((a, b) =>
90
+ a.name.localeCompare(b.name, undefined, { numeric: true })
91
+ );
92
+
93
+ try {
94
+ setStatus("جاري المعالجة، يرجى الانتظار...");
95
+
96
+ if (selectedType === "images") {
97
+ // التحقق
98
+ if (!files.every(f => f.type.startsWith("image/"))) {
99
+ setStatus("جميع الملفات يجب أن تكون صور (JPG/PNG) عند اختيار هذا الخيار.", true);
100
+ return;
101
+ }
102
+
103
+ const pdfDoc = await PDFLib.PDFDocument.create();
104
+
105
+ for (const file of files) {
106
+ const bytes = await file.arrayBuffer();
107
+ let image;
108
+
109
+ if (
110
+ file.type === "image/jpeg" ||
111
+ file.type === "image/jpg" ||
112
+ file.name.toLowerCase().endsWith(".jpg") ||
113
+ file.name.toLowerCase().endsWith(".jpeg")
114
+ ) {
115
+ image = await pdfDoc.embedJpg(bytes);
116
+ } else {
117
+ image = await pdfDoc.embedPng(bytes);
118
+ }
119
+
120
+ const imgWidth = image.width;
121
+ const imgHeight = image.height;
122
+
123
+ // حجم صفحة A4 تقريبي
124
+ const pageWidth = 595.28;
125
+ const pageHeight = 841.89;
126
+
127
+ const page = pdfDoc.addPage([pageWidth, pageHeight]);
128
+
129
+ const scale = Math.min(pageWidth / imgWidth, pageHeight / imgHeight);
130
+ const drawWidth = imgWidth * scale;
131
+ const drawHeight = imgHeight * scale;
132
+ const x = (pageWidth - drawWidth) / 2;
133
+ const y = (pageHeight - drawHeight) / 2;
134
+
135
+ page.drawImage(image, {
136
+ x,
137
+ y,
138
+ width: drawWidth,
139
+ height: drawHeight
140
+ });
141
+ }
142
+
143
+ const pdfBytes = await pdfDoc.save();
144
+ downloadPdf(pdfBytes, "merged-images.pdf");
145
+ setStatus("تم إنشاء وتحميل ملف PDF بنجاح. ✅");
146
+ }
147
+
148
+ if (selectedType === "pdfs") {
149
+ // التحقق
150
+ if (!files.every(f => f.type === "application/pdf" || f.name.toLowerCase().endsWith(".pdf"))) {
151
+ setStatus("جميع الملفات يجب أن تكون بصيغة PDF عند اختيار هذا الخيار.", true);
152
+ return;
153
+ }
154
+
155
+ const pdfDoc = await PDFLib.PDFDocument.create();
156
+
157
+ for (const file of files) {
158
+ const bytes = await file.arrayBuffer();
159
+ const donorPdf = await PDFLib.PDFDocument.load(bytes);
160
+ const pages = await pdfDoc.copyPages(
161
+ donorPdf,
162
+ donorPdf.getPageIndices()
163
+ );
164
+ pages.forEach(p => pdfDoc.addPage(p));
165
+ }
166
+
167
+ const pdfBytes = await pdfDoc.save();
168
+ downloadPdf(pdfBytes, "merged-pdfs.pdf");
169
+ setStatus("تم دمج ملفات PDF وتحميل الملف النهائي. ✅");
170
+ }
171
+
172
+ // تنظيف: لا نحتفظ بأي ملفات - كل شيء في الذاكرة فقط
173
+ filesInput.value = "";
174
+ } catch (err) {
175
+ console.error(err);
176
+ setStatus("حدث خطأ أثناء المعالجة. تأكد من الملفات وحاول مرة أخرى.", true);
177
+ }
178
+ });
179
+ </script>
180
+ </body>
181
  </html>