Skip to content

Instantly share code, notes, and snippets.

@AlexandruMiricioiu
Last active December 7, 2022 11:34
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlexandruMiricioiu/0ac17faf92e5dd72eabdae21bdbbe4a2 to your computer and use it in GitHub Desktop.
Save AlexandruMiricioiu/0ac17faf92e5dd72eabdae21bdbbe4a2 to your computer and use it in GitHub Desktop.
Pdf Embed Api
<template>
<div>
<div ref="pdfContainer" style="height: 800px"></div>
<button
@click="
renderPdf(
'https://documentcloud.adobe.com/view-sdk-demo/PDFs/Bodea Brochure.pdf',
'test.pdf'
)
"
>
Load Pdf
</button>
</div>
</template>
<script>
export default {
mounted() {
document.addEventListener('adobe_dc_view_sdk.ready', () => {
this.adobeApiReady = true
})
},
data() {
return {
adobeApiReady: false,
previewFilePromise: null
}
},
methods: {
nextPage() {
this.previewFilePromise.then(adobeViewer => {
adobeViewer.getAPIs().then(apis => {
apis.getCurrentPage()
.then(currentPage => apis.gotoLocation(currentPage + 1))
.catch(error => console.error(error))
})
})
},
previousPage() {
this.previewFilePromise.then(adobeViewer => {
adobeViewer.getAPIs().then(apis => {
apis.getCurrentPage()
.then(currentPage => {
if (currentPage > 1) {
return apis.gotoLocation(currentPage - 1)
}
})
.catch(error => console.error(error))
})
})
},
zoomIn() {
this.previewFilePromise.then(adobeViewer => {
adobeViewer.getAPIs().then(apis => {
apis.getZoomAPIs().zoomIn()
.catch(error => console.error(error))
})
})
},
zoomOut() {
this.previewFilePromise.then(adobeViewer => {
adobeViewer.getAPIs().then(apis => {
apis.getZoomAPIs().zoomOut()
.catch(error => console.error(error))
})
})
},
renderPdf(url, fileName) {
if (!this.adobeApiReady) {
return
}
const previewConfig = {
defaultViewMode: 'FIT_WIDTH',
showAnnotationTools: false
}
this.$refs.pdfContainer.innerHTML = ""
let viewer = document.createElement("div")
viewer.id = "viewer"
this.$refs.pdfContainer.appendChild(viewer)
let adobeDCView = new AdobeDC.View({
clientId: "API_KEY",
divId: "viewer"
})
this.previewFilePromise = adobeDCView.previewFile({
content: {
location: {
url: url,
}
},
metaData: {
fileName: fileName,
id: fileName
},
}, previewConfig)
}
}
}
</script>
@samiam9297
Copy link

Great job @AlexandruMiricioiu!

Future people who land here, if you're pre-loading Adobe's script, you might need to do something like this incase you miss the event:

...
    watch: {
        adobeApiReady: {
            handler () {
                this.renderPdf()
            }
        }
    },
    mounted () {
        if (window.AdobeDC) {
            this.adobeApiReady = true
        } else {
            document.addEventListener('adobe_dc_view_sdk.ready', () => {
                this.adobeApiReady = true
            })
        }

    }
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment