|
@@ -1,25 +1,39 @@
|
|
|
-import { ref, nextTick, computed } from 'vue';
|
|
|
+import { nextTick, computed } from 'vue';
|
|
|
import { useMainStore } from '@/store';
|
|
|
import html2canvas from 'html2canvas';
|
|
|
import jsPDF from 'jspdf';
|
|
|
+import { showMessage } from './common';
|
|
|
|
|
|
const mainStore = useMainStore();
|
|
|
|
|
|
const expanded = computed(() => mainStore.getExpanded);
|
|
|
|
|
|
-export const downloadPDF = async (pdfContent: HTMLElement) => {
|
|
|
-// if (!expanded.value) {
|
|
|
-// expanded.value = true;
|
|
|
-// await nextTick();
|
|
|
-// }
|
|
|
-// showWatermark.value = true;
|
|
|
+export const downloadPDF = async (pdfContent: HTMLElement, callback: () => void) => {
|
|
|
+ if (!expanded.value) {
|
|
|
+ mainStore.setExpanded(true);
|
|
|
+ await nextTick();
|
|
|
+ }
|
|
|
await nextTick();
|
|
|
- const canvas = await html2canvas(pdfContent, {
|
|
|
- scale: 1,
|
|
|
- useCORS: true,
|
|
|
- backgroundColor: '#fff' // 防止透明背景
|
|
|
- });
|
|
|
+ let canvas = null;
|
|
|
+ try {
|
|
|
+ canvas = await html2canvas(pdfContent, {
|
|
|
+ scale: 2,
|
|
|
+ useCORS: true,
|
|
|
+ backgroundColor: '#fff' // 防止透明背景
|
|
|
+ });
|
|
|
+ } catch (error) {
|
|
|
+ callback && callback();
|
|
|
+ mainStore.setExpanded(false);
|
|
|
+ showMessage({
|
|
|
+ type: 'error',
|
|
|
+ message: '下载失败,请稍后再试'
|
|
|
+ });
|
|
|
+ console.error('html2canvas error:', error);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
const imgData = canvas.toDataURL('image/png', 0.7);
|
|
|
+
|
|
|
const pdf = new jsPDF('p', 'mm', 'a4');
|
|
|
// const pdf = new jsPDF("landscape", "mm", "a4"); // 横向 A4
|
|
|
// pdf.setFontSize(100); // 设置当前字体大小为 12(单位:pt)
|
|
@@ -31,8 +45,7 @@ export const downloadPDF = async (pdfContent: HTMLElement) => {
|
|
|
const x = (pageWidth - imgWidth) / 2;
|
|
|
const y = (pageHeight - imgHeight) / 2;
|
|
|
pdf.addImage(imgData, 'JPEG', x, y, imgWidth, imgHeight);
|
|
|
-// showWatermark.value = false;
|
|
|
-// expanded.value = false;
|
|
|
-
|
|
|
+ mainStore.setExpanded(false);
|
|
|
pdf.save('charts-and-table.pdf');
|
|
|
+ callback && callback();
|
|
|
};
|