import os import sys import uuid import tempfile import pdfplumber from flask import Flask, request, jsonify, send_file from add_signature import find_signature_positions, add_signature_to_pdf from add_watermark import add_watermark_to_pdf from extract_table import extract_temp_time, extract_pdf_table_to_excel, extract_temp_by_datetime_pattern, allowed_file, \ safe_filename from lib import Qiniu from werkzeug.utils import secure_filename app = Flask(__name__) UPLOAD_FOLDER = tempfile.gettempdir() app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER @app.route('/add_signature', methods=['POST']) def add_signature(): try: # 获取请求参数 data = request.get_json() pdf_url = data.get('pdf_url') if not pdf_url: return jsonify({"error": "缺少pdf_url参数"}), 400 # 下载PDF文件 local_pdf = Qiniu.download_file(pdf_url) # 查找签名位置(使用默认公章图片) signature_img = './static/报告专用章.png' # 确保项目目录下有这个文件 positions = find_signature_positions(local_pdf) if not positions: os.remove(local_pdf) return jsonify({"error": "未找到签名位置"}), 400 # 生成带公章的PDF signature_pdf = os.path.join("./temp", f"signature_{os.path.basename(local_pdf)}") add_signature_to_pdf(local_pdf, signature_pdf, signature_img, positions) # 上传到七牛云 file_key = f"UpImage/{uuid.uuid4()}.pdf" new_pdf_url = Qiniu.upload_to_qiniu(signature_pdf, file_key) # 清理临时文件 os.remove(local_pdf) os.remove(signature_pdf) return jsonify({ "success": True, "signature_pdf_url": new_pdf_url }) except Exception as e: return jsonify({ "error": str(e), "success": False, }), 500 @app.route('/add_watermark', methods=['POST']) def add_watermark(): try: # 获取请求参数 data = request.get_json() pdf_url = data.get('pdf_url') if not pdf_url: return jsonify({"error": "缺少pdf_url参数"}), 400 # 下载PDF文件 local_pdf = Qiniu.download_file(pdf_url) # 水印文件 pdf_file_mark = './static/watermark.pdf' # 确保项目目录下有这个文件 # 生成带公章的PDF watermark_pdf = os.path.join("./temp", f"watermark_{os.path.basename(local_pdf)}") add_watermark_to_pdf(local_pdf, pdf_file_mark, watermark_pdf) # 上传到七牛云 file_key = f"UpImage/{uuid.uuid4()}.pdf" new_pdf_url = Qiniu.upload_to_qiniu(watermark_pdf, file_key) # 清理临时文件 os.remove(local_pdf) os.remove(watermark_pdf) return jsonify({ "success": True, "watermark_pdf_url": new_pdf_url }) except Exception as e: return jsonify({ "error": str(e), "success": False, }), 500 @app.route('/extract_table', methods=['POST']) def extract_table(): if 'file' not in request.files: return jsonify({'error': 'No file part'}), 400 file = request.files['file'] if file.filename == '': return jsonify({'error': 'No selected file'}), 400 if file and allowed_file(file.filename): original_filename = file.filename filename = safe_filename(original_filename) # filename = secure_filename(safe_name) filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename) file.save(filepath) df = None with pdfplumber.open(filepath) as pdf: # 获取第一页 first_page = pdf.pages[0] text = first_page.extract_text() if text: if "温湿度数据报告" in text: df = extract_pdf_table_to_excel(filepath) if "历史数据表" in text: df = extract_temp_time(filepath) if "设备汇总报告" in text: df = extract_temp_by_datetime_pattern(filepath) if df is None: os.remove(filepath) return jsonify({'error': '所有处理方法均失败'}), 400 # 保存Excel文件 output_filename = filename.replace('.pdf', '.xlsx') output_path = os.path.join(app.config['UPLOAD_FOLDER'], output_filename) df.to_excel(output_path, index=False, engine='openpyxl') # 删除上传的PDF文件 os.remove(filepath) # 返回Excel文件 try: return send_file( output_path, as_attachment=True, download_name=output_filename, mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' ) finally: # 确保临时文件最终被删除 try: os.remove(output_path) except: pass else: return jsonify({'error': 'Invalid file type'}), 400 if __name__ == '__main__': print("项目地址:", os.path.dirname(__file__)) if len(sys.argv) != 2: print("请填写端口号") sys.exit() # app.debug = True # 设置调试模式,生产模式的时候要关掉debug # app.config['JSON_AS_ASCII'] = False app.run(host='0.0.0.0', port=6500, debug=True)