feat: PDF对比用iframe显示原PDF文件(支持扫描版PDF)
This commit is contained in:
23
app.py
23
app.py
@@ -673,12 +673,33 @@ def compare_view(translation_id):
|
|||||||
return jsonify({
|
return jsonify({
|
||||||
'id': translation.id,
|
'id': translation.id,
|
||||||
'filename': translation.original_filename,
|
'filename': translation.original_filename,
|
||||||
'original': original_content or '原文内容未找到(可能PDF已被删除或为扫描版)',
|
'original': original_content or '',
|
||||||
|
'original_pdf_url': f'/api/original-pdf/{translation.id}' if translation.upload_path else None,
|
||||||
'translated': translated_content,
|
'translated': translated_content,
|
||||||
'pages': translated_pages
|
'pages': translated_pages
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/api/original-pdf/<int:translation_id>')
|
||||||
|
def get_original_pdf(translation_id):
|
||||||
|
"""获取原始PDF文件"""
|
||||||
|
user = get_current_user()
|
||||||
|
if not user:
|
||||||
|
return jsonify({'error': '请登录后使用此功能'}), 401
|
||||||
|
|
||||||
|
translation = Translation.query.get(translation_id)
|
||||||
|
if not translation or (translation.user_id != user.id and user.user_type != 'admin'):
|
||||||
|
return jsonify({'error': '无权访问'}), 403
|
||||||
|
|
||||||
|
if not translation.upload_path or not os.path.exists(translation.upload_path):
|
||||||
|
return jsonify({'error': '原PDF文件不存在'}), 404
|
||||||
|
|
||||||
|
return send_file(translation.upload_path,
|
||||||
|
mimetype='application/pdf',
|
||||||
|
as_attachment=False,
|
||||||
|
download_name=translation.original_filename)
|
||||||
|
|
||||||
|
|
||||||
# ==================== 路由: 用户系统 ====================
|
# ==================== 路由: 用户系统 ====================
|
||||||
@app.route('/login', methods=['GET', 'POST'])
|
@app.route('/login', methods=['GET', 'POST'])
|
||||||
def login():
|
def login():
|
||||||
|
|||||||
@@ -84,11 +84,21 @@
|
|||||||
const response = await fetch(`/api/compare/${translationId}`);
|
const response = await fetch(`/api/compare/${translationId}`);
|
||||||
const result = await response.json();
|
const result = await response.json();
|
||||||
|
|
||||||
|
// 原文面板:如果有PDF URL用iframe显示,否则显示提取的文本
|
||||||
|
let originalHtml = '';
|
||||||
|
if (result.original_pdf_url) {
|
||||||
|
originalHtml = `<iframe src="${result.original_pdf_url}" style="width:100%;height:500px;border:none;"></iframe>`;
|
||||||
|
} else if (result.original && result.original.length > 0) {
|
||||||
|
originalHtml = `<div style="white-space:pre-wrap;font-family:monospace;">${escapeHtml(result.original)}</div>`;
|
||||||
|
} else {
|
||||||
|
originalHtml = '<div class="text-muted">原文内容未找到(可能PDF已被删除)</div>';
|
||||||
|
}
|
||||||
|
|
||||||
document.getElementById('resultContent').innerHTML = `
|
document.getElementById('resultContent').innerHTML = `
|
||||||
<div class="compare-container">
|
<div class="compare-container">
|
||||||
<div class="compare-panel original">
|
<div class="compare-panel original">
|
||||||
<h5>原文</h5>
|
<h5>原文 PDF</h5>
|
||||||
<div>${escapeHtml(result.original)}</div>
|
${originalHtml}
|
||||||
</div>
|
</div>
|
||||||
<div class="compare-panel translated">
|
<div class="compare-panel translated">
|
||||||
<h5>译文</h5>
|
<h5>译文</h5>
|
||||||
@@ -98,7 +108,7 @@
|
|||||||
`;
|
`;
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
alert('加载对比失败');
|
alert('加载对比失败: ' + error.message);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
loadResult();
|
loadResult();
|
||||||
|
|||||||
Reference in New Issue
Block a user