diff --git a/app.py b/app.py index ab764a4..f5200ed 100644 --- a/app.py +++ b/app.py @@ -444,7 +444,7 @@ def get_job_status(job_id): "file_count": job.get("file_count", 0) }) -def process_pdfs(pdfdir, outputfolder): +def process_pdfs(pdfdir, outputfolder, job_id=None): global processing_complete status_file = 'status.txt' with open(status_file, 'w') as status_f: @@ -537,48 +537,55 @@ def process_pdfs(pdfdir, outputfolder): time.sleep(0.1) - # Merge by page count - page_groups = {} - for filepath in pdf_files: - f = os.path.basename(filepath) - doc = fitz.open(filepath) - pages = len(doc) - doc.close() - if pages not in page_groups: - page_groups[pages] = [] - page_groups[pages].append(filepath) - - msg = f"Step 3/4: Found {len(page_groups)} different page counts to merge\n" - with open(status_file, 'a') as status_f: - status_f.write(msg) - print(msg.strip()) - time.sleep(0.1) - - for pages, files in page_groups.items(): - msg = f"Merging {len(files)} files with {pages} pages each\n" + # Merge by page count (only if PDF support is available) + if PDF_SUPPORT: + page_groups = {} + for filepath in pdf_files: + f = os.path.basename(filepath) + doc = fitz.open(filepath) + pages = len(doc) + doc.close() + if pages not in page_groups: + page_groups[pages] = [] + page_groups[pages].append(filepath) + + msg = f"Step 3/4: Found {len(page_groups)} different page counts to merge\n" with open(status_file, 'a') as status_f: status_f.write(msg) print(msg.strip()) - time.sleep(0.05) - try: - merger = fitz.open() - for file in files: - doc = fitz.open(file) - merger.insert_pdf(doc) - doc.close() - output_path = os.path.join(outputfolder, f'Statement-Pages-{pages}.pdf') - merger.save(output_path) - merger.close() - msg = f" -> Created {output_path}\n" + time.sleep(0.1) + + for pages, files in page_groups.items(): + msg = f"Merging {len(files)} files with {pages} pages each\n" with open(status_file, 'a') as status_f: status_f.write(msg) print(msg.strip()) - except Exception as e: - msg = f" -> Error merging files with {pages} pages: {str(e)}\n" - with open(status_file, 'a') as status_f: - status_f.write(msg) - print(msg.strip()) - time.sleep(0.05) + time.sleep(0.05) + try: + merger = fitz.open() + for file in files: + doc = fitz.open(file) + merger.insert_pdf(doc) + doc.close() + output_path = os.path.join(outputfolder, f'Statement-Pages-{pages}.pdf') + merger.save(output_path) + merger.close() + msg = f" -> Created {output_path}\n" + with open(status_file, 'a') as status_f: + status_f.write(msg) + print(msg.strip()) + except Exception as e: + msg = f" -> Error merging files with {pages} pages: {str(e)}\n" + with open(status_file, 'a') as status_f: + status_f.write(msg) + print(msg.strip()) + time.sleep(0.05) + else: + msg = "Step 3/4: PDF merging skipped (PyMuPDF not available)\n" + with open(status_file, 'a') as status_f: + status_f.write(msg) + print(msg.strip()) + time.sleep(0.1) msg = f"Processing complete! Processed {len(pdf_files)} files total\n" with open(status_file, 'a') as status_f: @@ -774,38 +781,45 @@ def process_pdfs_async(pdfdir, outputfolder, job_id): time.sleep(0.1) - # Merge by page count - msg = f"Step 3/4: Found {len(page_groups)} different page counts to merge\n" - with open(status_file, 'a') as status_f: - status_f.write(msg) - print(msg.strip()) - time.sleep(0.1) - - for pages, files in page_groups.items(): - msg = f"Merging {len(files)} files with {pages} pages each\n" + # Merge by page count (only if PDF support is available) + if PDF_SUPPORT: + msg = f"Step 3/4: Found {len(page_groups)} different page counts to merge\n" with open(status_file, 'a') as status_f: status_f.write(msg) print(msg.strip()) - time.sleep(0.05) - try: - merger = fitz.open() - for file in files: - doc = fitz.open(file) - merger.insert_pdf(doc) - doc.close() - output_path = os.path.join(outputfolder, f'Statement-Pages-{pages}.pdf') - merger.save(output_path) - merger.close() - msg = f" -> Created {output_path}\n" + time.sleep(0.1) + + for pages, files in page_groups.items(): + msg = f"Merging {len(files)} files with {pages} pages each\n" with open(status_file, 'a') as status_f: status_f.write(msg) print(msg.strip()) - except Exception as e: - msg = f" -> Error merging files with {pages} pages: {str(e)}\n" - with open(status_file, 'a') as status_f: - status_f.write(msg) - print(msg.strip()) - time.sleep(0.05) + time.sleep(0.05) + try: + merger = fitz.open() + for file in files: + doc = fitz.open(file) + merger.insert_pdf(doc) + doc.close() + output_path = os.path.join(outputfolder, f'Statement-Pages-{pages}.pdf') + merger.save(output_path) + merger.close() + msg = f" -> Created {output_path}\n" + with open(status_file, 'a') as status_f: + status_f.write(msg) + print(msg.strip()) + except Exception as e: + msg = f" -> Error merging files with {pages} pages: {str(e)}\n" + with open(status_file, 'a') as status_f: + status_f.write(msg) + print(msg.strip()) + time.sleep(0.05) + else: + msg = "Step 3/4: PDF merging skipped (PyMuPDF not available)\n" + with open(status_file, 'a') as status_f: + status_f.write(msg) + print(msg.strip()) + time.sleep(0.1) msg = f"Processing complete! Processed {len(pdf_files)} files total\n" with open(status_file, 'a') as status_f: @@ -935,6 +949,9 @@ def clear_and_upload(): def process_single_pdf(filepath): """Process a single PDF file and return the result.""" + if not PDF_SUPPORT: + return {'type': 'error', 'filename': os.path.basename(filepath), 'error': 'PDF processing not available (PyMuPDF not installed)', 'filepath': filepath} + try: f = os.path.basename(filepath) doc = fitz.open(filepath) diff --git a/templates/jobs.html b/templates/jobs.html index 9cf6223..5e8a4da 100644 --- a/templates/jobs.html +++ b/templates/jobs.html @@ -173,7 +173,7 @@
← Back to Upload diff --git a/templates/results.html b/templates/results.html index 8aaba75..d9b0bc7 100644 --- a/templates/results.html +++ b/templates/results.html @@ -162,7 +162,7 @@ ← Back to Upload