File size: 17,329 Bytes
2c87df0
 
7a9a7a7
 
 
 
 
e9b25a4
2c87df0
902caa8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6debacf
 
 
 
902caa8
 
 
 
6debacf
 
 
 
902caa8
 
6debacf
 
902caa8
 
6debacf
 
 
902caa8
 
 
6debacf
 
902caa8
6debacf
902caa8
 
 
 
 
 
6debacf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
902caa8
 
 
6debacf
902caa8
 
6debacf
 
 
 
 
 
 
 
 
 
902caa8
6debacf
 
 
902caa8
 
6debacf
 
 
 
 
 
 
 
 
 
 
 
 
 
902caa8
 
 
 
6debacf
902caa8
2c87df0
e9b25a4
902caa8
6debacf
 
902caa8
 
6debacf
2c87df0
6debacf
 
 
 
 
 
 
 
 
 
902caa8
 
 
 
 
6debacf
902caa8
 
 
6debacf
 
902caa8
 
6debacf
902caa8
f5021c2
 
 
6debacf
f5021c2
902caa8
 
6debacf
 
 
902caa8
 
 
6debacf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f5021c2
6debacf
f5021c2
 
 
6debacf
 
 
f5021c2
 
 
6debacf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
902caa8
6debacf
 
 
902caa8
 
 
 
6debacf
 
902caa8
 
 
 
 
 
 
6debacf
 
 
 
 
 
 
 
 
 
902caa8
 
 
 
 
 
6debacf
 
 
 
 
e9b25a4
902caa8
2c87df0
 
 
 
e9b25a4
6debacf
 
 
 
 
 
 
 
 
f5021c2
6debacf
902caa8
6debacf
 
 
 
902caa8
 
6debacf
 
 
 
 
f5021c2
902caa8
 
6debacf
 
 
 
 
 
 
 
 
f5021c2
6debacf
902caa8
6debacf
 
 
 
902caa8
 
 
f5021c2
 
 
 
 
 
 
b0c5771
 
 
 
 
 
 
 
 
 
f5021c2
6debacf
 
f5021c2
 
 
 
 
6debacf
 
 
 
f5021c2
e9b25a4
 
6debacf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f5021c2
 
e9b25a4
6debacf
 
e9b25a4
6debacf
e9b25a4
 
 
f5021c2
 
b0c5771
 
6debacf
 
 
 
 
f5021c2
 
b0c5771
f5021c2
 
 
b0c5771
f5021c2
 
 
6debacf
 
 
 
 
 
 
 
f5021c2
6debacf
f5021c2
 
 
6debacf
 
 
f5021c2
 
 
 
 
 
902caa8
 
 
 
 
 
 
 
 
 
 
 
 
 
f5021c2
 
 
 
 
2c87df0
 
 
 
 
 
 
 
 
 
 
 
 
b0c5771
 
 
 
 
 
 
 
 
 
 
 
 
 
2c87df0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b0c5771
 
902caa8
 
b0c5771
 
 
7a9a7a7
902caa8
b0c5771
 
902caa8
b0c5771
 
2c87df0
902caa8
 
b0c5771
 
 
 
 
902caa8
 
 
b0c5771
 
 
 
 
 
 
 
 
 
7a9a7a7
 
2c87df0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
902caa8
 
 
 
 
 
 
 
 
 
2c87df0
 
902caa8
 
 
7a9a7a7
 
902caa8
2c87df0
902caa8
2c87df0
902caa8
 
 
 
 
 
 
 
7a9a7a7
 
 
902caa8
 
 
7a9a7a7
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
import csv
import multiprocessing
import os
import tempfile

import ocrmypdf
import pandas as pd
from flask import Flask, after_this_request, render_template_string, request, send_file
from openpyxl import load_workbook
from werkzeug.utils import secure_filename


app = Flask(__name__)
app.config["MAX_CONTENT_LENGTH"] = 50 * 1024 * 1024


