-
-
Notifications
You must be signed in to change notification settings - Fork 67
/
VuePDF.vue
320 lines (281 loc) · 8.74 KB
/
VuePDF.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<!-- eslint-disable no-case-declarations -->
<script setup lang="ts">
import * as PDFJS from 'pdfjs-dist'
import { computed, onMounted, ref, toRaw, watch } from 'vue'
import 'pdfjs-dist/web/pdf_viewer.css'
import type { PDFDocumentLoadingTask, PDFPageProxy, PageViewport, RenderTask } from 'pdfjs-dist'
import type { GetViewportParameters, PDFDocumentProxy, RenderParameters } from 'pdfjs-dist/types/src/display/api'
import type { AnnotationEventPayload, HighlightEventPayload, HighlightOptions, LoadedEventPayload, TextLayerLoadedEventPayload, WatermarkOptions } from './types'
import AnnotationLayer from './layers/AnnotationLayer.vue'
import TextLayer from './layers/TextLayer.vue'
import XFALayer from './layers/XFALayer.vue'
interface InternalProps {
page: PDFPageProxy | undefined
document: PDFDocumentProxy | undefined
viewport: PageViewport | undefined
}
const props = withDefaults(defineProps<{
pdf?: PDFDocumentLoadingTask
page?: number
scale?: number
rotation?: number
fitParent?: boolean
width?: number
height?: number
textLayer?: boolean
imageResourcesPath?: string
hideForms?: boolean
intent?: string
annotationLayer?: boolean
annotationsFilter?: string[]
annotationsMap?: object
watermarkText?: string
watermarkOptions?: WatermarkOptions
highlightText?: string
highlightOptions?: HighlightOptions
}>(), {
page: 1,
scale: 1,
intent: 'display',
})
const emit = defineEmits<{
(event: 'annotation', payload: AnnotationEventPayload): void
(event: 'highlight', payload: HighlightEventPayload): void
(event: 'loaded', payload: LoadedEventPayload): void
(event: 'textLoaded', payload: TextLayerLoadedEventPayload): void
(event: 'annotationLoaded', payload: any[]): void
(event: 'xfaLoaded'): void
}>()
// Template Refs
const container = ref<HTMLSpanElement>()
const loadingLayer = ref<HTMLSpanElement>()
const loading = ref(false)
let renderTask: RenderTask
const internalProps = computed(() => {
return {
viewport: undefined,
document: undefined,
page: undefined,
} as InternalProps
})
const alayerProps = computed(() => {
return {
annotationsMap: props.annotationsMap,
annotationsFilter: props.annotationsFilter,
imageResourcesPath: props.imageResourcesPath,
hideForms: props.hideForms,
intent: props.intent,
}
})
const tlayerProps = computed(() => {
return {
highlightText: props.highlightText,
highlightOptions: props.highlightOptions,
}
})
function getWatermarkOptionsWithDefaults(): WatermarkOptions {
return Object.assign({}, {
columns: 4,
rows: 4,
rotation: 45,
fontSize: 18,
color: 'rgba(211, 210, 211, 0.4)',
}, props.watermarkOptions)
}
function getRotation(rotation: number): number {
if (!(typeof rotation === 'number' && rotation % 90 === 0))
return 0
const factor = rotation / 90
if (factor > 4)
return getRotation(rotation - 360)
else if (factor < 0)
return getRotation(rotation + 360)
return rotation
}
function getScale(page: PDFPageProxy): number {
let fscale = props.scale
if (props.fitParent) {
const parentWidth: number = (container.value!.parentNode! as HTMLElement).clientWidth
const scale1Width = page.getViewport({ scale: 1 }).width
fscale = parentWidth / scale1Width
}
else if (props.width) {
const scale1Width = page.getViewport({ scale: 1 }).width
fscale = props.width / scale1Width
}
else if (props.height) {
const scale1Height = page.getViewport({ scale: 1 }).height
fscale = props.height / scale1Height
}
return fscale
}
function paintWatermark(zoomRatio = 1.0) {
if (!props.watermarkText)
return
const canvas = getCurrentCanvas()
if (!canvas)
return
const ctx = canvas.getContext('2d')
if (!ctx)
return
const mergeOptions = getWatermarkOptionsWithDefaults()
const text = props.watermarkText
const columns = mergeOptions.columns!
const rows = mergeOptions.rows!
const numWatermarks = columns * rows
const rotation = mergeOptions.rotation!
const pixels = mergeOptions.fontSize! * zoomRatio
ctx.font = `${pixels}px Trebuchet MS`
ctx.fillStyle = mergeOptions.color!
for (let i = 0; i < numWatermarks; i++) {
const x = (i % columns) * (canvas.width / columns) + canvas.width / (columns * 2)
const y = Math.floor(i / columns) * (canvas.height / rows) + canvas.height / (rows * 2)
const textWidth = ctx.measureText(text).width
ctx.save()
ctx.translate(x, y)
ctx.rotate(-rotation * (Math.PI / 180))
ctx.fillText(text, -textWidth / 2, pixels / 2)
ctx.restore()
}
}
function getCurrentCanvas(): HTMLCanvasElement | null {
let oldCanvas = null
container.value?.childNodes.forEach((el) => {
if ((el as HTMLElement).tagName === 'CANVAS')
oldCanvas = el
})
return oldCanvas
}
function setupCanvas(viewport: PageViewport): HTMLCanvasElement {
let canvas
const currentCanvas = getCurrentCanvas()!
if (currentCanvas && currentCanvas?.getAttribute('role') === 'main') {
canvas = currentCanvas
}
else {
canvas = document.createElement('canvas')
canvas.style.display = 'block'
canvas.setAttribute('dir', 'ltr')
}
const outputScale = window.devicePixelRatio || 1
canvas.width = Math.floor(viewport.width * outputScale)
canvas.height = Math.floor(viewport.height * outputScale)
canvas.style.width = `${Math.floor(viewport.width)}px`
canvas.style.height = `${Math.floor(viewport.height)}px`
// --scale-factor property
container.value?.style.setProperty('--scale-factor', `${viewport.scale}`)
// Also setting dimension properties for load layer
loadingLayer.value!.style.width = `${Math.floor(viewport.width)}px`
loadingLayer.value!.style.height = `${Math.floor(viewport.height)}px`
loadingLayer.value!.style.top = '0'
loadingLayer.value!.style.left = '0'
loading.value = true
return canvas
}
function cancelRender() {
if (renderTask)
renderTask.cancel()
}
function renderPage(pageNum: number) {
toRaw(internalProps.value.document)?.getPage(pageNum).then((page) => {
cancelRender()
const defaultViewport = page.getViewport()
const viewportParams: GetViewportParameters = {
scale: getScale(page),
rotation: getRotation((props.rotation || 0) + defaultViewport.rotation),
}
const viewport = page.getViewport(viewportParams)
const oldCanvas = getCurrentCanvas()
const canvas = setupCanvas(viewport)
const outputScale = window.devicePixelRatio || 1
const transform = outputScale !== 1 ? [outputScale, 0, 0, outputScale, 0, 0] : undefined
// Render PDF page into canvas context
const renderContext: RenderParameters = {
canvasContext: canvas.getContext('2d')!,
viewport,
annotationMode: props.hideForms ? PDFJS.AnnotationMode.ENABLE : PDFJS.AnnotationMode.ENABLE_FORMS,
transform,
intent: props.intent,
}
if (canvas?.getAttribute('role') !== 'main') {
if (oldCanvas)
container.value?.replaceChild(canvas, oldCanvas)
}
else {
canvas.removeAttribute('role')
}
internalProps.value.page = page
internalProps.value.viewport = viewport
renderTask = page.render(renderContext)
renderTask.promise.then(() => {
loading.value = false
paintWatermark(viewport.scale)
emit('loaded', internalProps.value.viewport!)
}).catch(() => {
// render task cancelled
})
})
}
function initDoc(proxy: PDFDocumentLoadingTask) {
proxy.promise.then(async (document) => {
internalProps.value.document = document
renderPage(props.page)
})
}
watch(() => props.pdf, (pdf) => {
// For any changes on pdf, reinicialize all
if (pdf !== undefined)
initDoc(pdf)
})
watch(() => [
props.scale,
props.width,
props.height,
props.rotation,
props.page,
props.hideForms,
props.intent,
], () => {
// Props that should dispatch an render task
renderPage(props.page)
})
onMounted(() => {
if (props.pdf !== undefined)
initDoc(props.pdf)
})
// Exposed methods
function reload() {
renderPage(props.page)
}
function cancel() {
cancelRender()
}
defineExpose({
reload,
cancel,
})
</script>
<template>
<div ref="container" style="position: relative; display: block; overflow: hidden;">
<canvas dir="ltr" style="display: block" role="main" />
<AnnotationLayer
v-if="annotationLayer"
v-bind="{ ...internalProps, ...alayerProps }"
@annotation="emit('annotation', $event)"
@annotation-loaded="emit('annotationLoaded', $event)"
/>
<TextLayer
v-if="textLayer"
v-bind="{ ...internalProps, ...tlayerProps }"
@highlight="emit('highlight', $event)"
@text-loaded="emit('textLoaded', $event)"
/>
<XFALayer
v-bind="{ ...internalProps }"
@xfa-loaded="emit('xfaLoaded')"
/>
<div v-show="loading" ref="loadingLayer" style="position: absolute;">
<slot />
</div>
</div>
</template>