@ebiz/designer-components 0.1.124 → 0.1.126
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 +29 -29
- package/dist/designer-components.css +1 -1
- package/dist/index.mjs +35548 -33511
- package/dist/{pdf-C9oIcL2N.js → pdf-KomaE0u5.js} +891 -891
- package/package.json +1 -1
- package/src/App.vue +26 -26
- package/src/apiService/SIMPLE_DATA_SERVICE.md +284 -284
- package/src/apiService/mockDataService.js +115 -115
- package/src/apiService/simpleDataService.js +312 -312
- package/src/assets/base.css +86 -86
- package/src/assets/logo.svg +1 -1
- package/src/components/Button.vue +149 -149
- package/src/components/DataContainer.vue +40 -40
- package/src/components/EbizApproval.vue +366 -366
- package/src/components/EbizApprovalDetail.vue +169 -169
- package/src/components/EbizApprovalForm.vue +534 -534
- package/src/components/EbizApprovalV2.vue +713 -713
- package/src/components/EbizAutoForm.vue +596 -596
- package/src/components/EbizAvatar.vue +115 -115
- package/src/components/EbizCheckbox.vue +93 -93
- package/src/components/EbizCheckboxGroup.vue +69 -69
- package/src/components/EbizDepartmentSelector.vue +148 -148
- package/src/components/EbizDescriptions.vue +340 -340
- package/src/components/EbizDescriptionsItem.vue +47 -47
- package/src/components/EbizDetailBlock.vue +81 -81
- package/src/components/EbizDetailItem.vue +559 -559
- package/src/components/EbizDetailView.md +438 -438
- package/src/components/EbizDetailView.vue +355 -355
- package/src/components/EbizDialog.vue +260 -260
- package/src/components/EbizDictionarySelect.vue +229 -229
- package/src/components/EbizDiv.vue +40 -40
- package/src/components/EbizDivider.vue +96 -96
- package/src/components/EbizDormDashboard.vue +314 -314
- package/src/components/EbizDropdown.vue +135 -135
- package/src/components/EbizDropdownItem.vue +85 -85
- package/src/components/EbizEmployeeInfo.vue +144 -144
- package/src/components/EbizEmployeeSelector.vue +1160 -1160
- package/src/components/EbizFileList.vue +502 -502
- package/src/components/EbizMap.vue +541 -541
- package/src/components/EbizMeetingRoomSelector.vue +1149 -1149
- package/src/components/EbizMobileMeetingRoomSelector.vue +727 -727
- package/src/components/EbizOkrTree.vue +99 -99
- package/src/components/EbizPageHeader.vue +98 -98
- package/src/components/EbizPagination.vue +162 -162
- package/src/components/EbizParkingDashboard.vue +152 -152
- package/src/components/EbizPdfViewer.vue +540 -540
- package/src/components/EbizPopconfirm.vue +47 -47
- package/src/components/EbizQrCode.vue +167 -167
- package/src/components/EbizRadio.vue +86 -86
- package/src/components/EbizRadioGroup.vue +83 -83
- package/src/components/EbizRemoteSelect.vue +232 -232
- package/src/components/EbizRichTextEditor.vue +338 -338
- package/src/components/EbizRouteBreadcrumb.vue +46 -46
- package/src/components/EbizSApprovalProcess.vue +1456 -1426
- package/src/components/EbizSelect.vue +85 -85
- package/src/components/EbizSpace.vue +100 -100
- package/src/components/EbizStatistic.vue +149 -149
- package/src/components/EbizStatsCard.vue +113 -113
- package/src/components/EbizSwiper.vue +113 -113
- package/src/components/EbizSwiperItem.vue +13 -13
- package/src/components/EbizSwitch.vue +85 -85
- package/src/components/EbizTabHeader.vue +132 -132
- package/src/components/EbizTabPanel.vue +22 -22
- package/src/components/EbizTable.vue +469 -469
- package/src/components/EbizTableColumn.vue +116 -116
- package/src/components/EbizTableSort.vue +179 -179
- package/src/components/EbizTabs.vue +142 -142
- package/src/components/EbizTdesignButtonDialog.vue +332 -332
- package/src/components/EbizTdesignLoading.vue +107 -107
- package/src/components/EbizTimePicker.vue +143 -143
- package/src/components/EbizTitle.vue +91 -91
- package/src/components/EbizTree.vue +141 -141
- package/src/components/EbizTreeMergeTable.vue +1494 -1494
- package/src/components/EbizTreeSelector.vue +469 -469
- package/src/components/EbizVideo.vue +553 -553
- package/src/components/EbizVxeTable.vue +290 -290
- package/src/components/Form.vue +28 -28
- package/src/components/Home.vue +7 -7
- package/src/components/LaunchInterview.vue +526 -526
- package/src/components/MyComponent.vue +39 -39
- package/src/components/Table.vue +45 -45
- package/src/components/TdesignAlert.vue +115 -115
- package/src/components/TdesignButton.vue +135 -135
- package/src/components/TdesignCalendar/index.vue +145 -145
- package/src/components/TdesignCard.vue +195 -195
- package/src/components/TdesignCol.vue +101 -101
- package/src/components/TdesignCollapse.vue +142 -142
- package/src/components/TdesignCollapsePanel.vue +79 -79
- package/src/components/TdesignDatePicker.vue +124 -124
- package/src/components/TdesignDescriptions.vue +74 -74
- package/src/components/TdesignDescriptionsItem.vue +50 -50
- package/src/components/TdesignDialog.vue +225 -225
- package/src/components/TdesignForm.vue +138 -138
- package/src/components/TdesignFormItem.vue +105 -105
- package/src/components/TdesignGrid.vue +55 -55
- package/src/components/TdesignIcon.vue +67 -67
- package/src/components/TdesignImage.vue +162 -162
- package/src/components/TdesignImageViewer.vue +200 -200
- package/src/components/TdesignInput.vue +242 -242
- package/src/components/TdesignSelect.vue +446 -446
- package/src/components/TdesignTag.vue +117 -117
- package/src/components/TdesignTextarea.vue +142 -142
- package/src/components/TdesignTimeline.vue +58 -58
- package/src/components/TdesignTimelineItem.vue +71 -71
- package/src/components/TdesignUpload.vue +414 -414
- package/src/components/TdesignWatermark.vue +107 -107
- package/src/components/ebiz-form/components/cascader.vue +61 -61
- package/src/components/ebiz-form/components/checkbox.vue +37 -37
- package/src/components/ebiz-form/components/city.vue +137 -137
- package/src/components/ebiz-form/components/date-panel.vue +52 -52
- package/src/components/ebiz-form/components/date-range-panel.vue +52 -52
- package/src/components/ebiz-form/components/date-range.vue +56 -56
- package/src/components/ebiz-form/components/date.vue +52 -52
- package/src/components/ebiz-form/components/editor-multi-language.vue +47 -47
- package/src/components/ebiz-form/components/editor.vue +78 -78
- package/src/components/ebiz-form/components/file-multi-language.vue +52 -52
- package/src/components/ebiz-form/components/file.vue +149 -149
- package/src/components/ebiz-form/components/images-multi-language.vue +52 -52
- package/src/components/ebiz-form/components/images.vue +129 -129
- package/src/components/ebiz-form/components/img-multi-language.vue +51 -51
- package/src/components/ebiz-form/components/img.vue +129 -129
- package/src/components/ebiz-form/components/number.vue +50 -50
- package/src/components/ebiz-form/components/radio.vue +28 -28
- package/src/components/ebiz-form/components/select.vue +119 -119
- package/src/components/ebiz-form/components/switch.vue +23 -23
- package/src/components/ebiz-form/components/text-multi-language.vue +47 -47
- package/src/components/ebiz-form/components/text.vue +52 -52
- package/src/components/ebiz-form/components/textarea-multi-language.vue +48 -48
- package/src/components/ebiz-form/components/textarea.vue +29 -29
- package/src/components/ebiz-form/components/video-multi-language.vue +51 -51
- package/src/components/ebiz-form/components/video.vue +97 -97
- package/src/components/ebiz-form/index.vue +157 -157
- package/src/components/examples/PopconfirmExample.vue +149 -149
- package/src/components/icons/IconCommunity.vue +7 -7
- package/src/components/icons/IconDocumentation.vue +7 -7
- package/src/components/icons/IconEcosystem.vue +7 -7
- package/src/components/icons/IconSupport.vue +7 -7
- package/src/components/icons/IconTooling.vue +19 -19
- package/src/components/mItems/UserInfo.vue +349 -349
- package/src/components/senior/EbizApprovalList/ApprovalList.vue +127 -127
- package/src/components/senior/EbizSData/index.vue +280 -280
- package/src/components/senior/EbizSDialog/index.vue +776 -776
- package/src/components/senior/EbizSForm/README.md +157 -157
- package/src/components/senior/EbizSForm/index.vue +667 -667
- package/src/components/senior/EbizSForm/item.vue +1011 -1011
- package/src/components/senior/EbizSForm/mItems/DateTimePicker.vue +51 -51
- package/src/components/senior/EbizSForm/mItems/Picker.vue +63 -63
- package/src/index.js +327 -327
- package/src/main.js +55 -55
- package/src/router/index.js +436 -436
- package/src/utils/formatCode.js +24 -24
- package/src/utils/generateImportStatement.js +52 -52
- package/src/utils/hasJsx.js +25 -25
- package/src/utils/index.js +166 -166
- package/src/utils/mergeOptions.js +29 -29
- package/src/utils/parseRequiredBlocks.js +18 -18
- package/src/utils/vue-sfc-validator.js +155 -155
- package/src/views/Button.vue +23 -23
- package/src/views/CheckboxDemo.vue +104 -104
- package/src/views/DataContainer.vue +19 -19
- package/src/views/DialogDemo.vue +125 -125
- package/src/views/EbizApprovalDemo.vue +87 -87
- package/src/views/EbizApprovalFormDemo.vue +207 -207
- package/src/views/EbizAutoFormDemo.vue +129 -129
- package/src/views/EbizAvatar.vue +223 -223
- package/src/views/EbizDepartmentSelectorDemo.vue +169 -169
- package/src/views/EbizDetailBlockDemo.vue +30 -30
- package/src/views/EbizDetailViewDemo.vue +412 -412
- package/src/views/EbizDormDashboardDemo.vue +87 -87
- package/src/views/EbizEmployeeInfo.vue +249 -249
- package/src/views/EbizEmployeeSelector.vue +85 -85
- package/src/views/EbizFileListDemo.vue +339 -339
- package/src/views/EbizMap.vue +201 -201
- package/src/views/EbizMeetingRoomSelectorDemo.vue +293 -293
- package/src/views/EbizMobileMeetingRoomSelectorDemo.vue +566 -566
- package/src/views/EbizParkingDashboardDemo.vue +28 -28
- package/src/views/EbizRadioDemo.vue +151 -151
- package/src/views/EbizSDataDemo.vue +136 -136
- package/src/views/EbizSDialogDemo.vue +303 -303
- package/src/views/EbizSForm/index.vue +352 -352
- package/src/views/EbizSFormDemo.vue +420 -420
- package/src/views/EbizSpace.vue +185 -185
- package/src/views/EbizSwiper.vue +157 -157
- package/src/views/EbizTdesignButtonDialogExample.vue +437 -437
- package/src/views/Form.vue +19 -19
- package/src/views/GridDemo.vue +238 -238
- package/src/views/Home.vue +156 -156
- package/src/views/LaunchInterviewDemo.vue +111 -111
- package/src/views/Mindmap.vue +17 -17
- package/src/views/MyComponent.vue +19 -19
- package/src/views/OkrTree.vue +19 -19
- package/src/views/PageHeaderDemo.vue +104 -104
- package/src/views/PaginationDemo.vue +96 -96
- package/src/views/PdfViewerDemo.vue +433 -433
- package/src/views/PermissionBoxDemo.vue +85 -85
- package/src/views/PopconfirmDemo.vue +80 -80
- package/src/views/RemoteSelect.vue +350 -350
- package/src/views/StatisticDemo.vue +190 -190
- package/src/views/SwitchDemo.vue +79 -79
- package/src/views/Table.vue +19 -19
- package/src/views/TableDemo.vue +334 -334
- package/src/views/TableSortDemo.vue +143 -143
- package/src/views/TableView.vue +68 -68
- package/src/views/TabsDemo.vue +282 -282
- package/src/views/TagDemo.vue +101 -101
- package/src/views/TdesignAlert.vue +98 -98
- package/src/views/TdesignButton.vue +190 -190
- package/src/views/TdesignCalendar.vue +94 -94
- package/src/views/TdesignCard.vue +296 -296
- package/src/views/TdesignCollapse.vue +293 -293
- package/src/views/TdesignDatePicker.vue +187 -187
- package/src/views/TdesignDescriptions.vue +101 -101
- package/src/views/TdesignForm.vue +248 -248
- package/src/views/TdesignIcon.vue +203 -203
- package/src/views/TdesignImage.vue +215 -215
- package/src/views/TdesignImageViewer.vue +198 -198
- package/src/views/TdesignInput.vue +252 -252
- package/src/views/TdesignSelect.vue +473 -473
- package/src/views/TdesignSwiper.vue +157 -157
- package/src/views/TextareaDemo.vue +93 -93
- package/src/views/TimePickerDemo.vue +146 -146
- package/src/views/TimelineDemo.vue +160 -160
- package/src/views/Title.vue +19 -19
- package/src/views/TreeDemo.vue +254 -254
- package/src/views/TreeMergeTableDemo.vue +239 -239
- package/src/views/TreeSelectorDemo.vue +245 -245
- package/src/views/UploadDemo.vue +128 -128
- package/src/views/VideoDemo.vue +245 -245
- package/src/views/VxeTableDemo.vue +279 -279
- package/src/views/WatermarkDemo.vue +85 -85
|
@@ -1,541 +1,541 @@
|
|
|
1
|
-
<template>
|
|
2
|
-
<div class="ebiz-pdf-viewer" :style="{ width: width, height: height }">
|
|
3
|
-
<!-- 工具栏 -->
|
|
4
|
-
<div v-if="showToolbar" class="pdf-toolbar">
|
|
5
|
-
<div class="toolbar-left">
|
|
6
|
-
<button
|
|
7
|
-
@click="previousPage"
|
|
8
|
-
:disabled="currentPage <= 1"
|
|
9
|
-
class="toolbar-btn"
|
|
10
|
-
title="上一页"
|
|
11
|
-
>
|
|
12
|
-
⬅
|
|
13
|
-
</button>
|
|
14
|
-
<span class="page-info">
|
|
15
|
-
{{ currentPage }} / {{ totalPages }}
|
|
16
|
-
</span>
|
|
17
|
-
<button
|
|
18
|
-
@click="nextPage"
|
|
19
|
-
:disabled="currentPage >= totalPages"
|
|
20
|
-
class="toolbar-btn"
|
|
21
|
-
title="下一页"
|
|
22
|
-
>
|
|
23
|
-
➡
|
|
24
|
-
</button>
|
|
25
|
-
</div>
|
|
26
|
-
<div class="toolbar-center">
|
|
27
|
-
<button @click="zoomOut" class="toolbar-btn" title="缩小">-</button>
|
|
28
|
-
<span class="zoom-info">{{ Math.round(scale * 100) }}%</span>
|
|
29
|
-
<button @click="zoomIn" class="toolbar-btn" title="放大">+</button>
|
|
30
|
-
<button @click="resetZoom" class="toolbar-btn" title="重置缩放">⟲</button>
|
|
31
|
-
</div>
|
|
32
|
-
<div class="toolbar-right">
|
|
33
|
-
<button @click="rotatePage" class="toolbar-btn" title="旋转">↻</button>
|
|
34
|
-
<button @click="downloadPdf" class="toolbar-btn" title="下载">⬇</button>
|
|
35
|
-
</div>
|
|
36
|
-
</div>
|
|
37
|
-
|
|
38
|
-
<!-- PDF 内容区域 -->
|
|
39
|
-
<div class="pdf-container" ref="containerRef">
|
|
40
|
-
<div v-if="loading" class="pdf-loading">
|
|
41
|
-
<div class="loading-spinner"></div>
|
|
42
|
-
<span>加载中...</span>
|
|
43
|
-
</div>
|
|
44
|
-
<div v-else-if="error" class="pdf-error">
|
|
45
|
-
<span>{{ error }}</span>
|
|
46
|
-
</div>
|
|
47
|
-
<div v-else class="pdf-content">
|
|
48
|
-
<canvas
|
|
49
|
-
ref="canvasRef"
|
|
50
|
-
:style="{
|
|
51
|
-
transform: `rotate(${rotation}deg)`,
|
|
52
|
-
transformOrigin: 'center center'
|
|
53
|
-
}"
|
|
54
|
-
></canvas>
|
|
55
|
-
</div>
|
|
56
|
-
</div>
|
|
57
|
-
</div>
|
|
58
|
-
</template>
|
|
59
|
-
|
|
60
|
-
<script>
|
|
61
|
-
export default {
|
|
62
|
-
name: "EbizPdfViewer"
|
|
63
|
-
}
|
|
64
|
-
</script>
|
|
65
|
-
|
|
66
|
-
<script setup>
|
|
67
|
-
import { ref, onMounted, onUnmounted, watch, nextTick } from 'vue';
|
|
68
|
-
|
|
69
|
-
const props = defineProps({
|
|
70
|
-
// PDF文件URL或base64数据
|
|
71
|
-
src: {
|
|
72
|
-
type: String,
|
|
73
|
-
required: true
|
|
74
|
-
},
|
|
75
|
-
// 组件宽度
|
|
76
|
-
width: {
|
|
77
|
-
type: String,
|
|
78
|
-
default: '100%'
|
|
79
|
-
},
|
|
80
|
-
// 组件高度
|
|
81
|
-
height: {
|
|
82
|
-
type: String,
|
|
83
|
-
default: '600px'
|
|
84
|
-
},
|
|
85
|
-
// 是否显示工具栏
|
|
86
|
-
showToolbar: {
|
|
87
|
-
type: Boolean,
|
|
88
|
-
default: true
|
|
89
|
-
},
|
|
90
|
-
// 初始页码
|
|
91
|
-
page: {
|
|
92
|
-
type: Number,
|
|
93
|
-
default: 1
|
|
94
|
-
},
|
|
95
|
-
// 初始缩放比例
|
|
96
|
-
zoom: {
|
|
97
|
-
type: Number,
|
|
98
|
-
default: 1
|
|
99
|
-
},
|
|
100
|
-
// 是否允许下载
|
|
101
|
-
downloadable: {
|
|
102
|
-
type: Boolean,
|
|
103
|
-
default: true
|
|
104
|
-
},
|
|
105
|
-
// PDF密码
|
|
106
|
-
password: {
|
|
107
|
-
type: String,
|
|
108
|
-
default: ''
|
|
109
|
-
}
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
const emit = defineEmits([
|
|
113
|
-
'load',
|
|
114
|
-
'error',
|
|
115
|
-
'page-change',
|
|
116
|
-
'zoom-change',
|
|
117
|
-
'download'
|
|
118
|
-
]);
|
|
119
|
-
|
|
120
|
-
// 响应式状态
|
|
121
|
-
const loading = ref(false);
|
|
122
|
-
const error = ref('');
|
|
123
|
-
const currentPage = ref(props.page);
|
|
124
|
-
const totalPages = ref(0);
|
|
125
|
-
const scale = ref(props.zoom);
|
|
126
|
-
const rotation = ref(0);
|
|
127
|
-
|
|
128
|
-
// DOM引用
|
|
129
|
-
const containerRef = ref(null);
|
|
130
|
-
const canvasRef = ref(null);
|
|
131
|
-
|
|
132
|
-
// PDF相关变量
|
|
133
|
-
let pdfDoc = null;
|
|
134
|
-
let pdfLib = null;
|
|
135
|
-
let renderRetryCount = 0;
|
|
136
|
-
const MAX_RETRY_COUNT = 3;
|
|
137
|
-
let resizeObserver = null;
|
|
138
|
-
|
|
139
|
-
// 初始化PDF.js
|
|
140
|
-
const initPdfJs = async () => {
|
|
141
|
-
try {
|
|
142
|
-
// 动态导入pdfjs-dist
|
|
143
|
-
pdfLib = await import('pdfjs-dist');
|
|
144
|
-
|
|
145
|
-
// 设置worker路径
|
|
146
|
-
pdfLib.GlobalWorkerOptions.workerSrc = `https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.11.174/pdf.worker.min.js`;
|
|
147
|
-
|
|
148
|
-
return true;
|
|
149
|
-
} catch (err) {
|
|
150
|
-
console.error('Failed to initialize PDF.js:', err);
|
|
151
|
-
error.value = '初始化PDF.js失败';
|
|
152
|
-
return false;
|
|
153
|
-
}
|
|
154
|
-
};
|
|
155
|
-
|
|
156
|
-
// 加载PDF文档
|
|
157
|
-
const loadPdf = async () => {
|
|
158
|
-
if (!props.src) return;
|
|
159
|
-
|
|
160
|
-
loading.value = true;
|
|
161
|
-
error.value = '';
|
|
162
|
-
|
|
163
|
-
try {
|
|
164
|
-
if (!pdfLib) {
|
|
165
|
-
const success = await initPdfJs();
|
|
166
|
-
if (!success) return;
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
const loadingTask = pdfLib.getDocument({
|
|
170
|
-
url: props.src,
|
|
171
|
-
password: props.password
|
|
172
|
-
});
|
|
173
|
-
|
|
174
|
-
pdfDoc = await loadingTask.promise;
|
|
175
|
-
totalPages.value = pdfDoc.numPages;
|
|
176
|
-
|
|
177
|
-
// 确保当前页在有效范围内
|
|
178
|
-
if (currentPage.value > totalPages.value) {
|
|
179
|
-
currentPage.value = totalPages.value;
|
|
180
|
-
}
|
|
181
|
-
if (currentPage.value < 1) {
|
|
182
|
-
currentPage.value = 1;
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
// 延迟渲染确保DOM完全准备
|
|
186
|
-
await nextTick();
|
|
187
|
-
setTimeout(async () => {
|
|
188
|
-
await renderPage();
|
|
189
|
-
}, 50);
|
|
190
|
-
|
|
191
|
-
emit('load', {
|
|
192
|
-
totalPages: totalPages.value,
|
|
193
|
-
currentPage: currentPage.value
|
|
194
|
-
});
|
|
195
|
-
|
|
196
|
-
} catch (err) {
|
|
197
|
-
console.error('PDF loading error:', err);
|
|
198
|
-
error.value = err.message || 'PDF加载失败';
|
|
199
|
-
emit('error', err);
|
|
200
|
-
} finally {
|
|
201
|
-
loading.value = false;
|
|
202
|
-
}
|
|
203
|
-
};
|
|
204
|
-
|
|
205
|
-
// 渲染当前页面
|
|
206
|
-
const renderPage = async () => {
|
|
207
|
-
if (!pdfDoc || !canvasRef.value) return;
|
|
208
|
-
|
|
209
|
-
try {
|
|
210
|
-
// 确保canvas元素已经完全渲染到DOM中
|
|
211
|
-
await nextTick();
|
|
212
|
-
|
|
213
|
-
const page = await pdfDoc.getPage(currentPage.value);
|
|
214
|
-
const canvas = canvasRef.value;
|
|
215
|
-
|
|
216
|
-
// 再次检查canvas是否存在
|
|
217
|
-
if (!canvas) {
|
|
218
|
-
if (renderRetryCount < MAX_RETRY_COUNT) {
|
|
219
|
-
renderRetryCount++;
|
|
220
|
-
console.warn(`Canvas element not found, retrying... (${renderRetryCount}/${MAX_RETRY_COUNT})`);
|
|
221
|
-
setTimeout(renderPage, 100);
|
|
222
|
-
return;
|
|
223
|
-
} else {
|
|
224
|
-
console.error('Canvas element not found after multiple retries');
|
|
225
|
-
error.value = 'Canvas元素未找到';
|
|
226
|
-
return;
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
// 重置重试计数
|
|
231
|
-
renderRetryCount = 0;
|
|
232
|
-
|
|
233
|
-
const context = canvas.getContext('2d');
|
|
234
|
-
|
|
235
|
-
// 获取页面尺寸
|
|
236
|
-
const viewport = page.getViewport({ scale: scale.value });
|
|
237
|
-
|
|
238
|
-
// 获取容器可用宽度
|
|
239
|
-
const containerWidth = containerRef.value?.clientWidth || 800;
|
|
240
|
-
const maxWidth = containerWidth - 40; // 减去padding
|
|
241
|
-
|
|
242
|
-
// 如果PDF宽度超过容器宽度,自动缩放
|
|
243
|
-
let finalScale = scale.value;
|
|
244
|
-
if (viewport.width > maxWidth) {
|
|
245
|
-
finalScale = (maxWidth / viewport.width) * scale.value;
|
|
246
|
-
const adjustedViewport = page.getViewport({ scale: finalScale });
|
|
247
|
-
canvas.height = adjustedViewport.height;
|
|
248
|
-
canvas.width = adjustedViewport.width;
|
|
249
|
-
} else {
|
|
250
|
-
canvas.height = viewport.height;
|
|
251
|
-
canvas.width = viewport.width;
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
// 清空canvas
|
|
255
|
-
context.clearRect(0, 0, canvas.width, canvas.height);
|
|
256
|
-
|
|
257
|
-
// 渲染页面 - 使用最终的viewport
|
|
258
|
-
const finalViewport = finalScale !== scale.value ?
|
|
259
|
-
page.getViewport({ scale: finalScale }) : viewport;
|
|
260
|
-
|
|
261
|
-
const renderContext = {
|
|
262
|
-
canvasContext: context,
|
|
263
|
-
viewport: finalViewport
|
|
264
|
-
};
|
|
265
|
-
|
|
266
|
-
await page.render(renderContext).promise;
|
|
267
|
-
|
|
268
|
-
} catch (err) {
|
|
269
|
-
console.error('Page rendering error:', err);
|
|
270
|
-
error.value = '页面渲染失败';
|
|
271
|
-
}
|
|
272
|
-
};
|
|
273
|
-
|
|
274
|
-
// 翻页方法
|
|
275
|
-
const previousPage = () => {
|
|
276
|
-
if (currentPage.value > 1) {
|
|
277
|
-
currentPage.value--;
|
|
278
|
-
renderPage();
|
|
279
|
-
emit('page-change', currentPage.value);
|
|
280
|
-
}
|
|
281
|
-
};
|
|
282
|
-
|
|
283
|
-
const nextPage = () => {
|
|
284
|
-
if (currentPage.value < totalPages.value) {
|
|
285
|
-
currentPage.value++;
|
|
286
|
-
renderPage();
|
|
287
|
-
emit('page-change', currentPage.value);
|
|
288
|
-
}
|
|
289
|
-
};
|
|
290
|
-
|
|
291
|
-
// 缩放方法
|
|
292
|
-
const zoomIn = () => {
|
|
293
|
-
scale.value = Math.min(scale.value * 1.2, 3);
|
|
294
|
-
renderPage();
|
|
295
|
-
emit('zoom-change', scale.value);
|
|
296
|
-
};
|
|
297
|
-
|
|
298
|
-
const zoomOut = () => {
|
|
299
|
-
scale.value = Math.max(scale.value / 1.2, 0.1);
|
|
300
|
-
renderPage();
|
|
301
|
-
emit('zoom-change', scale.value);
|
|
302
|
-
};
|
|
303
|
-
|
|
304
|
-
const resetZoom = () => {
|
|
305
|
-
scale.value = 1;
|
|
306
|
-
renderPage();
|
|
307
|
-
emit('zoom-change', scale.value);
|
|
308
|
-
};
|
|
309
|
-
|
|
310
|
-
// 旋转页面
|
|
311
|
-
const rotatePage = () => {
|
|
312
|
-
rotation.value = (rotation.value + 90) % 360;
|
|
313
|
-
};
|
|
314
|
-
|
|
315
|
-
// 下载PDF
|
|
316
|
-
const downloadPdf = () => {
|
|
317
|
-
if (!props.downloadable) return;
|
|
318
|
-
|
|
319
|
-
try {
|
|
320
|
-
const link = document.createElement('a');
|
|
321
|
-
link.href = props.src;
|
|
322
|
-
link.download = 'document.pdf';
|
|
323
|
-
document.body.appendChild(link);
|
|
324
|
-
link.click();
|
|
325
|
-
document.body.removeChild(link);
|
|
326
|
-
|
|
327
|
-
emit('download', props.src);
|
|
328
|
-
} catch (err) {
|
|
329
|
-
console.error('Download error:', err);
|
|
330
|
-
}
|
|
331
|
-
};
|
|
332
|
-
|
|
333
|
-
// 监听属性变化
|
|
334
|
-
watch(() => props.src, () => {
|
|
335
|
-
loadPdf();
|
|
336
|
-
});
|
|
337
|
-
|
|
338
|
-
watch(() => props.page, (newPage) => {
|
|
339
|
-
if (newPage !== currentPage.value && newPage >= 1 && newPage <= totalPages.value) {
|
|
340
|
-
currentPage.value = newPage;
|
|
341
|
-
renderPage();
|
|
342
|
-
}
|
|
343
|
-
});
|
|
344
|
-
|
|
345
|
-
watch(() => props.zoom, (newZoom) => {
|
|
346
|
-
if (newZoom !== scale.value) {
|
|
347
|
-
scale.value = newZoom;
|
|
348
|
-
renderPage();
|
|
349
|
-
}
|
|
350
|
-
});
|
|
351
|
-
|
|
352
|
-
// 强制刷新渲染
|
|
353
|
-
const forceRender = async () => {
|
|
354
|
-
if (pdfDoc && canvasRef.value) {
|
|
355
|
-
await nextTick();
|
|
356
|
-
await renderPage();
|
|
357
|
-
}
|
|
358
|
-
};
|
|
359
|
-
|
|
360
|
-
// 组件挂载时加载PDF
|
|
361
|
-
onMounted(() => {
|
|
362
|
-
nextTick(() => {
|
|
363
|
-
loadPdf();
|
|
364
|
-
|
|
365
|
-
// 监听容器大小变化
|
|
366
|
-
if (containerRef.value && window.ResizeObserver) {
|
|
367
|
-
resizeObserver = new ResizeObserver(() => {
|
|
368
|
-
if (pdfDoc && canvasRef.value) {
|
|
369
|
-
// 延迟渲染以避免频繁触发
|
|
370
|
-
setTimeout(() => {
|
|
371
|
-
renderPage();
|
|
372
|
-
}, 100);
|
|
373
|
-
}
|
|
374
|
-
});
|
|
375
|
-
resizeObserver.observe(containerRef.value);
|
|
376
|
-
}
|
|
377
|
-
});
|
|
378
|
-
});
|
|
379
|
-
|
|
380
|
-
// 组件卸载时清理
|
|
381
|
-
onUnmounted(() => {
|
|
382
|
-
if (resizeObserver) {
|
|
383
|
-
resizeObserver.disconnect();
|
|
384
|
-
resizeObserver = null;
|
|
385
|
-
}
|
|
386
|
-
});
|
|
387
|
-
|
|
388
|
-
// 暴露强制刷新方法
|
|
389
|
-
defineExpose({
|
|
390
|
-
forceRender
|
|
391
|
-
});
|
|
392
|
-
</script>
|
|
393
|
-
|
|
394
|
-
<style lang="less" scoped>
|
|
395
|
-
.ebiz-pdf-viewer {
|
|
396
|
-
display: flex;
|
|
397
|
-
flex-direction: column;
|
|
398
|
-
border: 1px solid #e1e1e1;
|
|
399
|
-
border-radius: 4px;
|
|
400
|
-
overflow: hidden;
|
|
401
|
-
background: #f5f5f5;
|
|
402
|
-
}
|
|
403
|
-
|
|
404
|
-
.pdf-toolbar {
|
|
405
|
-
display: flex;
|
|
406
|
-
justify-content: space-between;
|
|
407
|
-
align-items: center;
|
|
408
|
-
padding: 8px 12px;
|
|
409
|
-
background: #fff;
|
|
410
|
-
border-bottom: 1px solid #e1e1e1;
|
|
411
|
-
|
|
412
|
-
.toolbar-left,
|
|
413
|
-
.toolbar-center,
|
|
414
|
-
.toolbar-right {
|
|
415
|
-
display: flex;
|
|
416
|
-
align-items: center;
|
|
417
|
-
gap: 8px;
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
.toolbar-btn {
|
|
421
|
-
padding: 4px 8px;
|
|
422
|
-
border: 1px solid #d1d1d1;
|
|
423
|
-
background: #fff;
|
|
424
|
-
border-radius: 2px;
|
|
425
|
-
cursor: pointer;
|
|
426
|
-
font-size: 12px;
|
|
427
|
-
|
|
428
|
-
&:hover:not(:disabled) {
|
|
429
|
-
background: #f0f0f0;
|
|
430
|
-
}
|
|
431
|
-
|
|
432
|
-
&:disabled {
|
|
433
|
-
opacity: 0.5;
|
|
434
|
-
cursor: not-allowed;
|
|
435
|
-
}
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
.page-info,
|
|
439
|
-
.zoom-info {
|
|
440
|
-
font-size: 12px;
|
|
441
|
-
color: #666;
|
|
442
|
-
min-width: 60px;
|
|
443
|
-
text-align: center;
|
|
444
|
-
}
|
|
445
|
-
}
|
|
446
|
-
|
|
447
|
-
.pdf-container {
|
|
448
|
-
flex: 1;
|
|
449
|
-
display: flex;
|
|
450
|
-
justify-content: center;
|
|
451
|
-
align-items: flex-start;
|
|
452
|
-
overflow: auto;
|
|
453
|
-
padding: 10px;
|
|
454
|
-
box-sizing: border-box;
|
|
455
|
-
}
|
|
456
|
-
|
|
457
|
-
.pdf-loading {
|
|
458
|
-
display: flex;
|
|
459
|
-
flex-direction: column;
|
|
460
|
-
align-items: center;
|
|
461
|
-
justify-content: center;
|
|
462
|
-
gap: 12px;
|
|
463
|
-
color: #666;
|
|
464
|
-
min-height: 200px;
|
|
465
|
-
width: 100%;
|
|
466
|
-
|
|
467
|
-
.loading-spinner {
|
|
468
|
-
width: 24px;
|
|
469
|
-
height: 24px;
|
|
470
|
-
border: 2px solid #f3f3f3;
|
|
471
|
-
border-top: 2px solid #0052d9;
|
|
472
|
-
border-radius: 50%;
|
|
473
|
-
animation: spin 1s linear infinite;
|
|
474
|
-
}
|
|
475
|
-
}
|
|
476
|
-
|
|
477
|
-
.pdf-error {
|
|
478
|
-
color: #e34d59;
|
|
479
|
-
text-align: center;
|
|
480
|
-
padding: 20px;
|
|
481
|
-
min-height: 200px;
|
|
482
|
-
width: 100%;
|
|
483
|
-
display: flex;
|
|
484
|
-
align-items: center;
|
|
485
|
-
justify-content: center;
|
|
486
|
-
}
|
|
487
|
-
|
|
488
|
-
.pdf-content {
|
|
489
|
-
display: flex;
|
|
490
|
-
justify-content: center;
|
|
491
|
-
align-items: flex-start;
|
|
492
|
-
min-height: 100%;
|
|
493
|
-
width: 100%;
|
|
494
|
-
padding-top: 10px;
|
|
495
|
-
|
|
496
|
-
canvas {
|
|
497
|
-
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
|
|
498
|
-
background: white;
|
|
499
|
-
transition: transform 0.3s ease;
|
|
500
|
-
display: block;
|
|
501
|
-
max-width: calc(100% - 20px);
|
|
502
|
-
height: auto;
|
|
503
|
-
margin: 0 auto;
|
|
504
|
-
}
|
|
505
|
-
}
|
|
506
|
-
|
|
507
|
-
@keyframes spin {
|
|
508
|
-
0% { transform: rotate(0deg); }
|
|
509
|
-
100% { transform: rotate(360deg); }
|
|
510
|
-
}
|
|
511
|
-
|
|
512
|
-
/* 响应式适配 */
|
|
513
|
-
@media (max-width: 768px) {
|
|
514
|
-
.pdf-container {
|
|
515
|
-
padding: 5px;
|
|
516
|
-
}
|
|
517
|
-
|
|
518
|
-
.pdf-content {
|
|
519
|
-
padding-top: 5px;
|
|
520
|
-
|
|
521
|
-
canvas {
|
|
522
|
-
max-width: calc(100% - 10px);
|
|
523
|
-
}
|
|
524
|
-
}
|
|
525
|
-
|
|
526
|
-
.pdf-toolbar {
|
|
527
|
-
padding: 6px 8px;
|
|
528
|
-
|
|
529
|
-
.toolbar-btn {
|
|
530
|
-
padding: 3px 6px;
|
|
531
|
-
font-size: 11px;
|
|
532
|
-
}
|
|
533
|
-
|
|
534
|
-
.page-info,
|
|
535
|
-
.zoom-info {
|
|
536
|
-
font-size: 11px;
|
|
537
|
-
min-width: 50px;
|
|
538
|
-
}
|
|
539
|
-
}
|
|
540
|
-
}
|
|
1
|
+
<template>
|
|
2
|
+
<div class="ebiz-pdf-viewer" :style="{ width: width, height: height }">
|
|
3
|
+
<!-- 工具栏 -->
|
|
4
|
+
<div v-if="showToolbar" class="pdf-toolbar">
|
|
5
|
+
<div class="toolbar-left">
|
|
6
|
+
<button
|
|
7
|
+
@click="previousPage"
|
|
8
|
+
:disabled="currentPage <= 1"
|
|
9
|
+
class="toolbar-btn"
|
|
10
|
+
title="上一页"
|
|
11
|
+
>
|
|
12
|
+
⬅
|
|
13
|
+
</button>
|
|
14
|
+
<span class="page-info">
|
|
15
|
+
{{ currentPage }} / {{ totalPages }}
|
|
16
|
+
</span>
|
|
17
|
+
<button
|
|
18
|
+
@click="nextPage"
|
|
19
|
+
:disabled="currentPage >= totalPages"
|
|
20
|
+
class="toolbar-btn"
|
|
21
|
+
title="下一页"
|
|
22
|
+
>
|
|
23
|
+
➡
|
|
24
|
+
</button>
|
|
25
|
+
</div>
|
|
26
|
+
<div class="toolbar-center">
|
|
27
|
+
<button @click="zoomOut" class="toolbar-btn" title="缩小">-</button>
|
|
28
|
+
<span class="zoom-info">{{ Math.round(scale * 100) }}%</span>
|
|
29
|
+
<button @click="zoomIn" class="toolbar-btn" title="放大">+</button>
|
|
30
|
+
<button @click="resetZoom" class="toolbar-btn" title="重置缩放">⟲</button>
|
|
31
|
+
</div>
|
|
32
|
+
<div class="toolbar-right">
|
|
33
|
+
<button @click="rotatePage" class="toolbar-btn" title="旋转">↻</button>
|
|
34
|
+
<button @click="downloadPdf" class="toolbar-btn" title="下载">⬇</button>
|
|
35
|
+
</div>
|
|
36
|
+
</div>
|
|
37
|
+
|
|
38
|
+
<!-- PDF 内容区域 -->
|
|
39
|
+
<div class="pdf-container" ref="containerRef">
|
|
40
|
+
<div v-if="loading" class="pdf-loading">
|
|
41
|
+
<div class="loading-spinner"></div>
|
|
42
|
+
<span>加载中...</span>
|
|
43
|
+
</div>
|
|
44
|
+
<div v-else-if="error" class="pdf-error">
|
|
45
|
+
<span>{{ error }}</span>
|
|
46
|
+
</div>
|
|
47
|
+
<div v-else class="pdf-content">
|
|
48
|
+
<canvas
|
|
49
|
+
ref="canvasRef"
|
|
50
|
+
:style="{
|
|
51
|
+
transform: `rotate(${rotation}deg)`,
|
|
52
|
+
transformOrigin: 'center center'
|
|
53
|
+
}"
|
|
54
|
+
></canvas>
|
|
55
|
+
</div>
|
|
56
|
+
</div>
|
|
57
|
+
</div>
|
|
58
|
+
</template>
|
|
59
|
+
|
|
60
|
+
<script>
|
|
61
|
+
export default {
|
|
62
|
+
name: "EbizPdfViewer"
|
|
63
|
+
}
|
|
64
|
+
</script>
|
|
65
|
+
|
|
66
|
+
<script setup>
|
|
67
|
+
import { ref, onMounted, onUnmounted, watch, nextTick } from 'vue';
|
|
68
|
+
|
|
69
|
+
const props = defineProps({
|
|
70
|
+
// PDF文件URL或base64数据
|
|
71
|
+
src: {
|
|
72
|
+
type: String,
|
|
73
|
+
required: true
|
|
74
|
+
},
|
|
75
|
+
// 组件宽度
|
|
76
|
+
width: {
|
|
77
|
+
type: String,
|
|
78
|
+
default: '100%'
|
|
79
|
+
},
|
|
80
|
+
// 组件高度
|
|
81
|
+
height: {
|
|
82
|
+
type: String,
|
|
83
|
+
default: '600px'
|
|
84
|
+
},
|
|
85
|
+
// 是否显示工具栏
|
|
86
|
+
showToolbar: {
|
|
87
|
+
type: Boolean,
|
|
88
|
+
default: true
|
|
89
|
+
},
|
|
90
|
+
// 初始页码
|
|
91
|
+
page: {
|
|
92
|
+
type: Number,
|
|
93
|
+
default: 1
|
|
94
|
+
},
|
|
95
|
+
// 初始缩放比例
|
|
96
|
+
zoom: {
|
|
97
|
+
type: Number,
|
|
98
|
+
default: 1
|
|
99
|
+
},
|
|
100
|
+
// 是否允许下载
|
|
101
|
+
downloadable: {
|
|
102
|
+
type: Boolean,
|
|
103
|
+
default: true
|
|
104
|
+
},
|
|
105
|
+
// PDF密码
|
|
106
|
+
password: {
|
|
107
|
+
type: String,
|
|
108
|
+
default: ''
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
const emit = defineEmits([
|
|
113
|
+
'load',
|
|
114
|
+
'error',
|
|
115
|
+
'page-change',
|
|
116
|
+
'zoom-change',
|
|
117
|
+
'download'
|
|
118
|
+
]);
|
|
119
|
+
|
|
120
|
+
// 响应式状态
|
|
121
|
+
const loading = ref(false);
|
|
122
|
+
const error = ref('');
|
|
123
|
+
const currentPage = ref(props.page);
|
|
124
|
+
const totalPages = ref(0);
|
|
125
|
+
const scale = ref(props.zoom);
|
|
126
|
+
const rotation = ref(0);
|
|
127
|
+
|
|
128
|
+
// DOM引用
|
|
129
|
+
const containerRef = ref(null);
|
|
130
|
+
const canvasRef = ref(null);
|
|
131
|
+
|
|
132
|
+
// PDF相关变量
|
|
133
|
+
let pdfDoc = null;
|
|
134
|
+
let pdfLib = null;
|
|
135
|
+
let renderRetryCount = 0;
|
|
136
|
+
const MAX_RETRY_COUNT = 3;
|
|
137
|
+
let resizeObserver = null;
|
|
138
|
+
|
|
139
|
+
// 初始化PDF.js
|
|
140
|
+
const initPdfJs = async () => {
|
|
141
|
+
try {
|
|
142
|
+
// 动态导入pdfjs-dist
|
|
143
|
+
pdfLib = await import('pdfjs-dist');
|
|
144
|
+
|
|
145
|
+
// 设置worker路径
|
|
146
|
+
pdfLib.GlobalWorkerOptions.workerSrc = `https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.11.174/pdf.worker.min.js`;
|
|
147
|
+
|
|
148
|
+
return true;
|
|
149
|
+
} catch (err) {
|
|
150
|
+
console.error('Failed to initialize PDF.js:', err);
|
|
151
|
+
error.value = '初始化PDF.js失败';
|
|
152
|
+
return false;
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
// 加载PDF文档
|
|
157
|
+
const loadPdf = async () => {
|
|
158
|
+
if (!props.src) return;
|
|
159
|
+
|
|
160
|
+
loading.value = true;
|
|
161
|
+
error.value = '';
|
|
162
|
+
|
|
163
|
+
try {
|
|
164
|
+
if (!pdfLib) {
|
|
165
|
+
const success = await initPdfJs();
|
|
166
|
+
if (!success) return;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const loadingTask = pdfLib.getDocument({
|
|
170
|
+
url: props.src,
|
|
171
|
+
password: props.password
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
pdfDoc = await loadingTask.promise;
|
|
175
|
+
totalPages.value = pdfDoc.numPages;
|
|
176
|
+
|
|
177
|
+
// 确保当前页在有效范围内
|
|
178
|
+
if (currentPage.value > totalPages.value) {
|
|
179
|
+
currentPage.value = totalPages.value;
|
|
180
|
+
}
|
|
181
|
+
if (currentPage.value < 1) {
|
|
182
|
+
currentPage.value = 1;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// 延迟渲染确保DOM完全准备
|
|
186
|
+
await nextTick();
|
|
187
|
+
setTimeout(async () => {
|
|
188
|
+
await renderPage();
|
|
189
|
+
}, 50);
|
|
190
|
+
|
|
191
|
+
emit('load', {
|
|
192
|
+
totalPages: totalPages.value,
|
|
193
|
+
currentPage: currentPage.value
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
} catch (err) {
|
|
197
|
+
console.error('PDF loading error:', err);
|
|
198
|
+
error.value = err.message || 'PDF加载失败';
|
|
199
|
+
emit('error', err);
|
|
200
|
+
} finally {
|
|
201
|
+
loading.value = false;
|
|
202
|
+
}
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
// 渲染当前页面
|
|
206
|
+
const renderPage = async () => {
|
|
207
|
+
if (!pdfDoc || !canvasRef.value) return;
|
|
208
|
+
|
|
209
|
+
try {
|
|
210
|
+
// 确保canvas元素已经完全渲染到DOM中
|
|
211
|
+
await nextTick();
|
|
212
|
+
|
|
213
|
+
const page = await pdfDoc.getPage(currentPage.value);
|
|
214
|
+
const canvas = canvasRef.value;
|
|
215
|
+
|
|
216
|
+
// 再次检查canvas是否存在
|
|
217
|
+
if (!canvas) {
|
|
218
|
+
if (renderRetryCount < MAX_RETRY_COUNT) {
|
|
219
|
+
renderRetryCount++;
|
|
220
|
+
console.warn(`Canvas element not found, retrying... (${renderRetryCount}/${MAX_RETRY_COUNT})`);
|
|
221
|
+
setTimeout(renderPage, 100);
|
|
222
|
+
return;
|
|
223
|
+
} else {
|
|
224
|
+
console.error('Canvas element not found after multiple retries');
|
|
225
|
+
error.value = 'Canvas元素未找到';
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// 重置重试计数
|
|
231
|
+
renderRetryCount = 0;
|
|
232
|
+
|
|
233
|
+
const context = canvas.getContext('2d');
|
|
234
|
+
|
|
235
|
+
// 获取页面尺寸
|
|
236
|
+
const viewport = page.getViewport({ scale: scale.value });
|
|
237
|
+
|
|
238
|
+
// 获取容器可用宽度
|
|
239
|
+
const containerWidth = containerRef.value?.clientWidth || 800;
|
|
240
|
+
const maxWidth = containerWidth - 40; // 减去padding
|
|
241
|
+
|
|
242
|
+
// 如果PDF宽度超过容器宽度,自动缩放
|
|
243
|
+
let finalScale = scale.value;
|
|
244
|
+
if (viewport.width > maxWidth) {
|
|
245
|
+
finalScale = (maxWidth / viewport.width) * scale.value;
|
|
246
|
+
const adjustedViewport = page.getViewport({ scale: finalScale });
|
|
247
|
+
canvas.height = adjustedViewport.height;
|
|
248
|
+
canvas.width = adjustedViewport.width;
|
|
249
|
+
} else {
|
|
250
|
+
canvas.height = viewport.height;
|
|
251
|
+
canvas.width = viewport.width;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// 清空canvas
|
|
255
|
+
context.clearRect(0, 0, canvas.width, canvas.height);
|
|
256
|
+
|
|
257
|
+
// 渲染页面 - 使用最终的viewport
|
|
258
|
+
const finalViewport = finalScale !== scale.value ?
|
|
259
|
+
page.getViewport({ scale: finalScale }) : viewport;
|
|
260
|
+
|
|
261
|
+
const renderContext = {
|
|
262
|
+
canvasContext: context,
|
|
263
|
+
viewport: finalViewport
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
await page.render(renderContext).promise;
|
|
267
|
+
|
|
268
|
+
} catch (err) {
|
|
269
|
+
console.error('Page rendering error:', err);
|
|
270
|
+
error.value = '页面渲染失败';
|
|
271
|
+
}
|
|
272
|
+
};
|
|
273
|
+
|
|
274
|
+
// 翻页方法
|
|
275
|
+
const previousPage = () => {
|
|
276
|
+
if (currentPage.value > 1) {
|
|
277
|
+
currentPage.value--;
|
|
278
|
+
renderPage();
|
|
279
|
+
emit('page-change', currentPage.value);
|
|
280
|
+
}
|
|
281
|
+
};
|
|
282
|
+
|
|
283
|
+
const nextPage = () => {
|
|
284
|
+
if (currentPage.value < totalPages.value) {
|
|
285
|
+
currentPage.value++;
|
|
286
|
+
renderPage();
|
|
287
|
+
emit('page-change', currentPage.value);
|
|
288
|
+
}
|
|
289
|
+
};
|
|
290
|
+
|
|
291
|
+
// 缩放方法
|
|
292
|
+
const zoomIn = () => {
|
|
293
|
+
scale.value = Math.min(scale.value * 1.2, 3);
|
|
294
|
+
renderPage();
|
|
295
|
+
emit('zoom-change', scale.value);
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
const zoomOut = () => {
|
|
299
|
+
scale.value = Math.max(scale.value / 1.2, 0.1);
|
|
300
|
+
renderPage();
|
|
301
|
+
emit('zoom-change', scale.value);
|
|
302
|
+
};
|
|
303
|
+
|
|
304
|
+
const resetZoom = () => {
|
|
305
|
+
scale.value = 1;
|
|
306
|
+
renderPage();
|
|
307
|
+
emit('zoom-change', scale.value);
|
|
308
|
+
};
|
|
309
|
+
|
|
310
|
+
// 旋转页面
|
|
311
|
+
const rotatePage = () => {
|
|
312
|
+
rotation.value = (rotation.value + 90) % 360;
|
|
313
|
+
};
|
|
314
|
+
|
|
315
|
+
// 下载PDF
|
|
316
|
+
const downloadPdf = () => {
|
|
317
|
+
if (!props.downloadable) return;
|
|
318
|
+
|
|
319
|
+
try {
|
|
320
|
+
const link = document.createElement('a');
|
|
321
|
+
link.href = props.src;
|
|
322
|
+
link.download = 'document.pdf';
|
|
323
|
+
document.body.appendChild(link);
|
|
324
|
+
link.click();
|
|
325
|
+
document.body.removeChild(link);
|
|
326
|
+
|
|
327
|
+
emit('download', props.src);
|
|
328
|
+
} catch (err) {
|
|
329
|
+
console.error('Download error:', err);
|
|
330
|
+
}
|
|
331
|
+
};
|
|
332
|
+
|
|
333
|
+
// 监听属性变化
|
|
334
|
+
watch(() => props.src, () => {
|
|
335
|
+
loadPdf();
|
|
336
|
+
});
|
|
337
|
+
|
|
338
|
+
watch(() => props.page, (newPage) => {
|
|
339
|
+
if (newPage !== currentPage.value && newPage >= 1 && newPage <= totalPages.value) {
|
|
340
|
+
currentPage.value = newPage;
|
|
341
|
+
renderPage();
|
|
342
|
+
}
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
watch(() => props.zoom, (newZoom) => {
|
|
346
|
+
if (newZoom !== scale.value) {
|
|
347
|
+
scale.value = newZoom;
|
|
348
|
+
renderPage();
|
|
349
|
+
}
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
// 强制刷新渲染
|
|
353
|
+
const forceRender = async () => {
|
|
354
|
+
if (pdfDoc && canvasRef.value) {
|
|
355
|
+
await nextTick();
|
|
356
|
+
await renderPage();
|
|
357
|
+
}
|
|
358
|
+
};
|
|
359
|
+
|
|
360
|
+
// 组件挂载时加载PDF
|
|
361
|
+
onMounted(() => {
|
|
362
|
+
nextTick(() => {
|
|
363
|
+
loadPdf();
|
|
364
|
+
|
|
365
|
+
// 监听容器大小变化
|
|
366
|
+
if (containerRef.value && window.ResizeObserver) {
|
|
367
|
+
resizeObserver = new ResizeObserver(() => {
|
|
368
|
+
if (pdfDoc && canvasRef.value) {
|
|
369
|
+
// 延迟渲染以避免频繁触发
|
|
370
|
+
setTimeout(() => {
|
|
371
|
+
renderPage();
|
|
372
|
+
}, 100);
|
|
373
|
+
}
|
|
374
|
+
});
|
|
375
|
+
resizeObserver.observe(containerRef.value);
|
|
376
|
+
}
|
|
377
|
+
});
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
// 组件卸载时清理
|
|
381
|
+
onUnmounted(() => {
|
|
382
|
+
if (resizeObserver) {
|
|
383
|
+
resizeObserver.disconnect();
|
|
384
|
+
resizeObserver = null;
|
|
385
|
+
}
|
|
386
|
+
});
|
|
387
|
+
|
|
388
|
+
// 暴露强制刷新方法
|
|
389
|
+
defineExpose({
|
|
390
|
+
forceRender
|
|
391
|
+
});
|
|
392
|
+
</script>
|
|
393
|
+
|
|
394
|
+
<style lang="less" scoped>
|
|
395
|
+
.ebiz-pdf-viewer {
|
|
396
|
+
display: flex;
|
|
397
|
+
flex-direction: column;
|
|
398
|
+
border: 1px solid #e1e1e1;
|
|
399
|
+
border-radius: 4px;
|
|
400
|
+
overflow: hidden;
|
|
401
|
+
background: #f5f5f5;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
.pdf-toolbar {
|
|
405
|
+
display: flex;
|
|
406
|
+
justify-content: space-between;
|
|
407
|
+
align-items: center;
|
|
408
|
+
padding: 8px 12px;
|
|
409
|
+
background: #fff;
|
|
410
|
+
border-bottom: 1px solid #e1e1e1;
|
|
411
|
+
|
|
412
|
+
.toolbar-left,
|
|
413
|
+
.toolbar-center,
|
|
414
|
+
.toolbar-right {
|
|
415
|
+
display: flex;
|
|
416
|
+
align-items: center;
|
|
417
|
+
gap: 8px;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
.toolbar-btn {
|
|
421
|
+
padding: 4px 8px;
|
|
422
|
+
border: 1px solid #d1d1d1;
|
|
423
|
+
background: #fff;
|
|
424
|
+
border-radius: 2px;
|
|
425
|
+
cursor: pointer;
|
|
426
|
+
font-size: 12px;
|
|
427
|
+
|
|
428
|
+
&:hover:not(:disabled) {
|
|
429
|
+
background: #f0f0f0;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
&:disabled {
|
|
433
|
+
opacity: 0.5;
|
|
434
|
+
cursor: not-allowed;
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
.page-info,
|
|
439
|
+
.zoom-info {
|
|
440
|
+
font-size: 12px;
|
|
441
|
+
color: #666;
|
|
442
|
+
min-width: 60px;
|
|
443
|
+
text-align: center;
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
.pdf-container {
|
|
448
|
+
flex: 1;
|
|
449
|
+
display: flex;
|
|
450
|
+
justify-content: center;
|
|
451
|
+
align-items: flex-start;
|
|
452
|
+
overflow: auto;
|
|
453
|
+
padding: 10px;
|
|
454
|
+
box-sizing: border-box;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
.pdf-loading {
|
|
458
|
+
display: flex;
|
|
459
|
+
flex-direction: column;
|
|
460
|
+
align-items: center;
|
|
461
|
+
justify-content: center;
|
|
462
|
+
gap: 12px;
|
|
463
|
+
color: #666;
|
|
464
|
+
min-height: 200px;
|
|
465
|
+
width: 100%;
|
|
466
|
+
|
|
467
|
+
.loading-spinner {
|
|
468
|
+
width: 24px;
|
|
469
|
+
height: 24px;
|
|
470
|
+
border: 2px solid #f3f3f3;
|
|
471
|
+
border-top: 2px solid #0052d9;
|
|
472
|
+
border-radius: 50%;
|
|
473
|
+
animation: spin 1s linear infinite;
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
.pdf-error {
|
|
478
|
+
color: #e34d59;
|
|
479
|
+
text-align: center;
|
|
480
|
+
padding: 20px;
|
|
481
|
+
min-height: 200px;
|
|
482
|
+
width: 100%;
|
|
483
|
+
display: flex;
|
|
484
|
+
align-items: center;
|
|
485
|
+
justify-content: center;
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
.pdf-content {
|
|
489
|
+
display: flex;
|
|
490
|
+
justify-content: center;
|
|
491
|
+
align-items: flex-start;
|
|
492
|
+
min-height: 100%;
|
|
493
|
+
width: 100%;
|
|
494
|
+
padding-top: 10px;
|
|
495
|
+
|
|
496
|
+
canvas {
|
|
497
|
+
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
|
|
498
|
+
background: white;
|
|
499
|
+
transition: transform 0.3s ease;
|
|
500
|
+
display: block;
|
|
501
|
+
max-width: calc(100% - 20px);
|
|
502
|
+
height: auto;
|
|
503
|
+
margin: 0 auto;
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
@keyframes spin {
|
|
508
|
+
0% { transform: rotate(0deg); }
|
|
509
|
+
100% { transform: rotate(360deg); }
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
/* 响应式适配 */
|
|
513
|
+
@media (max-width: 768px) {
|
|
514
|
+
.pdf-container {
|
|
515
|
+
padding: 5px;
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
.pdf-content {
|
|
519
|
+
padding-top: 5px;
|
|
520
|
+
|
|
521
|
+
canvas {
|
|
522
|
+
max-width: calc(100% - 10px);
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
.pdf-toolbar {
|
|
527
|
+
padding: 6px 8px;
|
|
528
|
+
|
|
529
|
+
.toolbar-btn {
|
|
530
|
+
padding: 3px 6px;
|
|
531
|
+
font-size: 11px;
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
.page-info,
|
|
535
|
+
.zoom-info {
|
|
536
|
+
font-size: 11px;
|
|
537
|
+
min-width: 50px;
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
541
|
</style>
|