PAGE = """
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Document Converter Pro</title>
  <style>
    :root {
      color-scheme: light;
      font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
      color: #172033;
      background: #e8edf5;
    }
    * {
      box-sizing: border-box;
    }
    body {
      margin: 0;
      min-height: 100vh;
      padding: 28px 18px;
      background:
        radial-gradient(circle at top left, rgba(37, 99, 235, 0.16), transparent 30rem),
        linear-gradient(135deg, #f8fafc 0%, #e8edf5 52%, #e6f4ef 100%);
    }
    main {
      width: min(1120px, 100%);
      margin: 0 auto;
    }
    header {
      display: grid;
      gap: 18px;
      padding: 34px 0 26px;
    }
    h1 {
      margin: 0 0 8px;
      font-size: clamp(2rem, 5vw, 4rem);
      line-height: 1;
      letter-spacing: 0;
      color: #0f172a;
    }
    p {
      margin: 0;
      color: #526173;
      line-height: 1.5;
    }
    .hero-copy {
      max-width: 740px;
    }
    .hero-copy p {
      font-size: 1.03rem;
    }
    .toolbar {
      display: flex;
      flex-wrap: wrap;
      gap: 10px;
    }
    .pill {
      display: inline-flex;
      align-items: center;
      min-height: 32px;
      padding: 6px 10px;
      border-radius: 999px;
      border: 1px solid rgba(37, 99, 235, 0.18);
      background: rgba(255, 255, 255, 0.72);
      color: #1f3f75;
      font-size: 0.88rem;
      font-weight: 700;
    }
    .grid {
      display: grid;
      grid-template-columns: repeat(2, minmax(0, 1fr));
      gap: 18px;
    }
    section {
      display: grid;
      align-content: start;
      gap: 18px;
      min-height: 440px;
      padding: 26px;
      background: rgba(255, 255, 255, 0.86);
      border: 1px solid rgba(148, 163, 184, 0.32);
      border-radius: 8px;
      box-shadow: 0 20px 55px rgba(15, 23, 42, 0.12);
      backdrop-filter: blur(12px);
    }
    .panel-top {
      display: grid;
      gap: 10px;
    }
    h2 {
      margin: 0;
      font-size: 1.35rem;
      color: #111827;
    }
    .tag {
      width: fit-content;
      padding: 6px 9px;
      border-radius: 6px;
      background: #ecfdf5;
      color: #047857;
      font-size: 0.78rem;
      font-weight: 800;
      text-transform: uppercase;
      letter-spacing: 0.05em;
    }
    form {
      display: grid;
      gap: 16px;
      margin-top: 4px;
    }
    input[type="file"],
    select {
      width: 100%;
      min-height: 48px;
      padding: 12px 14px;
      border: 1px solid #cbd5e1;
      border-radius: 6px;
      background: #ffffff;
      color: #172033;
      font: inherit;
    }
    input[type="file"]::file-selector-button {
      margin-right: 12px;
      border: 0;
      border-radius: 5px;
      background: #e0f2fe;
      color: #075985;
      padding: 9px 12px;
      font-weight: 800;
    }
    button {
      min-height: 44px;
      border: 0;
      border-radius: 6px;
      background: linear-gradient(135deg, #2563eb, #059669);
      color: white;
      font-weight: 700;
      cursor: pointer;
      font: inherit;
      box-shadow: 0 10px 22px rgba(37, 99, 235, 0.22);
    }
    button:hover {
      filter: brightness(0.96);
    }
    button:disabled {
      cursor: wait;
      background: #94a3b8;
      box-shadow: none;
    }
    .message {
      padding: 14px 16px;
      margin-bottom: 18px;
      border: 1px solid #fecdd3;
      border-radius: 8px;
      color: #9f1239;
      background: #fff1f2;
    }
    .progress-wrap {
      display: none;
      gap: 8px;
    }
    .progress-wrap.active {
      display: grid;
    }
    .progress-label {
      display: flex;
      justify-content: space-between;
      gap: 12px;
      color: #334155;
      font-size: 0.9rem;
      font-weight: 750;
    }
    .progress-track {
      height: 10px;
      overflow: hidden;
      border-radius: 999px;
      background: #dbe5f1;
    }
    .progress-bar {
      width: 0%;
      height: 100%;
      border-radius: inherit;
      background: linear-gradient(90deg, #2563eb, #22c55e, #f59e0b);
      transition: width 320ms ease;
    }
    .status {
      min-height: 24px;
      color: #2563eb;
      font-weight: 650;
    }
    .status.success {
      color: #047857;
    }
    .status.error {
      color: #9f1239;
    }
    .download-link {
      display: none;
      align-items: center;
      justify-content: center;
      min-height: 44px;
      border-radius: 6px;
      background: #111827;
      color: #ffffff;
      text-decoration: none;
      font-weight: 800;
    }
    .download-link.ready {
      display: flex;
    }
    .details {
      display: grid;
      gap: 8px;
      margin-top: auto;
      padding-top: 8px;
      color: #64748b;
      font-size: 0.9rem;
    }
    @media (max-width: 760px) {
      body {
        padding: 18px 12px;
      }
      .grid {
        grid-template-columns: 1fr;
      }
      section {
        min-height: auto;
        padding: 22px;
      }
    }
  </style>
</head>
<body>
  <main>
    <header>
      <div class="hero-copy">
        <h1>Document Converter Pro</h1>
        <p>Fast OCR and spreadsheet conversion in one focused workspace. Upload your file, watch the process complete, then download only when the processed document is ready.</p>
      </div>
      <div class="toolbar" aria-label="Capabilities">
        <span class="pill">PDF OCR</span>
        <span class="pill">Excel to CSV</span>
        <span class="pill">Fast mode</span>
        <span class="pill">Private processing</span>
      </div>
    </header>
    {% if message %}
      <div class="message">{{ message }}</div>
    {% endif %}
    <div class="grid">
      <section>
        <div class="panel-top">
          <span class="tag">OCR engine</span>
          <h2>PDF OCR Converter</h2>
          <p>Turn scanned PDFs into searchable documents. Fast mode skips pages that already contain selectable text.</p>
        </div>
        <form action="/ocr" method="post" enctype="multipart/form-data" data-download-form>
          <input type="file" name="pdf_file" accept="application/pdf,.pdf" required>
          <select name="mode" aria-label="OCR mode">
            <option value="fast" selected>Fast OCR - skip pages that already have text</option>
            <option value="accurate">High accuracy - slower full OCR</option>
          </select>
          <button type="submit">Convert PDF with OCR</button>
          <div class="progress-wrap" aria-hidden="true">
            <div class="progress-label">
              <span>Processing document</span>
              <span data-progress-value>0%</span>
            </div>
            <div class="progress-track">
              <div class="progress-bar"></div>
            </div>
          </div>
          <div class="status" aria-live="polite"></div>
          <a class="download-link" href="#" download>Download processed document</a>
        </form>
        <div class="details">
          <span>Best for: scanned PDFs, contracts, reports, office paperwork.</span>
          <span>Tip: use high accuracy only when fast mode misses text.</span>
        </div>
      </section>
      <section>
        <div class="panel-top">
          <span class="tag">Spreadsheet export</span>
          <h2>Excel to CSV Converter</h2>
          <p>Convert the first worksheet into a clean CSV file using a memory-efficient streaming path for XLSX workbooks.</p>
        </div>
        <form action="/excel" method="post" enctype="multipart/form-data" data-download-form>
          <input type="file" name="excel_file" accept=".xlsx,.xls" required>
          <button type="submit">Convert to CSV</button>
          <div class="progress-wrap" aria-hidden="true">
            <div class="progress-label">
              <span>Processing document</span>
              <span data-progress-value>0%</span>
            </div>
            <div class="progress-track">
              <div class="progress-bar"></div>
            </div>
          </div>
          <div class="status" aria-live="polite"></div>
          <a class="download-link" href="#" download>Download processed document</a>
        </form>
        <div class="details">
          <span>Best for: tables, exports, lead lists, accounting sheets.</span>
          <span>Output: first worksheet saved as UTF-8 CSV.</span>
        </div>
      </section>
    </div>
  </main>
  <script>
    function filenameFromDisposition(disposition, fallback) {
      if (!disposition) return fallback;
      const match = disposition.match(/filename="?([^"]+)"?/i);
      return match ? match[1] : fallback;
    }

    async function errorFromResponse(response, fallback) {
      const contentType = response.headers.get("content-type") || "";
      if (contentType.includes("application/json")) {
        const payload = await response.json();
        return payload.error || fallback;
      }
      const message = await response.text();
      return message.replace(/<[^>]*>/g, " ").replace(/\\s+/g, " ").trim() || fallback;
    }

    document.querySelectorAll("[data-download-form]").forEach((form) => {
      let activeDownloadUrl = null;

      form.addEventListener("submit", async (event) => {
        event.preventDefault();

        const button = form.querySelector("button");
        const status = form.querySelector(".status");
        const progressWrap = form.querySelector(".progress-wrap");
        const progressBar = form.querySelector(".progress-bar");
        const progressValue = form.querySelector("[data-progress-value]");
        const downloadLink = form.querySelector(".download-link");
        const originalText = button.textContent;
        const isExcel = form.action.endsWith("/excel");
        const fallbackName = isExcel ? "converted.csv" : "processed.pdf";
        let progress = 0;
        let progressTimer = null;

        if (activeDownloadUrl) {
          URL.revokeObjectURL(activeDownloadUrl);
          activeDownloadUrl = null;
        }
        downloadLink.classList.remove("ready");
        downloadLink.removeAttribute("href");

        const setProgress = (value) => {
          progress = Math.max(progress, Math.min(value, 100));
          progressBar.style.width = `${progress}%`;
          progressValue.textContent = `${Math.round(progress)}%`;
        };

        button.disabled = true;
        button.textContent = "Processing...";
        progressWrap.classList.add("active");
        progressWrap.setAttribute("aria-hidden", "false");
        setProgress(12);
        status.classList.remove("error", "success");
        status.textContent = isExcel
          ? "Uploading and converting spreadsheet. Large workbooks may take a moment."
          : "Uploading and running OCR on PDF. Multi-page documents may take a moment.";

        try {
          const formData = new FormData(form);

          progressTimer = window.setInterval(() => {
            const next = progress < 55 ? progress + 7 : progress < 86 ? progress + 3 : progress + 0.6;
            setProgress(Math.min(next, 94));
          }, 650);

          const response = await fetch(form.action, {
            method: "POST",
            body: formData,
          });

          if (!response.ok) {
            throw new Error(await errorFromResponse(response, "Processing failed."));
          }

          const blob = await response.blob();
          activeDownloadUrl = URL.createObjectURL(blob);
          downloadLink.href = activeDownloadUrl;
          downloadLink.download = filenameFromDisposition(response.headers.get("content-disposition"), fallbackName);
          downloadLink.classList.add("ready");

          setProgress(100);
          status.classList.add("success");
          status.textContent = "Processed. Your document is ready to download.";
        } catch (error) {
          setProgress(100);
          status.classList.add("error");
          status.textContent = error.message || "Processing failed.";
        } finally {
          if (progressTimer) {
            window.clearInterval(progressTimer);
          }
          button.disabled = false;
          button.textContent = originalText;
        }
      });
    });
  </script>
</body>
</html>
"""


