Update app.py
Browse files
app.py
CHANGED
|
@@ -2758,6 +2758,42 @@ def save_pdf_to_generated_dir(pdf_stream: io.BytesIO, project_id: str) -> str:
|
|
| 2758 |
logger.error(f"Failed to save PDF to generated dir: {e}", exc_info=True)
|
| 2759 |
return None
|
| 2760 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2761 |
@app.route('/')
|
| 2762 |
def index():
|
| 2763 |
return render_template('app_index.html')
|
|
@@ -2833,7 +2869,8 @@ def process_pdf():
|
|
| 2833 |
total_time = time.time() - start_time
|
| 2834 |
print(f"-----------------------------Execution Time save_pdf_to_generated_dir() : {total_time}-----------------------------\n")
|
| 2835 |
# }
|
| 2836 |
-
|
|
|
|
| 2837 |
# {
|
| 2838 |
# Extract & process
|
| 2839 |
# output_path, result = extract_images_from_pdf(saved_pdf_path)
|
|
|
|
| 2758 |
logger.error(f"Failed to save PDF to generated dir: {e}", exc_info=True)
|
| 2759 |
return None
|
| 2760 |
|
| 2761 |
+
def pdf_to_images_with_size_check(pdf_path, output_dir, size_limit_mb=4):
|
| 2762 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 2763 |
+
|
| 2764 |
+
# Convert PDF to images
|
| 2765 |
+
images = convert_from_path(pdf_path, dpi=300) # 300 DPI keeps quality
|
| 2766 |
+
saved_files = []
|
| 2767 |
+
|
| 2768 |
+
for i, img in enumerate(images, start=1):
|
| 2769 |
+
output_path = os.path.join(output_dir, f"page_{i}.jpg")
|
| 2770 |
+
|
| 2771 |
+
# Save to memory first to check size
|
| 2772 |
+
img_bytes = io.BytesIO()
|
| 2773 |
+
img.save(img_bytes, format="JPEG", quality=95) # near-lossless
|
| 2774 |
+
size_mb = len(img_bytes.getvalue()) / (1024 * 1024)
|
| 2775 |
+
|
| 2776 |
+
if size_mb > size_limit_mb:
|
| 2777 |
+
print(f"Page {i}: {size_mb:.2f} MB → compressing...")
|
| 2778 |
+
# Compress until under size limit
|
| 2779 |
+
quality = 95
|
| 2780 |
+
while size_mb > size_limit_mb and quality > 70: # don't go below 70
|
| 2781 |
+
img_bytes = io.BytesIO()
|
| 2782 |
+
img.save(img_bytes, format="JPEG", quality=quality)
|
| 2783 |
+
size_mb = len(img_bytes.getvalue()) / (1024 * 1024)
|
| 2784 |
+
quality -= 5
|
| 2785 |
+
else:
|
| 2786 |
+
print(f"Page {i}: {size_mb:.2f} MB → no compression needed.")
|
| 2787 |
+
|
| 2788 |
+
# Write final image to disk
|
| 2789 |
+
with open(output_path, "wb") as f:
|
| 2790 |
+
f.write(img_bytes.getvalue())
|
| 2791 |
+
|
| 2792 |
+
saved_files.append(output_path)
|
| 2793 |
+
|
| 2794 |
+
return saved_files
|
| 2795 |
+
|
| 2796 |
+
|
| 2797 |
@app.route('/')
|
| 2798 |
def index():
|
| 2799 |
return render_template('app_index.html')
|
|
|
|
| 2869 |
total_time = time.time() - start_time
|
| 2870 |
print(f"-----------------------------Execution Time save_pdf_to_generated_dir() : {total_time}-----------------------------\n")
|
| 2871 |
# }
|
| 2872 |
+
compressed_pages = pdf_to_images_with_size_check(pdf_path, "/tmp/compressed_pages", size_limit_mb=4)
|
| 2873 |
+
|
| 2874 |
# {
|
| 2875 |
# Extract & process
|
| 2876 |
# output_path, result = extract_images_from_pdf(saved_pdf_path)
|