feat: default settings prop
parent
79b598315f
commit
93625e201e
45
README.md
45
README.md
|
|
@ -1,58 +1,36 @@
|
||||||
# 📄 pdf-viewer-vue
|
# 📄 pdf-viewer-vue
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
PDF viewer component for Vue 2 and Vue 3
|
PDF viewer component for Vue 2 and Vue 3
|
||||||
|
|
||||||
[](https://npmjs.com/package/pdf-viewer-vue) [](https://npmjs.com/package/pdf-viewer-vue) [](https://github.com/DingRui12138/vue-pdf-viewer) [](https://github.com/DingRui12138/vue-pdf-viewer/blob/master/LICENSE)
|
[](https://npmjs.com/package/pdf-viewer-vue) [](https://npmjs.com/package/pdf-viewer-vue) [](https://github.com/DingRui12138/vue-pdf-viewer) [](https://github.com/DingRui12138/vue-pdf-viewer/blob/master/LICENSE)
|
||||||
|
|
||||||
|
|
||||||
## Compatibility
|
## Compatibility
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
This package is compatible with both Vue 2 and Vue 3. The default exported build is for Vue 3, but `dist` directory also contains a build for Vue 2 (`dist/vue2-pdf-viewer.js`). See the example in [Usage](#usage) section.
|
This package is compatible with both Vue 2 and Vue 3. The default exported build is for Vue 3, but `dist` directory also contains a build for Vue 2 (`dist/vue2-pdf-viewer.js`). See the example in [Usage](#usage) section.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Depending on the environment, the package can be installed in one of the following ways:
|
Depending on the environment, the package can be installed in one of the following ways:
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
|
|
||||||
npm install pdf-viewer-vue
|
npm install pdf-viewer-vue
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
|
|
||||||
yarn add pdf-viewer-vue
|
yarn add pdf-viewer-vue
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
```html
|
```html
|
||||||
|
|
||||||
<script src="https://unpkg.com/pdf-viewer-vue"></script>
|
<script src="https://unpkg.com/pdf-viewer-vue"></script>
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
```vue
|
```vue
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<div>
|
<div>
|
||||||
<h1>File</h1>
|
<h1>File</h1>
|
||||||
|
|
@ -71,7 +49,6 @@ yarn add pdf-viewer-vue
|
||||||
@download="handleDownload"
|
@download="handleDownload"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
|
|
@ -86,37 +63,27 @@ export default {
|
||||||
},
|
},
|
||||||
|
|
||||||
data() {
|
data() {
|
||||||
|
|
||||||
return {
|
return {
|
||||||
url: '<PDF_URL>',
|
url: '<PDF_URL>',
|
||||||
base64: '<BASE64_ENCODED_PDF>',
|
base64: '<BASE64_ENCODED_PDF>',
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### Props
|
### Props
|
||||||
|
|
||||||
| Name | Type | Accepted values | Description |
|
| Name | Type | Accepted values | Description |
|
||||||
| -------------- | ---------- | --------------------------------------------------------------------------------------------------------------- | ------------------ |
|
| -------------- | ------------------------- | --------------------------------------------------------------------------------------------------------------- | ------------------ |
|
||||||
| source | `string` | document `URL` or `Base64` | source of document |
|
| source | `string` | document `URL` or `Base64` | source of document |
|
||||||
| controls | `string[]` | `[`<br/>`'download',`<br/>`'print',`<br/>`'rotate',`<br/>`'zoom',`<br/>`'catalog',`<br/>`'switchPage',`<br/>`]` | visible controls |
|
| controls | `string[]` | `[`<br/>`'download',`<br/>`'print',`<br/>`'rotate',`<br/>`'zoom',`<br/>`'catalog',`<br/>`'switchPage',`<br/>`]` | visible controls |
|
||||||
| loading-text | `string` | - | loading text |
|
| loading-text | `string` | - | loading text |
|
||||||
| rendering-text | `string` | - | rendering text |
|
| rendering-text | `string` | - | rendering text |
|
||||||
|
| settings | `{ defaultZoom: number }` | - | default settings |
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### Events
|
### Events
|
||||||
|
|
||||||
|
|
||||||
| Name | Value | Description |
|
| Name | Value | Description |
|
||||||
| ---------------- | ------------------------------------- | ------------------------------- |
|
| ---------------- | ------------------------------------- | ------------------------------- |
|
||||||
| download | `{source: string; filename: string;}` | pdf file base info |
|
| download | `{source: string; filename: string;}` | pdf file base info |
|
||||||
|
|
@ -125,20 +92,12 @@ export default {
|
||||||
| rendered | - | finished rendering the document |
|
| rendered | - | finished rendering the document |
|
||||||
| rendering-failed | `Error` | failed to render document |
|
| rendering-failed | `Error` | failed to render document |
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
|
|
||||||
```
|
```
|
||||||
TODO: CodeSandbox or JSFiddle
|
TODO: CodeSandbox or JSFiddle
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
MIT License. Please see [LICENSE file](LICENSE) for more information.
|
MIT License. Please see [LICENSE file](LICENSE) for more information.
|
||||||
11
demo/App.vue
11
demo/App.vue
File diff suppressed because one or more lines are too long
|
|
@ -60,6 +60,7 @@
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
|
import controls, { CATALOG } from './constants/controls'
|
||||||
import './assets/iconfont/iconfont.css'
|
import './assets/iconfont/iconfont.css'
|
||||||
import Viewer from './components/Viewer/Viewer.vue'
|
import Viewer from './components/Viewer/Viewer.vue'
|
||||||
import ViewerPageSelector from './components/ViewerPageSelector.vue'
|
import ViewerPageSelector from './components/ViewerPageSelector.vue'
|
||||||
|
|
@ -74,18 +75,7 @@ export default {
|
||||||
controls: {
|
controls: {
|
||||||
type: Array,
|
type: Array,
|
||||||
default: () => {
|
default: () => {
|
||||||
return [
|
return controls
|
||||||
'download',
|
|
||||||
'print',
|
|
||||||
'double', // TODO
|
|
||||||
'fullscreen', // TODO
|
|
||||||
'abort', // TODO
|
|
||||||
'fullpage', // TODO
|
|
||||||
'rotate',
|
|
||||||
'zoom',
|
|
||||||
'catalog',
|
|
||||||
'switchPage',
|
|
||||||
]
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
loadingText: {
|
loadingText: {
|
||||||
|
|
@ -96,6 +86,10 @@ export default {
|
||||||
type: String,
|
type: String,
|
||||||
default: 'Rendering',
|
default: 'Rendering',
|
||||||
},
|
},
|
||||||
|
settings: {
|
||||||
|
type: Object,
|
||||||
|
default: () => ({}),
|
||||||
|
},
|
||||||
},
|
},
|
||||||
components: {
|
components: {
|
||||||
Viewer,
|
Viewer,
|
||||||
|
|
@ -164,6 +158,10 @@ export default {
|
||||||
},
|
},
|
||||||
deep: true,
|
deep: true,
|
||||||
},
|
},
|
||||||
|
mounted() {
|
||||||
|
this.zoom = this.settings.defaultZoom || 100
|
||||||
|
this.catalogVisible = this.controls.includes(CATALOG)
|
||||||
|
},
|
||||||
methods: {
|
methods: {
|
||||||
handleDownload() {
|
handleDownload() {
|
||||||
this.$emit('download', {
|
this.$emit('download', {
|
||||||
|
|
|
||||||
|
|
@ -137,7 +137,7 @@ export default {
|
||||||
viewerStyle() {
|
viewerStyle() {
|
||||||
return {
|
return {
|
||||||
// height: `${this.zoom / NORMAL_RATIO}%`,
|
// height: `${this.zoom / NORMAL_RATIO}%`,
|
||||||
width: `${(this.zoom / NORMAL_RATIO / 100) * this.viewportWidth}px`,
|
width: `${(this.zoom / NORMAL_RATIO / 100) * this.viewportWidth - 40}px`,
|
||||||
transform: `rotate(${this.rotate}deg)`,
|
transform: `rotate(${this.rotate}deg)`,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
@ -159,6 +159,9 @@ export default {
|
||||||
}
|
}
|
||||||
: {}
|
: {}
|
||||||
},
|
},
|
||||||
|
verticalPadding() {
|
||||||
|
return (this.viewportWidth / this.viewportHeight) * 0
|
||||||
|
},
|
||||||
},
|
},
|
||||||
watch: {
|
watch: {
|
||||||
isFullpage(n, o) {
|
isFullpage(n, o) {
|
||||||
|
|
@ -185,6 +188,16 @@ export default {
|
||||||
this.render()
|
this.render()
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
catalogVisible(n, o) {
|
||||||
|
if (n !== o) {
|
||||||
|
this.viewportWidth = n ? this.viewportWidth - 300 : this.viewportWidth + 300
|
||||||
|
}
|
||||||
|
// setTimeout(() => {
|
||||||
|
// this.$nextTick(() => {
|
||||||
|
// this.handleResize()
|
||||||
|
// })
|
||||||
|
// }, 500)
|
||||||
|
},
|
||||||
},
|
},
|
||||||
activated() {
|
activated() {
|
||||||
this.$nextTick(() => {
|
this.$nextTick(() => {
|
||||||
|
|
@ -214,7 +227,8 @@ export default {
|
||||||
contentWindow.print()
|
contentWindow.print()
|
||||||
},
|
},
|
||||||
updateZoomFullpage() {
|
updateZoomFullpage() {
|
||||||
const perViewerHeight = this.viewerContentHeight / this.total
|
const perViewerHeight =
|
||||||
|
(this.viewerContentHeight - this.verticalPadding) / this.total
|
||||||
const rate = this.viewportHeight / (perViewerHeight - MARGIN_OFFSET)
|
const rate = this.viewportHeight / (perViewerHeight - MARGIN_OFFSET)
|
||||||
|
|
||||||
this.$emit('update:zoom', this.zoom * rate)
|
this.$emit('update:zoom', this.zoom * rate)
|
||||||
|
|
@ -223,7 +237,9 @@ export default {
|
||||||
handleViewerScroll(evt) {
|
handleViewerScroll(evt) {
|
||||||
this.isScrolling = true
|
this.isScrolling = true
|
||||||
const yOffset = evt.target.scrollTop
|
const yOffset = evt.target.scrollTop
|
||||||
const perHeight = (this.viewerContentHeight + MARGIN_OFFSET) / this.total
|
const perHeight =
|
||||||
|
(this.viewerContentHeight + MARGIN_OFFSET - this.verticalPadding) /
|
||||||
|
this.total
|
||||||
const halfViewportOffset = (perHeight - this.viewportHeight) / 2
|
const halfViewportOffset = (perHeight - this.viewportHeight) / 2
|
||||||
|
|
||||||
const currentPosition = (yOffset - halfViewportOffset) / perHeight + 1
|
const currentPosition = (yOffset - halfViewportOffset) / perHeight + 1
|
||||||
|
|
@ -299,6 +315,9 @@ export default {
|
||||||
const image = document.createElement('img')
|
const image = document.createElement('img')
|
||||||
image.className = 'placeholder'
|
image.className = 'placeholder'
|
||||||
image.src = URL.createObjectURL(blobData.blob)
|
image.src = URL.createObjectURL(blobData.blob)
|
||||||
|
image.onload = e => {
|
||||||
|
e.target.naturalWidth
|
||||||
|
}
|
||||||
|
|
||||||
return image
|
return image
|
||||||
}
|
}
|
||||||
|
|
@ -334,6 +353,10 @@ export default {
|
||||||
this.$emit('rendered')
|
this.$emit('rendered')
|
||||||
this.$emit('update:isRendering', false)
|
this.$emit('update:isRendering', false)
|
||||||
|
|
||||||
|
if (this.settings?.isFullWidth) {
|
||||||
|
this.$emit('update:zoom', 200)
|
||||||
|
}
|
||||||
|
|
||||||
await this.$nextTick()
|
await this.$nextTick()
|
||||||
this.viewerContentHeight = this.$refs.viewerContent.clientHeight
|
this.viewerContentHeight = this.$refs.viewerContent.clientHeight
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|
|
||||||
|
|
@ -12,10 +12,10 @@ export const SWITCH_PAGE = 'switchPage'
|
||||||
export default [
|
export default [
|
||||||
DOWNLOAD,
|
DOWNLOAD,
|
||||||
PRINT,
|
PRINT,
|
||||||
DOUBLE,
|
DOUBLE, // TODO
|
||||||
FULLSCREEN,
|
FULLSCREEN, // TODO
|
||||||
ABOUT,
|
ABOUT, // TODO
|
||||||
FULLPAGE,
|
FULLPAGE, // TODO
|
||||||
ROTATE,
|
ROTATE,
|
||||||
ZOOM,
|
ZOOM,
|
||||||
CATALOG,
|
CATALOG,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue