เพิ่ม โหลด pdf
This commit is contained in:
parent
f74fcdf9e0
commit
3b1e85168b
5 changed files with 94 additions and 16 deletions
9
src/api/exam/api.report.ts
Normal file
9
src/api/exam/api.report.ts
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
/**
|
||||
* api รายงานทั้งหมด
|
||||
*/
|
||||
import env from '../index'
|
||||
const reportTemplate = `${env.API_REPORT_TEMPLATE_URI}`
|
||||
|
||||
export default {
|
||||
reportTemplate
|
||||
}
|
||||
|
|
@ -10,7 +10,9 @@ const config = ref<any>({
|
|||
// API_URI: 'https://localhost:7007/api/v1',
|
||||
API_URI: apiUrlConfig,
|
||||
API_URI_V2: apiUrlConfigV2,
|
||||
MEET_URI: 'meet.frappet.com'
|
||||
MEET_URI: 'meet.frappet.com',
|
||||
API_REPORT_TEMPLATE_URI:
|
||||
"https://report-server.frappet.synology.me/api/v1/report-template",
|
||||
},
|
||||
test: {
|
||||
API_URI: 'http://localhost:5010/api/v1',
|
||||
|
|
@ -28,11 +30,14 @@ const config = ref<any>({
|
|||
const API_URI = ref<string>(config.value[env.value].API_URI)
|
||||
const API_URI_V2 = ref<string>(config.value[env.value].API_URI_V2)
|
||||
const MEET_URI = ref<string>(config.value[env.value].MEET_URI)
|
||||
|
||||
const API_REPORT_TEMPLATE_URI = ref<string>(
|
||||
config.value[env.value].API_REPORT_TEMPLATE_URI
|
||||
);
|
||||
export default {
|
||||
env: env.value,
|
||||
config: config.value,
|
||||
API_URI: API_URI.value,
|
||||
API_URI_V2: API_URI_V2.value,
|
||||
MEET_URI: MEET_URI.value
|
||||
MEET_URI: MEET_URI.value,
|
||||
API_REPORT_TEMPLATE_URI: API_REPORT_TEMPLATE_URI.value,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,10 +2,12 @@
|
|||
|
||||
import metadata from './api/exam/api.metadata'
|
||||
import candidate from './api/exam/api.candidate'
|
||||
import report from './api/exam/api.report'
|
||||
|
||||
const API = {
|
||||
...metadata,
|
||||
...candidate
|
||||
...candidate,
|
||||
...report
|
||||
}
|
||||
|
||||
export default {
|
||||
|
|
|
|||
|
|
@ -159,13 +159,16 @@
|
|||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { onMounted, ref } from 'vue'
|
||||
import { useQuasar } from 'quasar'
|
||||
import { useRoute } from 'vue-router'
|
||||
import { useCounterMixin } from '@/stores/mixin'
|
||||
import { useDataStore } from '@/stores/data'
|
||||
import { onMounted, ref } from 'vue'
|
||||
|
||||
import http from '@/plugins/http'
|
||||
import config from '@/app.config'
|
||||
import { useQuasar } from 'quasar'
|
||||
import { useCounterMixin } from '@/stores/mixin'
|
||||
import { useDataStore } from '@/stores/data'
|
||||
|
||||
import genReport from '@/plugins/genreport'
|
||||
|
||||
const $q = useQuasar()
|
||||
const route = useRoute()
|
||||
|
|
@ -246,19 +249,17 @@ const fetchStatus = async () => {
|
|||
const download = async () => {
|
||||
loaderPage(true)
|
||||
await http
|
||||
.get(config.API.candidateReport(candidateId.value), {
|
||||
responseType: 'blob'
|
||||
})
|
||||
.then((res) => {
|
||||
const data = res.data
|
||||
downloadFilePDF(data, `ใบสมัครสอบ.pdf`)
|
||||
.get(config.API.candidateReport(candidateId.value))
|
||||
.then(async (res) => {
|
||||
const data = await res.data.result
|
||||
await genReport(data, `ใบสมัครสอบ`, 'pdf')
|
||||
loaderPage(false)
|
||||
})
|
||||
.catch((e) => {
|
||||
messageError($q, e)
|
||||
})
|
||||
.finally(() => {
|
||||
loaderPage(false)
|
||||
})
|
||||
.finally(() => {})
|
||||
}
|
||||
const downloadFilePDF = async (res: string, fileName: string) => {
|
||||
const link = document.createElement('a')
|
||||
|
|
|
|||
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