def render_page(message=None):
    return render_template_string(PAGE, message=message)


@app.get("/")
def index():
    return render_page()


@app.get("/health")
def health():
    return {"status": "ok"}


def remove_later(path):
    def cleanup():
        try:
            os.remove(path)
        except OSError:
            pass

    @after_this_request
    def schedule_cleanup(response):
        response.call_on_close(cleanup)
        return response


def save_pdf_upload(uploaded):
    if not uploaded or uploaded.filename == "":
        raise ValueError("Please upload a PDF file.")

    filename = secure_filename(uploaded.filename)
    if not filename.lower().endswith(".pdf"):
        raise ValueError("Please upload a valid PDF file.")

    input_file = tempfile.NamedTemporaryFile(suffix=".pdf", delete=False)
    input_file.close()
    uploaded.save(input_file.name)
    return filename, input_file.name


def ocr_options(mode):
    workers = max(1, min(4, multiprocessing.cpu_count()))
    if mode == "accurate":
        return {
            "force_ocr": True,
            "deskew": True,
            "rotate_pages": True,
            "oversample": 300,
            "optimize": 1,
            "jobs": workers,
            "progress_bar": False,
        }

    return {
        "skip_text": True,
        "deskew": False,
        "rotate_pages": False,
        "oversample": 150,
        "optimize": 1,
        "jobs": workers,
        "progress_bar": False,
    }


