Fix PDF support and template URL issues
- Add PDF_SUPPORT checks before using PyMuPDF functions
- Handle missing PyMuPDF gracefully in PDF processing functions
- Fix url_for('jobs') to url_for('jobs_page') in templates
- Prevent AttributeError when fitz is None
This commit is contained in:
1 parent
12e46e3993
commit
25e4ccad07
3 files changed
+82
-65
No files matched your search
@@ -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)
|
||||
|
||||
+1
-1
@@ -173,7 +173,7 @@
|
||||
<div class="nav-links">
|
||||
<a href="{{ url_for('index') }}" class="nav-link">📤 Upload Files</a>
|
||||
<a href="{{ url_for('results') }}" class="nav-link">📋 View Results</a>
|
||||
<a href="{{ url_for('jobs') }}" class="nav-link">📊 View All Jobs</a>
|
||||
<a href="{{ url_for('jobs_page') }}" class="nav-link">📊 View All Jobs</a>
|
||||
</div>
|
||||
|
||||
<a href="{{ url_for('index') }}" class="back-link">← Back to Upload</a>
|
||||
|
||||
@@ -162,7 +162,7 @@
|
||||
<div class="nav-links">
|
||||
<a href="{{ url_for('index') }}" class="nav-link">📤 Upload Files</a>
|
||||
<a href="{{ url_for('results') }}" class="nav-link">📋 View Results</a>
|
||||
<a href="{{ url_for('jobs') }}" class="nav-link">📊 View All Jobs</a>
|
||||
<a href="{{ url_for('jobs_page') }}" class="nav-link">📊 View All Jobs</a>
|
||||
</div>
|
||||
|
||||
<a href="{{ url_for('index') }}" class="back-link">← Back to Upload</a>
|
||||
|
||||
Reference in new issue
Block a user