pdf.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import { nextTick, computed } from 'vue';
  2. import { useMainStore } from '@/store';
  3. import { ElLoading } from 'element-plus';
  4. import html2canvas from 'html2canvas';
  5. import jsPDF from 'jspdf';
  6. import { showMessage } from './common';
  7. import { uploadFile } from '@/utils/http';
  8. import { savePdf } from '@/utils/api';
  9. const mainStore = useMainStore();
  10. const expanded = computed(() => mainStore.getExpanded);
  11. const phone = computed(() => mainStore.getPhone);
  12. const recordId = computed(() => mainStore.getRecordId);
  13. const isLoadOver = computed(() => mainStore.getIsLoadOver);
  14. /**
  15. * 生成 PDF Blob
  16. */
  17. async function generatePDFBlob(
  18. pdfContent?: HTMLElement,
  19. showLoading = false
  20. ): Promise<Blob | null> {
  21. if (!isLoadOver.value) {
  22. showMessage({ type: 'warning', message: '数据加载中,请稍后再试' });
  23. return null;
  24. }
  25. let loading: any = null;
  26. if (showLoading) {
  27. loading = ElLoading.service({
  28. lock: true,
  29. text: '生成报告中...',
  30. background: 'rgba(0,0,0,0.7)'
  31. });
  32. }
  33. try {
  34. if (!pdfContent) pdfContent = document.getElementById('Record') as HTMLElement;
  35. if (!expanded.value) {
  36. mainStore.setExpanded(true);
  37. await nextTick();
  38. }
  39. await nextTick();
  40. const canvas = await html2canvas(pdfContent, {
  41. scale: 2,
  42. useCORS: true,
  43. backgroundColor: '#fff'
  44. });
  45. const canvasWidth = canvas.width;
  46. const canvasHeight = canvas.height;
  47. const pdf = new jsPDF('p', 'mm', 'a4');
  48. const pageWidth = pdf.internal.pageSize.getWidth();
  49. const pageHeight = pdf.internal.pageSize.getHeight();
  50. const ratio = pageWidth / canvasWidth;
  51. const pagePixelHeight = pageHeight / ratio;
  52. let positionY = 0;
  53. while (positionY < canvasHeight) {
  54. const h = Math.min(pagePixelHeight, canvasHeight - positionY);
  55. const pageCanvas = document.createElement('canvas');
  56. pageCanvas.width = canvasWidth;
  57. pageCanvas.height = h;
  58. const ctx = pageCanvas.getContext('2d');
  59. ctx?.drawImage(canvas, 0, positionY, canvasWidth, h, 0, 0, canvasWidth, h);
  60. const imgData = pageCanvas.toDataURL('image/jpeg', 0.95);
  61. pdf.addImage(imgData, 'JPEG', 0, 0, pageWidth, h * ratio);
  62. positionY += h;
  63. if (positionY < canvasHeight) pdf.addPage();
  64. }
  65. return pdf.output('blob');
  66. } catch (error) {
  67. console.error(error);
  68. showMessage({ type: 'error', message: '生成失败,请稍后再试' });
  69. return null;
  70. } finally {
  71. mainStore.setExpanded(false);
  72. setTimeout(() => loading && loading.close(), 500);
  73. }
  74. }
  75. /**
  76. * 下载文件
  77. */
  78. function triggerDownload(blob: Blob, filename = 'analysis.pdf') {
  79. const url = URL.createObjectURL(blob);
  80. const a = document.createElement('a');
  81. a.href = url;
  82. a.download = filename;
  83. a.click();
  84. URL.revokeObjectURL(url);
  85. }
  86. /**
  87. * 上传文件
  88. */
  89. async function uploadPDFFile(blob: Blob) {
  90. const file = new File([blob], 'analysis.pdf', { type: 'application/pdf' });
  91. const uploadRes = await uploadFile(file, { source: 'media', mediaType: 'file' });
  92. const pdfUrl = uploadRes.data.url;
  93. await savePdf({
  94. phone: phone.value,
  95. id: recordId.value,
  96. pdfUrl
  97. });
  98. return pdfUrl;
  99. }
  100. /**
  101. * 统一入口:生成 PDF
  102. * @param pdfContent - PDF内容DOM
  103. * @param mode - "manual" 手动下载 | "auto" 自动下载并上传
  104. */
  105. export async function handlePDF(pdfContent?: HTMLElement, mode: 'manual' | 'auto' = 'manual') {
  106. const blob = await generatePDFBlob(pdfContent, true);
  107. if (!blob) return;
  108. // 下载
  109. triggerDownload(blob);
  110. // 自动模式下再上传
  111. if (mode === 'auto') {
  112. try {
  113. await uploadPDFFile(blob);
  114. // showMessage({ type: 'success', message: '报告已下载并上传成功' });
  115. } catch (err) {
  116. // showMessage({ type: 'error', message: '报告上传失败' });
  117. throw err;
  118. }
  119. }
  120. }