[前端+RAG] 修复PDF ArrayBuffer detached;修复阅读模式右侧空白(覆盖PyMuPDF固定宽度)

This commit is contained in:
2026-04-02 14:30:58 +08:00
parent 379187f675
commit 7caf7cf66a
3 changed files with 11 additions and 5 deletions

View File

@@ -12,7 +12,7 @@
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { ref, onMounted, onBeforeUnmount, nextTick, watch } from 'vue'; import { ref, onMounted, onBeforeUnmount, nextTick, watch, toRaw } from 'vue';
import * as pdfjsLib from 'pdfjs-dist'; import * as pdfjsLib from 'pdfjs-dist';
import { renderTextLayer } from 'pdfjs-dist'; import { renderTextLayer } from 'pdfjs-dist';
@@ -74,9 +74,9 @@ const loadPdf = async () => {
loading.value = true; loading.value = true;
error.value = ''; error.value = '';
try { try {
const loadingTask = pdfjsLib.getDocument({ const rawSrc = toRaw(props.src);
data: props.src instanceof ArrayBuffer ? new Uint8Array(props.src) : props.src const data = rawSrc instanceof ArrayBuffer ? new Uint8Array(rawSrc) : rawSrc;
}); const loadingTask = pdfjsLib.getDocument({ data });
pdfDoc = await loadingTask.promise; pdfDoc = await loadingTask.promise;
const numPages = pdfDoc.numPages; const numPages = pdfDoc.numPages;
pages.value = Array.from({ length: numPages }, (_, i) => i + 1); pages.value = Array.from({ length: numPages }, (_, i) => i + 1);

View File

@@ -460,7 +460,11 @@ const loadPdfFile = async () => {
params: { fileId: selectedFile.value.fileId }, params: { fileId: selectedFile.value.fileId },
responseType: 'arraybuffer' responseType: 'arraybuffer'
}); });
pdfData.value = resp.data; // 复制 ArrayBuffer 避免被 Vue 响应式代理导致 detached
const src = resp.data as ArrayBuffer;
const copy = new ArrayBuffer(src.byteLength);
new Uint8Array(copy).set(new Uint8Array(src));
pdfData.value = copy;
} catch (e: any) { } catch (e: any) {
pdfData.value = null; pdfData.value = null;
docHtml.value = '<p style="color:#999;text-align:center;margin-top:40px;">PDF 文件加载失败</p>'; docHtml.value = '<p style="color:#999;text-align:center;margin-top:40px;">PDF 文件加载失败</p>';

View File

@@ -948,9 +948,11 @@ class FileConverter:
css = '''<style> css = '''<style>
.pdf-preview { font-family: system-ui, -apple-system, sans-serif; line-height: 1.6; max-width: 100%; } .pdf-preview { font-family: system-ui, -apple-system, sans-serif; line-height: 1.6; max-width: 100%; }
.pdf-preview > div { width: 100% !important; max-width: 100% !important; }
.pdf-preview img { max-width: 100%; height: auto; } .pdf-preview img { max-width: 100%; height: auto; }
.pdf-preview p { margin: 0.3em 0; } .pdf-preview p { margin: 0.3em 0; }
.pdf-preview span { line-height: 1.5; } .pdf-preview span { line-height: 1.5; }
.pdf-page > div { width: 100% !important; max-width: 100% !important; }
</style>''' </style>'''
if not any_text: if not any_text: