DownloadPdfBtn.client.vue 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <template>
  2. <v-btn :disabled="true" variant="outlined" @click="onClick">
  3. {{$t('download_pdf')}}
  4. </v-btn>
  5. </template>
  6. <script setup lang="ts">
  7. import { target } from '@vue/devtools-shared'
  8. const appStore = useAppStore()
  9. const onClick = async () => {
  10. // if (appStore.altchaPayload) {
  11. await submit()
  12. // } else {
  13. // console.log('missing payload')
  14. // }
  15. }
  16. const submit = async () => {
  17. const url = 'https://api.ogene.fr/api/download-cv';
  18. const headers = {
  19. 'Content-Type': 'application/ld+json'
  20. };
  21. const body = {
  22. "altchaPayload": appStore.altchaPayload
  23. };
  24. // try {
  25. const response = await fetch(url, {
  26. method: 'POST',
  27. headers: headers,
  28. body: JSON.stringify(body)
  29. });
  30. const base64 = await toBase64(await response.text())
  31. const binary = atob(base64.split(',')[1]);
  32. const array = [];
  33. for (let i = 0; i < binary.length; i++) {
  34. array.push(binary.charCodeAt(i));
  35. }
  36. // Create a blob object
  37. const blob = new Blob([new Uint8Array(array)], { type: 'application/pdf' });
  38. const blobUrl = URL.createObjectURL(blob);
  39. window.open(blobUrl, '_blank');
  40. // } catch (error) {
  41. // console.error('There was a problem with the fetch operation: ', error);
  42. // }
  43. }
  44. const newBlob = (data: BlobPart, filetype: string = 'image/jpeg'): Blob => {
  45. return new Blob([data], { type: filetype })
  46. }
  47. const blobToBase64 = async (blob: Blob): Promise<string> => {
  48. return new Promise((resolve, _reject) => {
  49. const reader = new FileReader()
  50. reader.onloadend = () => resolve(reader.result as string)
  51. reader.readAsDataURL(blob)
  52. })
  53. }
  54. const toBase64 = async (data: BlobPart) => {
  55. const blob = newBlob(data)
  56. return (await blobToBase64(blob)) ?? ''
  57. }
  58. </script>
  59. <style scoped lang="scss">
  60. </style>