Spaces:
Sleeping
Sleeping
Sync from GitHub via hub-sync
Browse files
app.py
CHANGED
|
@@ -26,109 +26,223 @@ PAGE = """
|
|
| 26 |
color-scheme: light;
|
| 27 |
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
|
| 28 |
color: #172033;
|
| 29 |
-
background: #
|
|
|
|
|
|
|
|
|
|
| 30 |
}
|
| 31 |
body {
|
| 32 |
margin: 0;
|
| 33 |
min-height: 100vh;
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
| 37 |
}
|
| 38 |
main {
|
| 39 |
-
width: min(
|
| 40 |
-
|
| 41 |
-
border: 1px solid #dbe3ef;
|
| 42 |
-
border-radius: 8px;
|
| 43 |
-
box-shadow: 0 18px 50px rgba(23, 32, 51, 0.12);
|
| 44 |
-
overflow: hidden;
|
| 45 |
}
|
| 46 |
header {
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
}
|
| 51 |
h1 {
|
| 52 |
margin: 0 0 8px;
|
| 53 |
-
font-size: clamp(
|
|
|
|
| 54 |
letter-spacing: 0;
|
|
|
|
| 55 |
}
|
| 56 |
p {
|
| 57 |
margin: 0;
|
| 58 |
color: #526173;
|
| 59 |
line-height: 1.5;
|
| 60 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
.grid {
|
| 62 |
display: grid;
|
| 63 |
grid-template-columns: repeat(2, minmax(0, 1fr));
|
| 64 |
-
gap:
|
| 65 |
}
|
| 66 |
section {
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
}
|
| 69 |
-
|
| 70 |
-
|
|
|
|
| 71 |
}
|
| 72 |
h2 {
|
| 73 |
-
margin: 0
|
| 74 |
-
font-size: 1.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
}
|
| 76 |
form {
|
| 77 |
display: grid;
|
| 78 |
gap: 16px;
|
| 79 |
-
margin-top:
|
| 80 |
}
|
| 81 |
input[type="file"],
|
| 82 |
select {
|
| 83 |
width: 100%;
|
| 84 |
-
|
| 85 |
-
padding: 12px;
|
| 86 |
border: 1px solid #cbd5e1;
|
| 87 |
border-radius: 6px;
|
| 88 |
-
background: #
|
| 89 |
color: #172033;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
}
|
| 91 |
button {
|
| 92 |
min-height: 44px;
|
| 93 |
border: 0;
|
| 94 |
border-radius: 6px;
|
| 95 |
-
background: #2563eb;
|
| 96 |
color: white;
|
| 97 |
font-weight: 700;
|
| 98 |
cursor: pointer;
|
|
|
|
|
|
|
| 99 |
}
|
| 100 |
button:hover {
|
| 101 |
-
|
| 102 |
}
|
| 103 |
button:disabled {
|
| 104 |
cursor: wait;
|
| 105 |
background: #94a3b8;
|
|
|
|
| 106 |
}
|
| 107 |
.message {
|
| 108 |
padding: 14px 16px;
|
| 109 |
-
|
|
|
|
|
|
|
| 110 |
color: #9f1239;
|
| 111 |
background: #fff1f2;
|
| 112 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
.status {
|
| 114 |
-
min-height:
|
| 115 |
color: #2563eb;
|
| 116 |
font-weight: 650;
|
| 117 |
}
|
|
|
|
|
|
|
|
|
|
| 118 |
.status.error {
|
| 119 |
color: #9f1239;
|
| 120 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
@media (max-width: 760px) {
|
|
|
|
|
|
|
|
|
|
| 122 |
.grid {
|
| 123 |
grid-template-columns: 1fr;
|
| 124 |
}
|
| 125 |
-
section + section {
|
| 126 |
-
border-left: 0;
|
| 127 |
-
border-top: 1px solid #e6ecf5;
|
| 128 |
-
}
|
| 129 |
-
header,
|
| 130 |
section {
|
| 131 |
-
|
|
|
|
| 132 |
}
|
| 133 |
}
|
| 134 |
</style>
|
|
@@ -136,16 +250,27 @@ PAGE = """
|
|
| 136 |
<body>
|
| 137 |
<main>
|
| 138 |
<header>
|
| 139 |
-
<
|
| 140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
</header>
|
| 142 |
{% if message %}
|
| 143 |
<div class="message">{{ message }}</div>
|
| 144 |
{% endif %}
|
| 145 |
<div class="grid">
|
| 146 |
<section>
|
| 147 |
-
<
|
| 148 |
-
|
|
|
|
|
|
|
|
|
|
| 149 |
<form action="/ocr" method="post" enctype="multipart/form-data" data-download-form>
|
| 150 |
<input type="file" name="pdf_file" accept="application/pdf,.pdf" required>
|
| 151 |
<select name="mode" aria-label="OCR mode">
|
|
@@ -153,17 +278,48 @@ PAGE = """
|
|
| 153 |
<option value="accurate">High accuracy - slower full OCR</option>
|
| 154 |
</select>
|
| 155 |
<button type="submit">Process PDF</button>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
<div class="status" aria-live="polite"></div>
|
|
|
|
| 157 |
</form>
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
</section>
|
| 159 |
<section>
|
| 160 |
-
<
|
| 161 |
-
|
|
|
|
|
|
|
|
|
|
| 162 |
<form action="/excel" method="post" enctype="multipart/form-data" data-download-form>
|
| 163 |
<input type="file" name="excel_file" accept=".xlsx,.xls" required>
|
| 164 |
<button type="submit">Convert to CSV</button>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
<div class="status" aria-live="polite"></div>
|
|
|
|
| 166 |
</form>
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
</section>
|
| 168 |
</div>
|
| 169 |
</main>
|
|
@@ -175,22 +331,51 @@ PAGE = """
|
|
| 175 |
}
|
| 176 |
|
| 177 |
document.querySelectorAll("[data-download-form]").forEach((form) => {
|
|
|
|
|
|
|
| 178 |
form.addEventListener("submit", async (event) => {
|
| 179 |
event.preventDefault();
|
| 180 |
|
| 181 |
const button = form.querySelector("button");
|
| 182 |
const status = form.querySelector(".status");
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
const originalText = button.textContent;
|
| 184 |
const fallbackName = form.action.endsWith("/excel") ? "converted.csv" : "processed.pdf";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
|
| 186 |
button.disabled = true;
|
| 187 |
button.textContent = "Processing...";
|
| 188 |
-
|
|
|
|
|
|
|
|
|
|
| 189 |
status.textContent = form.action.endsWith("/ocr")
|
| 190 |
? "Uploading and processing. Fast mode skips pages that already contain text."
|
| 191 |
: "Uploading and converting. Large workbooks may take a moment.";
|
| 192 |
|
| 193 |
try {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
const response = await fetch(form.action, {
|
| 195 |
method: "POST",
|
| 196 |
body: new FormData(form),
|
|
@@ -202,21 +387,22 @@ PAGE = """
|
|
| 202 |
}
|
| 203 |
|
| 204 |
const blob = await response.blob();
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
status.textContent = "Done. Your download should start automatically.";
|
| 215 |
-
form.reset();
|
| 216 |
} catch (error) {
|
|
|
|
| 217 |
status.classList.add("error");
|
| 218 |
status.textContent = error.message || "Processing failed.";
|
| 219 |
} finally {
|
|
|
|
|
|
|
|
|
|
| 220 |
button.disabled = false;
|
| 221 |
button.textContent = originalText;
|
| 222 |
}
|
|
|
|
| 26 |
color-scheme: light;
|
| 27 |
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
|
| 28 |
color: #172033;
|
| 29 |
+
background: #e8edf5;
|
| 30 |
+
}
|
| 31 |
+
* {
|
| 32 |
+
box-sizing: border-box;
|
| 33 |
}
|
| 34 |
body {
|
| 35 |
margin: 0;
|
| 36 |
min-height: 100vh;
|
| 37 |
+
padding: 28px 18px;
|
| 38 |
+
background:
|
| 39 |
+
radial-gradient(circle at top left, rgba(37, 99, 235, 0.16), transparent 30rem),
|
| 40 |
+
linear-gradient(135deg, #f8fafc 0%, #e8edf5 52%, #e6f4ef 100%);
|
| 41 |
}
|
| 42 |
main {
|
| 43 |
+
width: min(1120px, 100%);
|
| 44 |
+
margin: 0 auto;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
}
|
| 46 |
header {
|
| 47 |
+
display: grid;
|
| 48 |
+
gap: 18px;
|
| 49 |
+
padding: 34px 0 26px;
|
| 50 |
}
|
| 51 |
h1 {
|
| 52 |
margin: 0 0 8px;
|
| 53 |
+
font-size: clamp(2rem, 5vw, 4rem);
|
| 54 |
+
line-height: 1;
|
| 55 |
letter-spacing: 0;
|
| 56 |
+
color: #0f172a;
|
| 57 |
}
|
| 58 |
p {
|
| 59 |
margin: 0;
|
| 60 |
color: #526173;
|
| 61 |
line-height: 1.5;
|
| 62 |
}
|
| 63 |
+
.hero-copy {
|
| 64 |
+
max-width: 740px;
|
| 65 |
+
}
|
| 66 |
+
.hero-copy p {
|
| 67 |
+
font-size: 1.03rem;
|
| 68 |
+
}
|
| 69 |
+
.toolbar {
|
| 70 |
+
display: flex;
|
| 71 |
+
flex-wrap: wrap;
|
| 72 |
+
gap: 10px;
|
| 73 |
+
}
|
| 74 |
+
.pill {
|
| 75 |
+
display: inline-flex;
|
| 76 |
+
align-items: center;
|
| 77 |
+
min-height: 32px;
|
| 78 |
+
padding: 6px 10px;
|
| 79 |
+
border-radius: 999px;
|
| 80 |
+
border: 1px solid rgba(37, 99, 235, 0.18);
|
| 81 |
+
background: rgba(255, 255, 255, 0.72);
|
| 82 |
+
color: #1f3f75;
|
| 83 |
+
font-size: 0.88rem;
|
| 84 |
+
font-weight: 700;
|
| 85 |
+
}
|
| 86 |
.grid {
|
| 87 |
display: grid;
|
| 88 |
grid-template-columns: repeat(2, minmax(0, 1fr));
|
| 89 |
+
gap: 18px;
|
| 90 |
}
|
| 91 |
section {
|
| 92 |
+
display: grid;
|
| 93 |
+
align-content: start;
|
| 94 |
+
gap: 18px;
|
| 95 |
+
min-height: 440px;
|
| 96 |
+
padding: 26px;
|
| 97 |
+
background: rgba(255, 255, 255, 0.86);
|
| 98 |
+
border: 1px solid rgba(148, 163, 184, 0.32);
|
| 99 |
+
border-radius: 8px;
|
| 100 |
+
box-shadow: 0 20px 55px rgba(15, 23, 42, 0.12);
|
| 101 |
+
backdrop-filter: blur(12px);
|
| 102 |
}
|
| 103 |
+
.panel-top {
|
| 104 |
+
display: grid;
|
| 105 |
+
gap: 10px;
|
| 106 |
}
|
| 107 |
h2 {
|
| 108 |
+
margin: 0;
|
| 109 |
+
font-size: 1.35rem;
|
| 110 |
+
color: #111827;
|
| 111 |
+
}
|
| 112 |
+
.tag {
|
| 113 |
+
width: fit-content;
|
| 114 |
+
padding: 6px 9px;
|
| 115 |
+
border-radius: 6px;
|
| 116 |
+
background: #ecfdf5;
|
| 117 |
+
color: #047857;
|
| 118 |
+
font-size: 0.78rem;
|
| 119 |
+
font-weight: 800;
|
| 120 |
+
text-transform: uppercase;
|
| 121 |
+
letter-spacing: 0.05em;
|
| 122 |
}
|
| 123 |
form {
|
| 124 |
display: grid;
|
| 125 |
gap: 16px;
|
| 126 |
+
margin-top: 4px;
|
| 127 |
}
|
| 128 |
input[type="file"],
|
| 129 |
select {
|
| 130 |
width: 100%;
|
| 131 |
+
min-height: 48px;
|
| 132 |
+
padding: 12px 14px;
|
| 133 |
border: 1px solid #cbd5e1;
|
| 134 |
border-radius: 6px;
|
| 135 |
+
background: #ffffff;
|
| 136 |
color: #172033;
|
| 137 |
+
font: inherit;
|
| 138 |
+
}
|
| 139 |
+
input[type="file"]::file-selector-button {
|
| 140 |
+
margin-right: 12px;
|
| 141 |
+
border: 0;
|
| 142 |
+
border-radius: 5px;
|
| 143 |
+
background: #e0f2fe;
|
| 144 |
+
color: #075985;
|
| 145 |
+
padding: 9px 12px;
|
| 146 |
+
font-weight: 800;
|
| 147 |
}
|
| 148 |
button {
|
| 149 |
min-height: 44px;
|
| 150 |
border: 0;
|
| 151 |
border-radius: 6px;
|
| 152 |
+
background: linear-gradient(135deg, #2563eb, #059669);
|
| 153 |
color: white;
|
| 154 |
font-weight: 700;
|
| 155 |
cursor: pointer;
|
| 156 |
+
font: inherit;
|
| 157 |
+
box-shadow: 0 10px 22px rgba(37, 99, 235, 0.22);
|
| 158 |
}
|
| 159 |
button:hover {
|
| 160 |
+
filter: brightness(0.96);
|
| 161 |
}
|
| 162 |
button:disabled {
|
| 163 |
cursor: wait;
|
| 164 |
background: #94a3b8;
|
| 165 |
+
box-shadow: none;
|
| 166 |
}
|
| 167 |
.message {
|
| 168 |
padding: 14px 16px;
|
| 169 |
+
margin-bottom: 18px;
|
| 170 |
+
border: 1px solid #fecdd3;
|
| 171 |
+
border-radius: 8px;
|
| 172 |
color: #9f1239;
|
| 173 |
background: #fff1f2;
|
| 174 |
}
|
| 175 |
+
.progress-wrap {
|
| 176 |
+
display: none;
|
| 177 |
+
gap: 8px;
|
| 178 |
+
}
|
| 179 |
+
.progress-wrap.active {
|
| 180 |
+
display: grid;
|
| 181 |
+
}
|
| 182 |
+
.progress-label {
|
| 183 |
+
display: flex;
|
| 184 |
+
justify-content: space-between;
|
| 185 |
+
gap: 12px;
|
| 186 |
+
color: #334155;
|
| 187 |
+
font-size: 0.9rem;
|
| 188 |
+
font-weight: 750;
|
| 189 |
+
}
|
| 190 |
+
.progress-track {
|
| 191 |
+
height: 10px;
|
| 192 |
+
overflow: hidden;
|
| 193 |
+
border-radius: 999px;
|
| 194 |
+
background: #dbe5f1;
|
| 195 |
+
}
|
| 196 |
+
.progress-bar {
|
| 197 |
+
width: 0%;
|
| 198 |
+
height: 100%;
|
| 199 |
+
border-radius: inherit;
|
| 200 |
+
background: linear-gradient(90deg, #2563eb, #22c55e, #f59e0b);
|
| 201 |
+
transition: width 320ms ease;
|
| 202 |
+
}
|
| 203 |
.status {
|
| 204 |
+
min-height: 24px;
|
| 205 |
color: #2563eb;
|
| 206 |
font-weight: 650;
|
| 207 |
}
|
| 208 |
+
.status.success {
|
| 209 |
+
color: #047857;
|
| 210 |
+
}
|
| 211 |
.status.error {
|
| 212 |
color: #9f1239;
|
| 213 |
}
|
| 214 |
+
.download-link {
|
| 215 |
+
display: none;
|
| 216 |
+
align-items: center;
|
| 217 |
+
justify-content: center;
|
| 218 |
+
min-height: 44px;
|
| 219 |
+
border-radius: 6px;
|
| 220 |
+
background: #111827;
|
| 221 |
+
color: #ffffff;
|
| 222 |
+
text-decoration: none;
|
| 223 |
+
font-weight: 800;
|
| 224 |
+
}
|
| 225 |
+
.download-link.ready {
|
| 226 |
+
display: flex;
|
| 227 |
+
}
|
| 228 |
+
.details {
|
| 229 |
+
display: grid;
|
| 230 |
+
gap: 8px;
|
| 231 |
+
margin-top: auto;
|
| 232 |
+
padding-top: 8px;
|
| 233 |
+
color: #64748b;
|
| 234 |
+
font-size: 0.9rem;
|
| 235 |
+
}
|
| 236 |
@media (max-width: 760px) {
|
| 237 |
+
body {
|
| 238 |
+
padding: 18px 12px;
|
| 239 |
+
}
|
| 240 |
.grid {
|
| 241 |
grid-template-columns: 1fr;
|
| 242 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
section {
|
| 244 |
+
min-height: auto;
|
| 245 |
+
padding: 22px;
|
| 246 |
}
|
| 247 |
}
|
| 248 |
</style>
|
|
|
|
| 250 |
<body>
|
| 251 |
<main>
|
| 252 |
<header>
|
| 253 |
+
<div class="hero-copy">
|
| 254 |
+
<h1>Document Converter Pro</h1>
|
| 255 |
+
<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>
|
| 256 |
+
</div>
|
| 257 |
+
<div class="toolbar" aria-label="Capabilities">
|
| 258 |
+
<span class="pill">PDF OCR</span>
|
| 259 |
+
<span class="pill">Excel to CSV</span>
|
| 260 |
+
<span class="pill">Fast mode</span>
|
| 261 |
+
<span class="pill">Private processing</span>
|
| 262 |
+
</div>
|
| 263 |
</header>
|
| 264 |
{% if message %}
|
| 265 |
<div class="message">{{ message }}</div>
|
| 266 |
{% endif %}
|
| 267 |
<div class="grid">
|
| 268 |
<section>
|
| 269 |
+
<div class="panel-top">
|
| 270 |
+
<span class="tag">OCR engine</span>
|
| 271 |
+
<h2>PDF OCR Converter</h2>
|
| 272 |
+
<p>Turn scanned PDFs into searchable documents. Fast mode skips pages that already contain selectable text.</p>
|
| 273 |
+
</div>
|
| 274 |
<form action="/ocr" method="post" enctype="multipart/form-data" data-download-form>
|
| 275 |
<input type="file" name="pdf_file" accept="application/pdf,.pdf" required>
|
| 276 |
<select name="mode" aria-label="OCR mode">
|
|
|
|
| 278 |
<option value="accurate">High accuracy - slower full OCR</option>
|
| 279 |
</select>
|
| 280 |
<button type="submit">Process PDF</button>
|
| 281 |
+
<div class="progress-wrap" aria-hidden="true">
|
| 282 |
+
<div class="progress-label">
|
| 283 |
+
<span>Processing document</span>
|
| 284 |
+
<span data-progress-value>0%</span>
|
| 285 |
+
</div>
|
| 286 |
+
<div class="progress-track">
|
| 287 |
+
<div class="progress-bar"></div>
|
| 288 |
+
</div>
|
| 289 |
+
</div>
|
| 290 |
<div class="status" aria-live="polite"></div>
|
| 291 |
+
<a class="download-link" href="#" download>Download processed document</a>
|
| 292 |
</form>
|
| 293 |
+
<div class="details">
|
| 294 |
+
<span>Best for: scanned PDFs, contracts, reports, office paperwork.</span>
|
| 295 |
+
<span>Tip: use high accuracy only when fast mode misses text.</span>
|
| 296 |
+
</div>
|
| 297 |
</section>
|
| 298 |
<section>
|
| 299 |
+
<div class="panel-top">
|
| 300 |
+
<span class="tag">Spreadsheet export</span>
|
| 301 |
+
<h2>Excel to CSV Converter</h2>
|
| 302 |
+
<p>Convert the first worksheet into a clean CSV file using a memory-efficient streaming path for XLSX workbooks.</p>
|
| 303 |
+
</div>
|
| 304 |
<form action="/excel" method="post" enctype="multipart/form-data" data-download-form>
|
| 305 |
<input type="file" name="excel_file" accept=".xlsx,.xls" required>
|
| 306 |
<button type="submit">Convert to CSV</button>
|
| 307 |
+
<div class="progress-wrap" aria-hidden="true">
|
| 308 |
+
<div class="progress-label">
|
| 309 |
+
<span>Processing document</span>
|
| 310 |
+
<span data-progress-value>0%</span>
|
| 311 |
+
</div>
|
| 312 |
+
<div class="progress-track">
|
| 313 |
+
<div class="progress-bar"></div>
|
| 314 |
+
</div>
|
| 315 |
+
</div>
|
| 316 |
<div class="status" aria-live="polite"></div>
|
| 317 |
+
<a class="download-link" href="#" download>Download processed document</a>
|
| 318 |
</form>
|
| 319 |
+
<div class="details">
|
| 320 |
+
<span>Best for: tables, exports, lead lists, accounting sheets.</span>
|
| 321 |
+
<span>Output: first worksheet saved as UTF-8 CSV.</span>
|
| 322 |
+
</div>
|
| 323 |
</section>
|
| 324 |
</div>
|
| 325 |
</main>
|
|
|
|
| 331 |
}
|
| 332 |
|
| 333 |
document.querySelectorAll("[data-download-form]").forEach((form) => {
|
| 334 |
+
let activeDownloadUrl = null;
|
| 335 |
+
|
| 336 |
form.addEventListener("submit", async (event) => {
|
| 337 |
event.preventDefault();
|
| 338 |
|
| 339 |
const button = form.querySelector("button");
|
| 340 |
const status = form.querySelector(".status");
|
| 341 |
+
const progressWrap = form.querySelector(".progress-wrap");
|
| 342 |
+
const progressBar = form.querySelector(".progress-bar");
|
| 343 |
+
const progressValue = form.querySelector("[data-progress-value]");
|
| 344 |
+
const downloadLink = form.querySelector(".download-link");
|
| 345 |
const originalText = button.textContent;
|
| 346 |
const fallbackName = form.action.endsWith("/excel") ? "converted.csv" : "processed.pdf";
|
| 347 |
+
let progress = 0;
|
| 348 |
+
let progressTimer = null;
|
| 349 |
+
|
| 350 |
+
if (activeDownloadUrl) {
|
| 351 |
+
URL.revokeObjectURL(activeDownloadUrl);
|
| 352 |
+
activeDownloadUrl = null;
|
| 353 |
+
}
|
| 354 |
+
downloadLink.classList.remove("ready");
|
| 355 |
+
downloadLink.removeAttribute("href");
|
| 356 |
+
|
| 357 |
+
const setProgress = (value) => {
|
| 358 |
+
progress = Math.max(progress, Math.min(value, 100));
|
| 359 |
+
progressBar.style.width = `${progress}%`;
|
| 360 |
+
progressValue.textContent = `${Math.round(progress)}%`;
|
| 361 |
+
};
|
| 362 |
|
| 363 |
button.disabled = true;
|
| 364 |
button.textContent = "Processing...";
|
| 365 |
+
progressWrap.classList.add("active");
|
| 366 |
+
progressWrap.setAttribute("aria-hidden", "false");
|
| 367 |
+
setProgress(8);
|
| 368 |
+
status.classList.remove("error", "success");
|
| 369 |
status.textContent = form.action.endsWith("/ocr")
|
| 370 |
? "Uploading and processing. Fast mode skips pages that already contain text."
|
| 371 |
: "Uploading and converting. Large workbooks may take a moment.";
|
| 372 |
|
| 373 |
try {
|
| 374 |
+
progressTimer = window.setInterval(() => {
|
| 375 |
+
const next = progress < 55 ? progress + 7 : progress < 86 ? progress + 3 : progress + 0.6;
|
| 376 |
+
setProgress(Math.min(next, 94));
|
| 377 |
+
}, 650);
|
| 378 |
+
|
| 379 |
const response = await fetch(form.action, {
|
| 380 |
method: "POST",
|
| 381 |
body: new FormData(form),
|
|
|
|
| 387 |
}
|
| 388 |
|
| 389 |
const blob = await response.blob();
|
| 390 |
+
activeDownloadUrl = URL.createObjectURL(blob);
|
| 391 |
+
downloadLink.href = activeDownloadUrl;
|
| 392 |
+
downloadLink.download = filenameFromDisposition(response.headers.get("content-disposition"), fallbackName);
|
| 393 |
+
downloadLink.classList.add("ready");
|
| 394 |
+
|
| 395 |
+
setProgress(100);
|
| 396 |
+
status.classList.add("success");
|
| 397 |
+
status.textContent = "Processed. Your document is ready to download.";
|
|
|
|
|
|
|
|
|
|
| 398 |
} catch (error) {
|
| 399 |
+
setProgress(100);
|
| 400 |
status.classList.add("error");
|
| 401 |
status.textContent = error.message || "Processing failed.";
|
| 402 |
} finally {
|
| 403 |
+
if (progressTimer) {
|
| 404 |
+
window.clearInterval(progressTimer);
|
| 405 |
+
}
|
| 406 |
button.disabled = false;
|
| 407 |
button.textContent = originalText;
|
| 408 |
}
|