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)
|
"file_count": job.get("file_count", 0)
|
||||||
})
|
})
|
||||||
|
|
||||||
def process_pdfs(pdfdir, outputfolder):
|
def process_pdfs(pdfdir, outputfolder, job_id=None):
|
||||||
global processing_complete
|
global processing_complete
|
||||||
status_file = 'status.txt'
|
status_file = 'status.txt'
|
||||||
with open(status_file, 'w') as status_f:
|
with open(status_file, 'w') as status_f:
|
||||||
@@ -537,48 +537,55 @@ def process_pdfs(pdfdir, outputfolder):
|
|||||||
|
|
||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
|
|
||||||
# Merge by page count
|
# Merge by page count (only if PDF support is available)
|
||||||
page_groups = {}
|
if PDF_SUPPORT:
|
||||||
for filepath in pdf_files:
|
page_groups = {}
|
||||||
f = os.path.basename(filepath)
|
for filepath in pdf_files:
|
||||||
doc = fitz.open(filepath)
|
f = os.path.basename(filepath)
|
||||||
pages = len(doc)
|
doc = fitz.open(filepath)
|
||||||
doc.close()
|
pages = len(doc)
|
||||||
if pages not in page_groups:
|
doc.close()
|
||||||
page_groups[pages] = []
|
if pages not in page_groups:
|
||||||
page_groups[pages].append(filepath)
|
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:
|
msg = f"Step 3/4: Found {len(page_groups)} different page counts to merge\n"
|
||||||
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"
|
|
||||||
with open(status_file, 'a') as status_f:
|
with open(status_file, 'a') as status_f:
|
||||||
status_f.write(msg)
|
status_f.write(msg)
|
||||||
print(msg.strip())
|
print(msg.strip())
|
||||||
time.sleep(0.05)
|
time.sleep(0.1)
|
||||||
try:
|
|
||||||
merger = fitz.open()
|
for pages, files in page_groups.items():
|
||||||
for file in files:
|
msg = f"Merging {len(files)} files with {pages} pages each\n"
|
||||||
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:
|
with open(status_file, 'a') as status_f:
|
||||||
status_f.write(msg)
|
status_f.write(msg)
|
||||||
print(msg.strip())
|
print(msg.strip())
|
||||||
except Exception as e:
|
time.sleep(0.05)
|
||||||
msg = f" -> Error merging files with {pages} pages: {str(e)}\n"
|
try:
|
||||||
with open(status_file, 'a') as status_f:
|
merger = fitz.open()
|
||||||
status_f.write(msg)
|
for file in files:
|
||||||
print(msg.strip())
|
doc = fitz.open(file)
|
||||||
time.sleep(0.05)
|
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"
|
msg = f"Processing complete! Processed {len(pdf_files)} files total\n"
|
||||||
with open(status_file, 'a') as status_f:
|
with open(status_file, 'a') as status_f:
|
||||||
@@ -774,38 +781,45 @@ def process_pdfs_async(pdfdir, outputfolder, job_id):
|
|||||||
|
|
||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
|
|
||||||
# Merge by page count
|
# Merge by page count (only if PDF support is available)
|
||||||
msg = f"Step 3/4: Found {len(page_groups)} different page counts to merge\n"
|
if PDF_SUPPORT:
|
||||||
with open(status_file, 'a') as status_f:
|
msg = f"Step 3/4: Found {len(page_groups)} different page counts to merge\n"
|
||||||
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"
|
|
||||||
with open(status_file, 'a') as status_f:
|
with open(status_file, 'a') as status_f:
|
||||||
status_f.write(msg)
|
status_f.write(msg)
|
||||||
print(msg.strip())
|
print(msg.strip())
|
||||||
time.sleep(0.05)
|
time.sleep(0.1)
|
||||||
try:
|
|
||||||
merger = fitz.open()
|
for pages, files in page_groups.items():
|
||||||
for file in files:
|
msg = f"Merging {len(files)} files with {pages} pages each\n"
|
||||||
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:
|
with open(status_file, 'a') as status_f:
|
||||||
status_f.write(msg)
|
status_f.write(msg)
|
||||||
print(msg.strip())
|
print(msg.strip())
|
||||||
except Exception as e:
|
time.sleep(0.05)
|
||||||
msg = f" -> Error merging files with {pages} pages: {str(e)}\n"
|
try:
|
||||||
with open(status_file, 'a') as status_f:
|
merger = fitz.open()
|
||||||
status_f.write(msg)
|
for file in files:
|
||||||
print(msg.strip())
|
doc = fitz.open(file)
|
||||||
time.sleep(0.05)
|
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"
|
msg = f"Processing complete! Processed {len(pdf_files)} files total\n"
|
||||||
with open(status_file, 'a') as status_f:
|
with open(status_file, 'a') as status_f:
|
||||||
@@ -935,6 +949,9 @@ def clear_and_upload():
|
|||||||
|
|
||||||
def process_single_pdf(filepath):
|
def process_single_pdf(filepath):
|
||||||
"""Process a single PDF file and return the result."""
|
"""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:
|
try:
|
||||||
f = os.path.basename(filepath)
|
f = os.path.basename(filepath)
|
||||||
doc = fitz.open(filepath)
|
doc = fitz.open(filepath)
|
||||||
|
|||||||
+1
-1
@@ -173,7 +173,7 @@
|
|||||||
<div class="nav-links">
|
<div class="nav-links">
|
||||||
<a href="{{ url_for('index') }}" class="nav-link">📤 Upload Files</a>
|
<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('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>
|
</div>
|
||||||
|
|
||||||
<a href="{{ url_for('index') }}" class="back-link">← Back to Upload</a>
|
<a href="{{ url_for('index') }}" class="back-link">← Back to Upload</a>
|
||||||
|
|||||||
@@ -162,7 +162,7 @@
|
|||||||
<div class="nav-links">
|
<div class="nav-links">
|
||||||
<a href="{{ url_for('index') }}" class="nav-link">📤 Upload Files</a>
|
<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('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>
|
</div>
|
||||||
|
|
||||||
<a href="{{ url_for('index') }}" class="back-link">← Back to Upload</a>
|
<a href="{{ url_for('index') }}" class="back-link">← Back to Upload</a>
|
||||||
|
|||||||
Reference in new issue
Block a user