Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import io
|
| 3 |
+
from flask import Flask, render_template, request, send_file, Response
|
| 4 |
+
from PIL import Image
|
| 5 |
+
from PyPDF2 import PdfMerger
|
| 6 |
+
|
| 7 |
+
app = Flask(__name__)
|
| 8 |
+
|
| 9 |
+
@app.route('/')
|
| 10 |
+
def index():
|
| 11 |
+
return render_template('index.html')
|
| 12 |
+
|
| 13 |
+
# Funci贸n para manejar la subida y la combinaci贸n de archivos
|
| 14 |
+
@app.route('/combine', methods=['POST'])
|
| 15 |
+
def combine_files():
|
| 16 |
+
if 'files' not in request.files:
|
| 17 |
+
return Response('No se seleccionaron archivos', status=400)
|
| 18 |
+
|
| 19 |
+
uploaded_files = request.files.getlist('files')
|
| 20 |
+
if not uploaded_files:
|
| 21 |
+
return Response('No se seleccionaron archivos', status=400)
|
| 22 |
+
|
| 23 |
+
# NUEVO: Obtener el nivel de compresi贸n (0 a 100) del formulario
|
| 24 |
+
compression_level_str = request.form.get('compression_level', '85')
|
| 25 |
+
try:
|
| 26 |
+
# Convertir a entero. Usamos 85 como calidad est谩ndar si falla.
|
| 27 |
+
compression_level = int(compression_level_str)
|
| 28 |
+
if not 0 <= compression_level <= 100:
|
| 29 |
+
compression_level = 85
|
| 30 |
+
except ValueError:
|
| 31 |
+
compression_level = 85
|
| 32 |
+
|
| 33 |
+
pdf_merger = PdfMerger()
|
| 34 |
+
image_pdfs = []
|
| 35 |
+
|
| 36 |
+
# 1. Ajuste de calidad para la compresi贸n JPEG
|
| 37 |
+
# Para archivos PDF generados a partir de im谩genes, la calidad afecta la compresi贸n JPEG interna.
|
| 38 |
+
# 95 es alta calidad, 50 es compresi贸n media.
|
| 39 |
+
quality = compression_level
|
| 40 |
+
|
| 41 |
+
try:
|
| 42 |
+
for file in uploaded_files:
|
| 43 |
+
file_name = file.filename.lower()
|
| 44 |
+
file_stream = io.BytesIO(file.read())
|
| 45 |
+
|
| 46 |
+
if file_name.endswith(('.pdf')):
|
| 47 |
+
# Si es PDF, se agrega directamente SIN COMPRESI脫N.
|
| 48 |
+
pdf_merger.append(file_stream)
|
| 49 |
+
|
| 50 |
+
elif file_name.endswith(('.jpg', '.jpeg', '.png')):
|
| 51 |
+
# Si es una imagen, la convertimos a PDF con compresi贸n.
|
| 52 |
+
try:
|
| 53 |
+
img = Image.open(file_stream)
|
| 54 |
+
|
| 55 |
+
if img.mode != 'RGB':
|
| 56 |
+
img = img.convert('RGB')
|
| 57 |
+
|
| 58 |
+
pdf_stream = io.BytesIO()
|
| 59 |
+
|
| 60 |
+
# APLICACI脫N DE COMPRESI脫N AL CONVERTIR LA IMAGEN A PDF
|
| 61 |
+
# El argumento 'quality' controla el nivel de compresi贸n JPEG interna del PDF.
|
| 62 |
+
img.save(
|
| 63 |
+
pdf_stream,
|
| 64 |
+
format='PDF',
|
| 65 |
+
quality=quality,
|
| 66 |
+
resolution=100.0 # Resoluci贸n est谩ndar para pantalla
|
| 67 |
+
)
|
| 68 |
+
pdf_stream.seek(0)
|
| 69 |
+
|
| 70 |
+
pdf_merger.append(pdf_stream)
|
| 71 |
+
image_pdfs.append(pdf_stream)
|
| 72 |
+
except Exception as e:
|
| 73 |
+
return Response(f"Error al procesar la imagen {file_name}: {e}", status=500)
|
| 74 |
+
|
| 75 |
+
# Ignorar otros tipos de archivo
|
| 76 |
+
else:
|
| 77 |
+
print(f"Archivo ignorado: {file_name}")
|
| 78 |
+
|
| 79 |
+
final_pdf_stream = io.BytesIO()
|
| 80 |
+
pdf_merger.write(final_pdf_stream)
|
| 81 |
+
pdf_merger.close()
|
| 82 |
+
final_pdf_stream.seek(0)
|
| 83 |
+
|
| 84 |
+
return send_file(
|
| 85 |
+
final_pdf_stream,
|
| 86 |
+
as_attachment=True,
|
| 87 |
+
download_name=request.form.get('filename', 'documento_combinado.pdf'),
|
| 88 |
+
mimetype='application/pdf'
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
except Exception as e:
|
| 92 |
+
print(f"Error general: {e}")
|
| 93 |
+
return Response(f'Ocurri贸 un error inesperado en el servidor: {e}', status=500)
|
| 94 |
+
finally:
|
| 95 |
+
for pdf_stream in image_pdfs:
|
| 96 |
+
pdf_stream.close()
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
if __name__ == '__main__':
|
| 100 |
+
# Obtener el puerto de las variables de entorno, usando 7860 como fallback
|
| 101 |
+
port = int(os.environ.get('PORT', 7860))
|
| 102 |
+
|
| 103 |
+
# Ejecutar la aplicaci贸n en el host 0.0.0.0 para que sea accesible externamente
|
| 104 |
+
app.run(host='0.0.0.0', port=port, debug=False)
|