zbparse/flask_app/routes/test_readpdf.py

46 lines
1.5 KiB
Python
Raw Normal View History

2025-02-17 12:34:47 +08:00
import os.path
2025-02-17 11:30:11 +08:00
from flask import request, jsonify, Blueprint, g
import uuid
import time
from flask_app.ConnectionLimiter import require_execution_timeout
from flask_app.general.format_change import download_file
from flask_app.general.读取文件.按页读取pdf import read_pdf_main
from flask_app.routes.utils import validate_and_setup_logger
from flask_app.routes.货物标解析main import preprocess_files
test_readpdf_bp = Blueprint('test_readpdf', __name__)
@test_readpdf_bp.route('/test_readpdf', methods=['POST'])
@validate_and_setup_logger
@require_execution_timeout(timeout=1800)
def process_file():
logger = g.logger
output_folder = g.output_folder
try:
# 解析请求参数
data = request.json
file_url = data.get('file_url')
# 参数校验
if not file_url:
return jsonify({'error': 'Missing file_url parameter'})
# 生成唯一文件名
2025-02-17 12:34:47 +08:00
filename = os.path.join(output_folder,'ztbfile.pdf')
2025-02-17 11:30:11 +08:00
file_path,file_type=download_file(file_url, filename)
2025-02-17 14:46:46 +08:00
# print(file_path)
# 调用预处理函数
result = read_pdf_main(pdf_path=file_path)
# 处理结果
if not result:
return jsonify({'error': 'File processing failed'})
2025-02-17 11:30:11 +08:00
response_data={
"处理结果":"yes"
}
return jsonify(response_data)
except Exception as e:
# app.logger.error(f"Processing error: {str(e)}")
return jsonify({'error': f'Internal server error: {str(e)}'})