@koi-br/ocr-web-sdk 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,754 @@
1
+ # OCR Web SDK
2
+
3
+ 一个功能强大的 Vue3 文件预览组件 SDK,支持多种 Office 文件格式和图片格式的在线预览,包括 PDF、Word、Excel、图片、OFD、TIF 等格式。
4
+
5
+ ## ✨ 功能特性
6
+
7
+ ### 📄 PDF 预览
8
+ - ✅ 完整的 PDF 文档渲染和浏览
9
+ - ✅ 目录导航和印章列表(支持侧边栏切换)
10
+ - ✅ 多种缩放模式(适应宽度、适应高度、实际大小、适应页面)
11
+ - ✅ 缩放控制(放大、缩小、重置)
12
+ - ✅ 全屏预览
13
+ - ✅ 文本选择和复制
14
+ - ✅ 页面跳转和定位
15
+ - ✅ 支持外部调用跳转方法
16
+ - ✅ 支持跳转事件监听
17
+ - ✅ PDF 分块数据支持(blocksData)
18
+
19
+ ### 📊 Excel 预览
20
+ - ✅ XLSX 格式完整支持
21
+ - ✅ 缩放控制(0.5x - 2x)
22
+ - ✅ 下载功能(可选)
23
+
24
+ ### 📝 Word 预览
25
+ - ✅ DOCX 格式完整支持
26
+ - ✅ 文档样式渲染
27
+ - ✅ 下载功能(可选)
28
+
29
+ ### 🖼️ 图片预览
30
+ - ✅ 支持 JPG、PNG、GIF、BMP、WEBP、SVG、APNG、AVIF 等格式
31
+ - ✅ 缩放控制(可配置范围)
32
+ - ✅ 图片旋转(左右旋转)
33
+ - ✅ 拖拽平移
34
+ - ✅ 滚轮缩放
35
+ - ✅ 查看原图功能
36
+ - ✅ 下载功能(可选)
37
+
38
+ ### 📑 OFD 预览
39
+ - ✅ OFD 格式文件预览
40
+ - ✅ 下载功能(可选)
41
+
42
+ ### 🖨️ TIF 预览
43
+ - ✅ TIF/TIFF 多页图片支持
44
+ - ✅ 缩放控制
45
+ - ✅ 图片旋转
46
+ - ✅ 拖拽平移
47
+ - ✅ 查看原图功能
48
+ - ✅ 下载功能(可选)
49
+
50
+ ## 📦 安装
51
+
52
+ ```bash
53
+ npm install ocr-web-sdk
54
+ # 或
55
+ yarn add ocr-web-sdk
56
+ # 或
57
+ pnpm add ocr-web-sdk
58
+ ```
59
+
60
+ ## 🔧 依赖要求
61
+
62
+ ### 必需依赖
63
+ - **Vue 3.x** - Vue 框架(peer dependency)
64
+ - **@arco-design/web-vue** - Arco Design UI 组件库
65
+
66
+ ### 内部依赖(已包含)
67
+ - `pdfjs-dist` - PDF 渲染引擎
68
+ - `@vue-office/excel` - Excel 预览组件
69
+ - `docx-preview` - Word 文档预览
70
+ - `bestofdview` - OFD 文件预览
71
+ - `lucide-vue-next` - 图标库
72
+
73
+ ## 🚀 快速开始
74
+
75
+ ### 方式一:使用 FilePreview 组件(推荐)
76
+
77
+ `FilePreview` 是一个统一的文件预览入口组件,会根据文件类型自动选择合适的预览组件。
78
+
79
+ ```vue
80
+ <template>
81
+ <div>
82
+ <FilePreview ref="previewRef" :is-download="true" />
83
+ <button @click="previewFile">预览文件</button>
84
+ </div>
85
+ </template>
86
+
87
+ <script setup>
88
+ import { ref } from 'vue';
89
+ import { FilePreview } from 'ocr-web-sdk';
90
+
91
+ const previewRef = ref(null);
92
+
93
+ // 预览文件 URL
94
+ const previewFile = () => {
95
+ previewRef.value.preview('https://example.com/file.pdf', 'file.pdf');
96
+ };
97
+
98
+ // 预览 Blob 对象
99
+ const previewBlob = async () => {
100
+ const response = await fetch('https://example.com/file.xlsx');
101
+ const blob = await response.blob();
102
+ previewRef.value.preview(blob, 'file.xlsx');
103
+ };
104
+
105
+ // 预览 File 对象(上传的文件)
106
+ const handleFileUpload = (event) => {
107
+ const file = event.target.files[0];
108
+ if (file) {
109
+ previewRef.value.preview(file, file.name);
110
+ }
111
+ };
112
+
113
+ // 带选项的预览
114
+ const previewWithOptions = () => {
115
+ previewRef.value.preview('https://example.com/image.jpg', 'image.jpg', {
116
+ onProgress: (progress) => {
117
+ console.log('下载进度:', progress);
118
+ },
119
+ originalId: 'original-image-id'
120
+ });
121
+ };
122
+
123
+ // 清除预览
124
+ const clearPreview = () => {
125
+ previewRef.value.clearPreview();
126
+ };
127
+ </script>
128
+ ```
129
+
130
+ ### 方式二:使用单个预览组件
131
+
132
+ 如果需要更精细的控制,可以直接使用对应的预览组件。
133
+
134
+ ```vue
135
+ <template>
136
+ <!-- PDF 预览 -->
137
+ <PdfPreview
138
+ ref="pdfRef"
139
+ :pdf-url="pdfUrl"
140
+ :blocks-data="blocksData"
141
+ :show-toc-sidebar="true"
142
+ @pdf-loaded="handlePdfLoaded"
143
+ @page-jump="handlePageJump"
144
+ @position-jump="handlePositionJump"
145
+ />
146
+
147
+ <!-- 图片预览 -->
148
+ <ImagePreview
149
+ :url="imageUrl"
150
+ :min-scale="0.1"
151
+ :max-scale="5"
152
+ :original-id="originalId"
153
+ :is-download="true"
154
+ @original="handleOriginal"
155
+ @download="handleDownload"
156
+ />
157
+
158
+ <!-- Excel 预览 -->
159
+ <XlsxPreview
160
+ :url="excelUrl"
161
+ :is-download="true"
162
+ @download="handleDownload"
163
+ />
164
+
165
+ <!-- Word 预览 -->
166
+ <DocxPreview
167
+ :url="wordUrl"
168
+ :is-download="true"
169
+ @download="handleDownload"
170
+ />
171
+
172
+ <!-- OFD 预览 -->
173
+ <OfdPreview
174
+ :url="ofdUrl"
175
+ :is-download="true"
176
+ @download="handleDownload"
177
+ />
178
+
179
+ <!-- TIF 预览 -->
180
+ <TifPreview
181
+ :url="tifUrl"
182
+ :min-scale="0.1"
183
+ :max-scale="5"
184
+ :original-id="originalId"
185
+ :is-download="true"
186
+ @original="handleOriginal"
187
+ @download="handleDownload"
188
+ />
189
+ </template>
190
+
191
+ <script setup>
192
+ import {
193
+ PdfPreview,
194
+ ImagePreview,
195
+ XlsxPreview,
196
+ DocxPreview,
197
+ OfdPreview,
198
+ TifPreview
199
+ } from 'ocr-web-sdk';
200
+
201
+ const pdfUrl = ref('https://example.com/file.pdf');
202
+ const imageUrl = ref('https://example.com/image.jpg');
203
+ // ... 其他 URL
204
+
205
+ const handlePdfLoaded = () => {
206
+ console.log('PDF 加载完成');
207
+ };
208
+
209
+ const handlePageJump = (pageNum) => {
210
+ console.log('跳转到第', pageNum, '页');
211
+ };
212
+
213
+ const handlePositionJump = (pageNum, bbox, type) => {
214
+ const typeName = type === 'seal' ? '印章' : '文本块';
215
+ console.log(`跳转到${typeName}:`, pageNum, bbox);
216
+ };
217
+
218
+ const handleOriginal = () => {
219
+ console.log('查看原图');
220
+ };
221
+
222
+ const handleDownload = () => {
223
+ console.log('下载文件');
224
+ };
225
+ </script>
226
+ ```
227
+
228
+ ## 📚 API 文档
229
+
230
+ ### FilePreview 组件
231
+
232
+ 统一的文件预览入口组件,自动根据文件类型选择合适的预览组件。
233
+
234
+ #### Props
235
+
236
+ | 参数 | 类型 | 默认值 | 说明 |
237
+ |------|------|--------|------|
238
+ | `isDownload` | `boolean` | `false` | 是否显示下载按钮 |
239
+
240
+ #### Methods
241
+
242
+ ##### `preview(file, fileName?, options?)`
243
+
244
+ 预览文件方法。
245
+
246
+ **参数:**
247
+ - `file` (`string | Blob | File`) - 文件 URL、Blob 对象或 File 对象
248
+ - `fileName` (`string`, 可选) - 文件名,用于判断文件类型。如果不提供,会尝试从 URL 或 File 对象中获取
249
+ - `options` (`object`, 可选) - 配置选项
250
+ - `onProgress` (`Function`) - 下载进度回调函数,参数为进度百分比 `(progress: number) => void`
251
+ - `originalId` (`string`) - 原图 ID(用于图片预览)
252
+
253
+ **示例:**
254
+ ```javascript
255
+ // 预览 URL
256
+ previewRef.value.preview('https://example.com/file.pdf', 'file.pdf');
257
+
258
+ // 预览 Blob
259
+ previewRef.value.preview(blob, 'file.xlsx');
260
+
261
+ // 预览 File 对象
262
+ previewRef.value.preview(file, file.name);
263
+
264
+ // 带选项
265
+ previewRef.value.preview(url, 'file.pdf', {
266
+ onProgress: (progress) => {
267
+ console.log('下载进度:', progress);
268
+ },
269
+ originalId: 'original-image-id'
270
+ });
271
+ ```
272
+
273
+ ##### `clearPreview()`
274
+
275
+ 清除当前预览,释放资源。
276
+
277
+ **示例:**
278
+ ```javascript
279
+ previewRef.value.clearPreview();
280
+ ```
281
+
282
+ ---
283
+
284
+ ### PdfPreview 组件
285
+
286
+ PDF 文档预览组件,功能最丰富。
287
+
288
+ #### Props
289
+
290
+ | 参数 | 类型 | 默认值 | 说明 |
291
+ |------|------|--------|------|
292
+ | `pdfUrl` | `string` | - | PDF 文件 URL(必需) |
293
+ | `blocksData` | `Array<BlockInfo>` | - | PDF 分块数据(从后端 layout-parsing 接口获取),用于目录和印章列表 |
294
+ | `showTocSidebar` | `boolean` | `true` | 是否显示目录侧边栏(包括工具栏中的切换按钮) |
295
+ | `isDownload` | `boolean` | `false` | 是否显示下载按钮 |
296
+
297
+ **BlockInfo 类型定义:**
298
+ ```typescript
299
+ interface BlockInfo {
300
+ blockLabel: string; // 块标签
301
+ blockContent: string; // 块内容
302
+ blockBbox: [number, number, number, number]; // 块的边界框 [x1, y1, x2, y2]
303
+ blockPage: number; // 块所属的页码(从1开始)
304
+ }
305
+ ```
306
+
307
+ #### Events
308
+
309
+ | 事件名 | 参数 | 说明 |
310
+ |--------|------|------|
311
+ | `pdf-loaded` | - | PDF 加载并渲染完成时触发 |
312
+ | `page-jump` | `pageNum: number` | 页面跳转时触发,传递目标页码 |
313
+ | `position-jump` | `pageNum: number, bbox: [number, number, number, number], type: "block" \| "seal"` | 位置跳转时触发,传递页码、边界框和类型(自动检测,block 文本块 或 seal 印章) |
314
+ | `download` | - | 点击下载按钮时触发 |
315
+
316
+ #### Methods(通过 ref 调用)
317
+
318
+ | 方法名 | 参数 | 返回值 | 说明 |
319
+ |--------|------|--------|------|
320
+ | `goToPage` | `pageNum: number` | - | 跳转到指定页码(从1开始) |
321
+ | `jumpToPosition` | `pageNum: number, bbox: [number, number, number, number], emitEvent?: boolean` | - | 跳转到指定位置并高亮(统一接口,**自动识别类型**,无需传入类型参数) |
322
+ | `jumpToBlockPosition` | `pageNum: number, bbox: [number, number, number, number], emitEvent?: boolean` | - | 跳转到指定文本块位置并高亮(向后兼容的别名,内部调用统一接口) |
323
+ | `jumpToSealPosition` | `pageNum: number, bbox: [number, number, number, number], emitEvent?: boolean` | - | 跳转到指定印章位置并高亮(向后兼容的别名,内部调用统一接口) |
324
+ | `getCurrentPage` | - | `number` | 获取当前页码 |
325
+ | `getTotalPages` | - | `number` | 获取总页数 |
326
+ | `reset` | - | - | 重置预览状态 |
327
+ | `cleanup` | - | - | 清理资源 |
328
+
329
+ **示例:**
330
+ ```vue
331
+ <template>
332
+ <PdfPreview
333
+ ref="pdfRef"
334
+ :pdf-url="pdfUrl"
335
+ :blocks-data="blocksData"
336
+ :show-toc-sidebar="true"
337
+ @pdf-loaded="handlePdfLoaded"
338
+ @page-jump="handlePageJump"
339
+ @position-jump="handlePositionJump"
340
+ />
341
+
342
+ <div>
343
+ <button @click="jumpToPage(5)">跳转到第5页</button>
344
+ <button @click="jumpToBlock">跳转到文本块</button>
345
+ <button @click="jumpToSeal">跳转到印章</button>
346
+ </div>
347
+ </template>
348
+
349
+ <script setup>
350
+ import { ref } from 'vue';
351
+ import { PdfPreview } from 'ocr-web-sdk';
352
+
353
+ const pdfRef = ref(null);
354
+
355
+ const handlePdfLoaded = () => {
356
+ console.log('PDF 加载完成');
357
+ };
358
+
359
+ const handlePageJump = (pageNum) => {
360
+ console.log('跳转到第', pageNum, '页');
361
+ };
362
+
363
+ // 统一的位置跳转事件处理(类型会自动检测)
364
+ const handlePositionJump = (pageNum, bbox, type) => {
365
+ const typeName = type === 'seal' ? '印章' : '文本块';
366
+ console.log(`跳转到${typeName}:`, pageNum, bbox);
367
+ };
368
+
369
+ // 跳转到指定页面
370
+ const jumpToPage = (pageNum) => {
371
+ pdfRef.value?.goToPage(pageNum);
372
+ };
373
+
374
+ // 跳转到指定位置(自动识别是文本块还是印章)
375
+ const jumpToBlock = () => {
376
+ // bbox 格式: [x1, y1, x2, y2] - PDF 坐标系的边界框
377
+ // 无需指定类型,系统会根据坐标自动匹配
378
+ const pageNum = 3;
379
+ const bbox = [100, 200, 300, 400];
380
+ pdfRef.value?.jumpToPosition(pageNum, bbox);
381
+ };
382
+
383
+ // 跳转到印章位置(同样无需指定类型)
384
+ const jumpToSeal = () => {
385
+ const pageNum = 2;
386
+ const bbox = [150, 250, 250, 350];
387
+ // 系统会自动识别这是印章还是文本块
388
+ pdfRef.value?.jumpToPosition(pageNum, bbox);
389
+ };
390
+
391
+ // 获取当前页码
392
+ const getCurrentPage = () => {
393
+ const currentPage = pdfRef.value?.getCurrentPage();
394
+ console.log('当前页码:', currentPage);
395
+ };
396
+
397
+ // 获取总页数
398
+ const getTotalPages = () => {
399
+ const totalPages = pdfRef.value?.getTotalPages();
400
+ console.log('总页数:', totalPages);
401
+ };
402
+ </script>
403
+ ```
404
+
405
+ ---
406
+
407
+ ### ImagePreview 组件
408
+
409
+ 图片预览组件,支持缩放、旋转、拖拽等功能。
410
+
411
+ #### Props
412
+
413
+ | 参数 | 类型 | 默认值 | 说明 |
414
+ |------|------|--------|------|
415
+ | `url` | `string` | - | 图片 URL(必需) |
416
+ | `minScale` | `number` | `0.1` | 最小缩放比例 |
417
+ | `maxScale` | `number` | `5` | 最大缩放比例 |
418
+ | `clickStep` | `number` | `0.25` | 点击缩放按钮的步长 |
419
+ | `wheelStep` | `number` | `0.1` | 滚轮缩放的步长 |
420
+ | `originalId` | `string` | `''` | 原图 ID,用于查看原图功能 |
421
+ | `isDownload` | `boolean` | `false` | 是否显示下载按钮 |
422
+
423
+ #### Events
424
+
425
+ | 事件名 | 参数 | 说明 |
426
+ |--------|------|------|
427
+ | `original` | - | 点击"查看原图"按钮时触发 |
428
+ | `download` | - | 点击"下载"按钮时触发 |
429
+
430
+ #### Methods(通过 ref 调用)
431
+
432
+ | 方法名 | 参数 | 返回值 | 说明 |
433
+ |--------|------|--------|------|
434
+ | `reset` | - | - | 重置图片状态(缩放、旋转、位置) |
435
+
436
+ **示例:**
437
+ ```vue
438
+ <ImagePreview
439
+ ref="imageRef"
440
+ :url="imageUrl"
441
+ :min-scale="0.1"
442
+ :max-scale="5"
443
+ :original-id="originalId"
444
+ :is-download="true"
445
+ @original="handleOriginal"
446
+ @download="handleDownload"
447
+ />
448
+ ```
449
+
450
+ ---
451
+
452
+ ### XlsxPreview 组件
453
+
454
+ Excel 文件预览组件。
455
+
456
+ #### Props
457
+
458
+ | 参数 | 类型 | 默认值 | 说明 |
459
+ |------|------|--------|------|
460
+ | `url` | `string` | - | Excel 文件 URL(必需) |
461
+ | `isDownload` | `boolean` | `false` | 是否显示下载按钮 |
462
+
463
+ #### Events
464
+
465
+ | 事件名 | 参数 | 说明 |
466
+ |--------|------|------|
467
+ | `download` | - | 点击"下载"按钮时触发 |
468
+
469
+ #### Methods(通过 ref 调用)
470
+
471
+ | 方法名 | 参数 | 返回值 | 说明 |
472
+ |--------|------|--------|------|
473
+ | `cleanup` | - | - | 清理资源 |
474
+
475
+ **示例:**
476
+ ```vue
477
+ <XlsxPreview
478
+ ref="excelRef"
479
+ :url="excelUrl"
480
+ :is-download="true"
481
+ @download="handleDownload"
482
+ />
483
+ ```
484
+
485
+ ---
486
+
487
+ ### DocxPreview 组件
488
+
489
+ Word 文档预览组件。
490
+
491
+ #### Props
492
+
493
+ | 参数 | 类型 | 默认值 | 说明 |
494
+ |------|------|--------|------|
495
+ | `url` | `string` | - | Word 文档 URL(必需) |
496
+ | `docName` | `string` | `'文档预览'` | 文档名称 |
497
+ | `docData` | `Blob \| ArrayBuffer` | `null` | 直接传入文档数据(优先级高于 url) |
498
+ | `minScale` | `number` | `0.1` | 最小缩放比例 |
499
+ | `maxScale` | `number` | `3` | 最大缩放比例 |
500
+ | `clickStep` | `number` | `0.25` | 点击缩放按钮的步长 |
501
+ | `wheelStep` | `number` | `0.1` | 滚轮缩放的步长 |
502
+ | `isDownload` | `boolean` | `false` | 是否显示下载按钮 |
503
+
504
+ #### Events
505
+
506
+ | 事件名 | 参数 | 说明 |
507
+ |--------|------|------|
508
+ | `download` | - | 点击"下载"按钮时触发 |
509
+
510
+ #### Methods(通过 ref 调用)
511
+
512
+ | 方法名 | 参数 | 返回值 | 说明 |
513
+ |--------|------|--------|------|
514
+ | `reset` | - | - | 重置文档状态 |
515
+ | `zoom` | `delta: number, isWheel?: boolean` | - | 缩放文档 |
516
+ | `rotate` | `delta: number` | - | 旋转文档 |
517
+
518
+ **示例:**
519
+ ```vue
520
+ <DocxPreview
521
+ ref="wordRef"
522
+ :url="wordUrl"
523
+ :is-download="true"
524
+ @download="handleDownload"
525
+ />
526
+ ```
527
+
528
+ ---
529
+
530
+ ### OfdPreview 组件
531
+
532
+ OFD 文件预览组件。
533
+
534
+ #### Props
535
+
536
+ | 参数 | 类型 | 默认值 | 说明 |
537
+ |------|------|--------|------|
538
+ | `url` | `string` | - | OFD 文件 URL(必需) |
539
+ | `isDownload` | `boolean` | `false` | 是否显示下载按钮 |
540
+
541
+ #### Events
542
+
543
+ | 事件名 | 参数 | 说明 |
544
+ |--------|------|------|
545
+ | `download` | - | 点击"下载"按钮时触发 |
546
+
547
+ #### Methods(通过 ref 调用)
548
+
549
+ | 方法名 | 参数 | 返回值 | 说明 |
550
+ |--------|------|--------|------|
551
+ | `cleanup` | - | - | 清理资源 |
552
+
553
+ **示例:**
554
+ ```vue
555
+ <OfdPreview
556
+ ref="ofdRef"
557
+ :url="ofdUrl"
558
+ :is-download="true"
559
+ @download="handleDownload"
560
+ />
561
+ ```
562
+
563
+ ---
564
+
565
+ ### TifPreview 组件
566
+
567
+ TIF/TIFF 图片预览组件,支持多页 TIF 文件。
568
+
569
+ #### Props
570
+
571
+ | 参数 | 类型 | 默认值 | 说明 |
572
+ |------|------|--------|------|
573
+ | `url` | `string` | - | TIF 文件 URL(必需) |
574
+ | `minScale` | `number` | `0.1` | 最小缩放比例 |
575
+ | `maxScale` | `number` | `5` | 最大缩放比例 |
576
+ | `clickStep` | `number` | `0.25` | 点击缩放按钮的步长 |
577
+ | `wheelStep` | `number` | `0.1` | 滚轮缩放的步长 |
578
+ | `originalId` | `string` | `''` | 原图 ID,用于查看原图功能 |
579
+ | `isDownload` | `boolean` | `false` | 是否显示下载按钮 |
580
+
581
+ #### Events
582
+
583
+ | 事件名 | 参数 | 说明 |
584
+ |--------|------|------|
585
+ | `original` | - | 点击"查看原图"按钮时触发 |
586
+ | `download` | - | 点击"下载"按钮时触发 |
587
+ | `load` | - | TIF 文件加载完成时触发 |
588
+ | `error` | `error: Error` | TIF 文件加载失败时触发 |
589
+
590
+ #### Methods(通过 ref 调用)
591
+
592
+ | 方法名 | 参数 | 返回值 | 说明 |
593
+ |--------|------|--------|------|
594
+ | `cleanup` | - | - | 清理资源 |
595
+
596
+ **示例:**
597
+ ```vue
598
+ <TifPreview
599
+ ref="tifRef"
600
+ :url="tifUrl"
601
+ :min-scale="0.1"
602
+ :max-scale="5"
603
+ :original-id="originalId"
604
+ :is-download="true"
605
+ @original="handleOriginal"
606
+ @download="handleDownload"
607
+ @load="handleLoad"
608
+ @error="handleError"
609
+ />
610
+ ```
611
+
612
+ ## 📋 支持的文件格式
613
+
614
+ | 格式 | 扩展名 | 预览组件 |
615
+ |------|--------|----------|
616
+ | **PDF** | `.pdf` | `PdfPreview` |
617
+ | **Excel** | `.xlsx` | `XlsxPreview` |
618
+ | **Word** | `.docx` | `DocxPreview` |
619
+ | **图片** | `.jpg`, `.jpeg`, `.png`, `.gif`, `.bmp`, `.webp`, `.svg`, `.apng`, `.avif` | `ImagePreview` |
620
+ | **OFD** | `.ofd` | `OfdPreview` |
621
+ | **TIF** | `.tif`, `.tiff` | `TifPreview` |
622
+
623
+ ## 🛠️ 开发
624
+
625
+ ### 本地开发
626
+
627
+ ```bash
628
+ # 克隆项目
629
+ git clone <repository-url>
630
+
631
+ # 安装依赖
632
+ npm install
633
+
634
+ # 启动开发服务器
635
+ npm run dev
636
+
637
+ # 构建生产版本
638
+ npm run build
639
+
640
+ # 预览构建结果
641
+ npm run preview
642
+ ```
643
+
644
+ ### 项目结构
645
+
646
+ ```
647
+ ocr-web-sdk/
648
+ ├── Preview/ # 预览组件目录
649
+ │ ├── index.vue # FilePreview 主组件
650
+ │ ├── PdfPreview.vue # PDF 预览组件
651
+ │ ├── ImagePreview.vue # 图片预览组件
652
+ │ ├── xlsxPreview.vue # Excel 预览组件
653
+ │ ├── docxPreview.vue # Word 预览组件
654
+ │ ├── ofdPreview.vue # OFD 预览组件
655
+ │ └── tifPreview.vue # TIF 预览组件
656
+ ├── src/ # 源码目录
657
+ │ ├── index.ts # 入口文件
658
+ │ └── styles/ # 样式文件
659
+ ├── demo/ # 示例项目
660
+ └── package.json # 项目配置
661
+ ```
662
+
663
+ ## 💡 使用技巧
664
+
665
+ ### 1. 文件类型判断
666
+
667
+ `FilePreview` 组件会根据文件扩展名自动判断文件类型。如果文件名没有扩展名,可以手动指定:
668
+
669
+ ```javascript
670
+ // 手动指定文件类型
671
+ previewRef.value.preview(blob, 'document.pdf');
672
+ ```
673
+
674
+ ### 2. 预览 Blob 对象
675
+
676
+ 当从 API 获取文件数据时,可以转换为 Blob 对象进行预览:
677
+
678
+ ```javascript
679
+ const response = await fetch('https://api.example.com/file');
680
+ const blob = await response.blob();
681
+ previewRef.value.preview(blob, 'file.pdf');
682
+ ```
683
+
684
+ ### 3. 处理文件上传
685
+
686
+ 结合文件上传组件使用:
687
+
688
+ ```vue
689
+ <template>
690
+ <input type="file" @change="handleFileChange" />
691
+ <FilePreview ref="previewRef" />
692
+ </template>
693
+
694
+ <script setup>
695
+ const handleFileChange = (event) => {
696
+ const file = event.target.files[0];
697
+ if (file) {
698
+ previewRef.value.preview(file, file.name);
699
+ }
700
+ };
701
+ </script>
702
+ ```
703
+
704
+ ### 4. PDF 分块数据使用
705
+
706
+ 如果后端提供了 PDF 分块数据(blocksData),可以传递给 `PdfPreview` 组件以启用目录和印章列表功能:
707
+
708
+ ```vue
709
+ <PdfPreview
710
+ :pdf-url="pdfUrl"
711
+ :blocks-data="blocksData"
712
+ />
713
+ ```
714
+
715
+ ### 5. PDF 位置跳转
716
+
717
+ 通过坐标自动匹配文本块或印章,无需指定类型:
718
+
719
+ ```javascript
720
+ // 自动识别类型(推荐)
721
+ pdfRef.value?.jumpToPosition(pageNum, bbox);
722
+
723
+ // 向后兼容的别名方法
724
+ pdfRef.value?.jumpToBlockPosition(pageNum, bbox);
725
+ pdfRef.value?.jumpToSealPosition(pageNum, bbox);
726
+ ```
727
+
728
+ ### 6. 资源清理
729
+
730
+ 组件会在卸载时自动清理资源,但也可以手动调用清理方法:
731
+
732
+ ```javascript
733
+ // 切换文件时,先清除旧预览
734
+ previewRef.value.clearPreview();
735
+ // 然后加载新文件
736
+ previewRef.value.preview(newFileUrl, 'new-file.pdf');
737
+ ```
738
+
739
+ ## ⚠️ 注意事项
740
+
741
+ 1. **Vue 版本要求**:本项目需要 Vue 3.x,不支持 Vue 2.x
742
+ 2. **UI 组件库**:需要安装 `@arco-design/web-vue` 作为依赖
743
+ 3. **文件大小**:大文件预览可能会影响性能,建议对文件大小进行限制
744
+ 4. **跨域问题**:预览跨域文件时,需要确保服务器配置了正确的 CORS 头
745
+ 5. **PDF 渲染**:PDF 预览使用 pdfjs-dist,首次加载可能需要下载 worker 文件
746
+ 6. **TIF 格式**:TIF 预览支持多页,但需要浏览器支持相应的解码器
747
+
748
+ ## 📄 许可证
749
+
750
+ MIT
751
+
752
+ ## 🤝 贡献
753
+
754
+ 欢迎提交 Issue 和 Pull Request!