本方案旨在将纸质会计档案(凭证、账簿、报表等)转化为结构化、可检索、长期保存的电子档案,并建立合规的归档管理体系。核心目标包括:确保档案法律效力、实现全文检索、保障数据安全、满足长期保存要求。
根据日均处理量(如5000页/天)配置:
采用开源技术栈以控制成本:
sudo apt install tesseract-ocr tesseract-ocr-chi-sim在扫描前必须完成:
在NAPS2中配置扫描预设文件accounting_scan.naps2:
保存该文件至C:\Users\[用户名]\AppData\Local\NAPS2\Profiles,扫描时直接调用。
使用ImageMagick批量处理扫描得到的TIFF图像:
```bash 批量转换为PDF并自动纠偏 for file in .tiff; do convert "$file" -deskew 40% -density 300 -compress JPEG "output/$(basename "$file" .tiff).pdf" done ```-deskew 40%参数自动检测并旋转倾斜超过40%的页面。
创建Python脚本ocr_processor.py:
运行前安装依赖:pip install pytesseract pdf2image Pillow
使用PostgreSQL存储元数据,Elasticsearch实现全文检索:
```sql -- 创建档案主表 CREATE TABLE accounting_archive ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), barcode VARCHAR(50) UNIQUE NOT NULL, -- 条形码 original_filename VARCHAR(255), year INTEGER NOT NULL, archive_type VARCHAR(20) CHECK (archive_type IN ('凭证', '账簿', '报表')), scan_date DATE DEFAULT CURRENT_DATE, file_path TEXT NOT NULL, ocr_text TEXT, metadata JSONB ); -- 创建索引 CREATE INDEX idx_archive_year_type ON accounting_archive(year, archive_type); CREATE INDEX idx_metadata_gin ON accounting_archive USING GIN(metadata); ```
Elasticsearch索引映射配置archive-mapping.json:
创建索引命令:curl -X PUT "localhost:9200/accounting_archive" -H 'Content-Type: application/json' -d @archive-mapping.json
在NAS上建立以下目录结构:
```bash /accounting_archive/ ├── raw_scans/ 原始扫描图像 ├── processed_pdfs/ 处理后的PDF ├── thumbnails/ 缩略图(200x200) └── backup/ 增量备份 ```设置每日同步备份脚本backup.sh:
添加到crontab每日执行:0 2 /root/backup.sh
为每个档案文件生成SHA-256哈希值并记录:
```python import hashlib def generate_file_hash(file_path): sha256_hash = hashlib.sha256() with open(file_path, "rb") as f: for byte_block in iter(lambda: f.read(4096), b""): sha256_hash.update(byte_block) return sha256_hash.hexdigest() 在归档时记录哈希值 hash_value = generate_file_hash("/path/to/archive.pdf") 将hash_value存入数据库metadata字段 ```在Nginx配置文件中设置访问限制:
```nginx location /archive/ { 限制内部网络访问 allow 192.168.1.0/24; deny all; 启用HTTPS proxy_pass http://archive_app; 设置操作日志 access_log /var/log/nginx/archive_access.log; 文件下载限制(仅PDF) if ($request_filename ~ ^.\.(pdf|PDF)$) { add_header Content-Disposition 'attachment'; } } ```在Django应用中添加审计中间件:
```python middleware/audit_middleware.py import json from django.utils import timezone class AuditMiddleware: def __init__(self, get_response): self.get_response = get_response def __call__(self, request): response = self.get_response(request) if request.path.startswith('/archive/'): log_entry = { 'timestamp': timezone.now().isoformat(), 'user': request.user.username if request.user.is_authenticated else 'anonymous', 'ip': request.META.get('REMOTE_ADDR'), 'method': request.method, 'path': request.path, 'status_code': response.status_code } 写入审计日志文件 with open('/var/log/archive_audit.log', 'a') as f: f.write(json.dumps(log_entry) + '\n') return response ```在settings.py中添加:MIDDLEWARE.append('middleware.audit_middleware.AuditMiddleware')
创建Django视图提供检索API:
```python views/search.py from django.http import JsonResponse from elasticsearch import Elasticsearch es = Elasticsearch(['localhost:9200']) def search_archive(request): query = request.GET.get('q', '') year = request.GET.get('year', '') 构建ES查询 search_body = { "query": { "bool": { "must": [ {"match": {"ocr_text": query}} ] } } } if year: search_body["query"]["bool"]["filter"] = [{"term": {"year": int(year)}}] result = es.search(index="accounting_archive", body=search_body) return JsonResponse({ "count": result["hits"]["total"]["value"], "results": [hit["_source"] for hit in result["hits"]["hits"]] }) ```创建导出脚本export_by_year.py:
运行该脚本前确保数据库连接配置正确。