เพิ่ม โหลด pdf
This commit is contained in:
parent
f74fcdf9e0
commit
3b1e85168b
5 changed files with 94 additions and 16 deletions
61
src/plugins/genreport.ts
Normal file
61
src/plugins/genreport.ts
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
import axios from 'axios'
|
||||
import config from '@/app.config'
|
||||
|
||||
import { useQuasar } from 'quasar'
|
||||
import { useCounterMixin } from '@/stores/mixin'
|
||||
import { useDataStore } from '@/stores/data'
|
||||
|
||||
const $q = useQuasar()
|
||||
|
||||
const mixin = useCounterMixin()
|
||||
const dataStore = useDataStore()
|
||||
const { messageError } = mixin
|
||||
const { loaderPage } = dataStore
|
||||
|
||||
async function genReport(data: any, fileName: string, type: string = 'docx') {
|
||||
loaderPage(true)
|
||||
await axios
|
||||
.post(`${config.API.reportTemplate}/docx`, data, {
|
||||
headers:
|
||||
type == 'docx'
|
||||
? {
|
||||
accept: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
||||
'content-Type': 'application/json'
|
||||
}
|
||||
: {
|
||||
accept: 'application/pdf',
|
||||
'content-Type': 'application/json'
|
||||
},
|
||||
responseType: 'arraybuffer'
|
||||
})
|
||||
.then((res) => {
|
||||
const data = res.data
|
||||
if (data) {
|
||||
// สร้าง Blob จาก array buffer
|
||||
const blob = new Blob([data], {
|
||||
type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
|
||||
})
|
||||
|
||||
// สร้าง URL สำหรับไฟล์ Blob
|
||||
const url = URL.createObjectURL(blob)
|
||||
|
||||
// สร้างลิงก์เพื่อดาวน์โหลดไฟล์
|
||||
const link = document.createElement('a')
|
||||
link.href = url
|
||||
link.download = `${fileName}.${type === 'docx' ? 'docx' : 'pdf'}` // กำหนดชื่อไฟล์ที่จะดาวน์โหลด
|
||||
document.body.appendChild(link)
|
||||
link.click()
|
||||
|
||||
// ลบ URL ที่สร้างขึ้นหลังจากใช้งาน
|
||||
URL.revokeObjectURL(url)
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
messageError($q, err)
|
||||
})
|
||||
.finally(() => {
|
||||
loaderPage(false)
|
||||
})
|
||||
}
|
||||
|
||||
export default genReport
|
||||
Loading…
Add table
Add a link
Reference in a new issue