Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/components/pdf-viewer/pdf-viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,14 +292,30 @@ export default class PDFViewer extends RoleModelElement {
}
}

downloadPDF() {
async downloadPDF() {
if (!this.src) return

try {
const response = await fetch(this.src)

if (!response.ok) {
throw new Error(`Failed to download: ${response.status} ${response.statusText}`)
}

const contentType = response.headers.get('content-type') || ''
if (!contentType.includes('application/pdf')) {
throw new Error(`Unexpected content type: ${contentType}`)
}

const blob = await response.blob()
const blobUrl = URL.createObjectURL(blob)

const link = document.createElement('a')
link.href = this.src
link.href = blobUrl
link.download = this.src.split('/').pop() || 'document.pdf'
link.click()

URL.revokeObjectURL(blobUrl)
} catch (error) {
console.error('Error downloading PDF:', error)
}
Expand Down