Commit 996791ac by Pedro Bueno

Corrigindo estrutura de pastas e adicionando arquivos

parents
from flask import Flask, render_template, request, send_file, redirect
import fitz, os, zipfile, sqlite3
from PIL import Image
app = Flask(__name__)
UPLOAD_FOLDER = 'uploads'
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
# Configuração do Banco de Dados (CRUD)
def init_db():
conn = sqlite3.connect('database.db')
cursor = conn.cursor()
cursor.execute('''CREATE TABLE IF NOT EXISTS configuracoes
(id INTEGER PRIMARY KEY AUTOINCREMENT, nome TEXT, coords TEXT)''')
conn.commit()
conn.close()
@app.route('/')
def index():
conn = sqlite3.connect('database.db')
configs = conn.execute('SELECT * FROM configuracoes').fetchall()
conn.close()
return render_template('index.html', configs=configs)
@app.route('/salvar', methods=['POST'])
def salvar_config():
nome = request.form['nome_config']
coords = request.form['coords']
conn = sqlite3.connect('database.db')
conn.execute('INSERT INTO configuracoes (nome, coords) VALUES (?, ?)', (nome, coords))
conn.commit()
conn.close()
return redirect('/')
@app.route('/processar', methods=['POST'])
def processar():
pdf = request.files['pdf']
inicio = int(request.form['inicio'])
fim = int(request.form['fim'])
coords = list(map(float, request.form['coords'].split(',')))
pdf_path = os.path.join(UPLOAD_FOLDER, pdf.filename)
pdf.save(pdf_path)
doc = fitz.open(pdf_path)
zip_path = os.path.join(UPLOAD_FOLDER, "resultado.zip")
with zipfile.ZipFile(zip_path, 'w') as zip_f:
for p in range(inicio, fim + 1):
if p >= len(doc): break
page = doc[p] # Lógica +1 integrada
pix = page.get_pixmap(dpi=300)
img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
fator = 300 / 72
area = (coords[0]*fator, coords[1]*fator, coords[2]*fator, coords[3]*fator)
img_cortada = img.crop(area)
img_tmp = f"pag_{p}.png"
img_cortada.save(img_tmp)
zip_f.write(img_tmp)
os.remove(img_tmp)
doc.close()
return send_file(zip_path, as_attachment=True)
if __name__ == '__main__':
init_db()
app.run(debug=True)
\ No newline at end of file
File added
body { background: #121212; color: #eee; font-family: sans-serif; display: flex; justify-content: center; padding: 50px; }
.container { background: #1e1e1e; padding: 30px; border-radius: 12px; width: 450px; box-shadow: 0 10px 30px rgba(0,0,0,0.5); }
h1 { text-align: center; color: #4CAF50; }
input { width: 100%; padding: 10px; margin: 10px 0; background: #333; border: 1px solid #444; color: white; border-radius: 5px; box-sizing: border-box; }
.row { display: flex; gap: 10px; }
button { width: 100%; padding: 12px; background: #4CAF50; border: none; color: white; font-weight: bold; border-radius: 5px; cursor: pointer; margin-top: 10px; }
button:hover { background: #45a049; }
hr { border: 0; border-top: 1px solid #333; margin: 20px 0; }
.config-list { list-style: none; padding: 0; font-size: 14px; }
.config-list li { background: #252525; padding: 10px; margin-bottom: 5px; border-radius: 4px; }
.copyright { text-align: center; font-size: 10px; color: #666; margin-top: 20px; }
\ No newline at end of file
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8">
<title>PDF Supremo CRUD</title>
<link rel="stylesheet" href="/static/style.css">
</head>
<body>
<div class="container">
<h1>Manipulador de PDF 🚀</h1>
<form action="/processar" method="post" enctype="multipart/form-data">
<input type="file" name="pdf" required>
<div class="row">
<input type="number" name="inicio" placeholder="Início" value="10">
<input type="number" name="fim" placeholder="Fim" value="11">
</div>
<input type="text" name="coords" id="coords_input" placeholder="x0, y0, x1, y1" value="0, 51, 700, 745">
<button type="submit" class="btn-primary">GERAR ZIP</button>
</form>
<hr>
<h3>Salvar Configuração de Corte</h3>
<form action="/salvar" method="post">
<input type="text" name="nome_config" placeholder="Nome (Ex: Livro de História)">
<input type="text" name="coords" placeholder="Coordenadas">
<button type="submit">Salvar no Banco</button>
</form>
<ul class="config-list">
{% for config in configs %}
<li>
<strong>{{ config[1] }}</strong>: {{ config[2] }}
</li>
{% endfor %}
</ul>
</div>
</body>
</html>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment