@libresign/pdf-elements 0.1.2 → 0.2.1
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 +14 -12
- package/dist/pdf-elements.common.js +1408 -1365
- package/dist/pdf-elements.common.js.map +1 -1
- package/dist/pdf-elements.css +1 -1
- package/dist/pdf-elements.umd.js +1408 -1365
- package/dist/pdf-elements.umd.js.map +1 -1
- package/dist/pdf-elements.umd.min.js +2 -2
- package/dist/pdf-elements.umd.min.js.map +1 -1
- package/package.json +1 -1
- package/src/components/PDFElements.vue +42 -0
- package/src/index.js +6 -0
- package/src/utils/asyncReader.js +4 -0
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@libresign/pdf-elements",
|
|
3
3
|
"description": "PDF viewer with draggable and resizable element overlays for Vue 2",
|
|
4
|
-
"version": "0.1
|
|
4
|
+
"version": "0.2.1",
|
|
5
5
|
"author": "LibreCode <contact@librecode.coop>",
|
|
6
6
|
"private": false,
|
|
7
7
|
"main": "dist/pdf-elements.umd.js",
|
|
@@ -179,6 +179,10 @@ export default {
|
|
|
179
179
|
type: String,
|
|
180
180
|
default: '{currentPage} of {totalPages}',
|
|
181
181
|
},
|
|
182
|
+
autoFitZoom: {
|
|
183
|
+
type: Boolean,
|
|
184
|
+
default: false,
|
|
185
|
+
},
|
|
182
186
|
},
|
|
183
187
|
data() {
|
|
184
188
|
return {
|
|
@@ -224,6 +228,9 @@ export default {
|
|
|
224
228
|
window.addEventListener('resize', this.onViewportScroll)
|
|
225
229
|
this.$el?.addEventListener('scroll', this.onViewportScroll, { passive: true })
|
|
226
230
|
this.$el?.addEventListener('wheel', this.boundHandleWheel, { passive: false })
|
|
231
|
+
if (this.autoFitZoom) {
|
|
232
|
+
window.addEventListener('resize', this.adjustZoomToFit)
|
|
233
|
+
}
|
|
227
234
|
},
|
|
228
235
|
beforeUnmount() {
|
|
229
236
|
if (this.zoomRafId) {
|
|
@@ -241,6 +248,9 @@ export default {
|
|
|
241
248
|
window.removeEventListener('scroll', this.onViewportScroll)
|
|
242
249
|
window.removeEventListener('resize', this.onViewportScroll)
|
|
243
250
|
this.$el?.removeEventListener('scroll', this.onViewportScroll)
|
|
251
|
+
if (this.autoFitZoom) {
|
|
252
|
+
window.removeEventListener('resize', this.adjustZoomToFit)
|
|
253
|
+
}
|
|
244
254
|
if (this.viewportRafId) {
|
|
245
255
|
window.cancelAnimationFrame(this.viewportRafId)
|
|
246
256
|
this.viewportRafId = 0
|
|
@@ -284,6 +294,11 @@ export default {
|
|
|
284
294
|
this.selectedDocIndex = 0
|
|
285
295
|
this.selectedPageIndex = 0
|
|
286
296
|
this.$emit('pdf-elements:end-init', { docsCount: docs.length })
|
|
297
|
+
this.$nextTick(() => {
|
|
298
|
+
if (this.autoFitZoom) {
|
|
299
|
+
this.adjustZoomToFit()
|
|
300
|
+
}
|
|
301
|
+
})
|
|
287
302
|
}
|
|
288
303
|
},
|
|
289
304
|
|
|
@@ -804,6 +819,33 @@ export default {
|
|
|
804
819
|
const pagesScale = doc.pagesScale[pageIndex] || 1
|
|
805
820
|
return pageRef.getCanvasMeasurement().canvasHeight / pagesScale
|
|
806
821
|
},
|
|
822
|
+
calculateOptimalScale(maxPageWidth) {
|
|
823
|
+
const containerWidth = this.$el?.clientWidth || 0
|
|
824
|
+
if (!containerWidth || !maxPageWidth) return 1
|
|
825
|
+
|
|
826
|
+
const availableWidth = containerWidth - 40
|
|
827
|
+
return Math.max(0.1, Math.min(2, availableWidth / maxPageWidth))
|
|
828
|
+
},
|
|
829
|
+
adjustZoomToFit() {
|
|
830
|
+
if (!this.autoFitZoom || !this.pdfDocuments.length) return
|
|
831
|
+
|
|
832
|
+
const canvases = this.$el?.querySelectorAll('canvas')
|
|
833
|
+
if (!canvases?.length) return
|
|
834
|
+
|
|
835
|
+
const maxCanvasWidth = Math.max(...Array.from(canvases).map(canvas =>
|
|
836
|
+
canvas.width / (this.scale || 1),
|
|
837
|
+
))
|
|
838
|
+
|
|
839
|
+
const optimalScale = this.calculateOptimalScale(maxCanvasWidth)
|
|
840
|
+
if (Math.abs(optimalScale - this.scale) > 0.01) {
|
|
841
|
+
this.scale = optimalScale
|
|
842
|
+
this.visualScale = optimalScale
|
|
843
|
+
this.pdfDocuments.forEach((doc) => {
|
|
844
|
+
doc.pagesScale = doc.pagesScale.map(() => this.scale)
|
|
845
|
+
})
|
|
846
|
+
this.cachePageBounds()
|
|
847
|
+
}
|
|
848
|
+
},
|
|
807
849
|
},
|
|
808
850
|
}
|
|
809
851
|
</script>
|
package/src/index.js
CHANGED
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
// SPDX-FileCopyrightText: 2026 LibreCode coop and contributors
|
|
2
2
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
3
3
|
|
|
4
|
+
import { GlobalWorkerOptions } from 'pdfjs-dist'
|
|
5
|
+
import pdfWorkerCode from 'pdfjs-dist/build/pdf.worker.min.mjs'
|
|
6
|
+
|
|
7
|
+
GlobalWorkerOptions.workerSrc = pdfWorkerCode
|
|
8
|
+
|
|
4
9
|
import PDFElements from './components/PDFElements.vue'
|
|
10
|
+
export { setWorkerPath } from './utils/asyncReader.js'
|
|
5
11
|
|
|
6
12
|
PDFElements.install = function(Vue) {
|
|
7
13
|
if (PDFElements.install.installed) return
|
package/src/utils/asyncReader.js
CHANGED
|
@@ -6,6 +6,10 @@ import pdfWorkerCode from 'pdfjs-dist/build/pdf.worker.min.mjs'
|
|
|
6
6
|
|
|
7
7
|
GlobalWorkerOptions.workerSrc = pdfWorkerCode
|
|
8
8
|
|
|
9
|
+
export function setWorkerPath(path) {
|
|
10
|
+
GlobalWorkerOptions.workerSrc = path
|
|
11
|
+
}
|
|
12
|
+
|
|
9
13
|
export function readAsArrayBuffer(file) {
|
|
10
14
|
return new Promise((resolve, reject) => {
|
|
11
15
|
const reader = new FileReader()
|