@app.post("/ocr")
def ocr_pdf():
    output_file = tempfile.NamedTemporaryFile(suffix=".pdf", delete=False)
    output_file.close()
    output_path = output_file.name
    output_sent = False
    input_path = None

    try:
        filename, input_path = save_pdf_upload(request.files.get("pdf_file"))
        os.remove(output_path)
        ocrmypdf.ocr(
            input_path,
            output_path,
            **ocr_options(request.form.get("mode", "fast")),
        )
        download_name = f"ocr_{filename}"
        remove_later(output_path)
        output_sent = True
        return send_file(output_path, as_attachment=True, download_name=download_name)
    except ValueError as exc:
        return render_page(str(exc)), 400
    except Exception as exc:
        return render_page(f"OCR failed: {exc}"), 500
    finally:
        if input_path:
            try:
                os.remove(input_path)
            except OSError:
                pass
        if not output_sent:
            try:
                os.remove(output_path)
            except OSError:
                pass


def convert_xlsx_to_csv(input_path, output_path):
    workbook = load_workbook(input_path, read_only=True, data_only=True)
    try:
        sheet = workbook.active
        with open(output_path, "w", newline="", encoding="utf-8") as csv_file:
            writer = csv.writer(csv_file)
            for row in sheet.iter_rows(values_only=True):
                writer.writerow(["" if value is None else value for value in row])
    finally:
        workbook.close()


def convert_excel_file(input_path, output_path, extension):
    if extension == ".xlsx":
        convert_xlsx_to_csv(input_path, output_path)
        return

    df = pd.read_excel(
        input_path,
        dtype=str,
        parse_dates=False,
        keep_default_na=False,
    )
    df.to_csv(output_path, index=False, encoding="utf-8")


@app.post("/excel")
def excel_to_csv():
    uploaded = request.files.get("excel_file")
    if not uploaded or uploaded.filename == "":
        return render_page("Please upload an Excel file."), 400

    filename = secure_filename(uploaded.filename)
    if not filename.lower().endswith((".xlsx", ".xls")):
        return render_page("Please upload a valid Excel file."), 400

    extension = os.path.splitext(filename)[1].lower()
    input_file = tempfile.NamedTemporaryFile(suffix=extension, delete=False)
    output_file = tempfile.NamedTemporaryFile(suffix=".csv", delete=False)
    input_file.close()
    output_file.close()

    try:
        uploaded.save(input_file.name)
        convert_excel_file(input_file.name, output_file.name, extension)
        download_name = f"{os.path.splitext(filename)[0]}.csv"
        remove_later(output_file.name)
        return send_file(output_file.name, as_attachment=True, download_name=download_name)
    except Exception as exc:
        return render_page(f"Conversion failed: {exc}"), 500
    finally:
        try:
            os.remove(input_file.name)
        except OSError:
            pass


if __name__ == "__main__":
    app.run(
        host=os.getenv("HOST", "0.0.0.0"),
        port=int(os.getenv("PORT", "7860")),
    )