|
11 | 11 | v-loading="loading" |
12 | 12 | style="height: calc(70vh - 150px); overflow-y: auto; display: flex; justify-content: center" |
13 | 13 | > |
| 14 | + <div ref="cloneContainerRef" style="width: 100%"></div> |
14 | 15 | <div ref="svgContainerRef"></div> |
15 | 16 | </div> |
16 | 17 | <template #footer> |
@@ -39,42 +40,82 @@ import html2Canvas from 'html2canvas' |
39 | 40 | import { jsPDF } from 'jspdf' |
40 | 41 | const loading = ref<boolean>(false) |
41 | 42 | const svgContainerRef = ref() |
| 43 | +const cloneContainerRef = ref() |
42 | 44 | const dialogVisible = ref<boolean>(false) |
43 | 45 | const open = (element: HTMLElement | null) => { |
44 | 46 | dialogVisible.value = true |
45 | 47 | loading.value = true |
46 | 48 | if (!element) { |
47 | 49 | return |
48 | 50 | } |
49 | | - setTimeout(() => { |
50 | | - nextTick(() => { |
51 | | - htmlToImage |
52 | | - .toSvg(element, { pixelRatio: 1, quality: 1 }) |
53 | | - .then((dataUrl) => { |
54 | | - return fetch(dataUrl) |
55 | | - .then((response) => { |
56 | | - return response.text() |
57 | | - }) |
58 | | - .then((text) => { |
59 | | - const parser = new DOMParser() |
60 | | - const svgDoc = parser.parseFromString(text, 'image/svg+xml') |
61 | | - const svgElement = svgDoc.documentElement |
62 | | - svgContainerRef.value.appendChild(svgElement) |
63 | | - svgContainerRef.value.style.height = svgElement.scrollHeight + 'px' |
64 | | - }) |
65 | | - }) |
66 | | - .finally(() => { |
67 | | - loading.value = false |
68 | | - }) |
| 51 | + const cElement = element.cloneNode(true) as HTMLElement |
| 52 | + const images = cElement.querySelectorAll('img') |
| 53 | + const loadPromises = Array.from(images).map((img) => { |
| 54 | + if (!img.src.startsWith(window.origin) && img.src.startsWith('http')) { |
| 55 | + img.src = `${window.MaxKB.prefix}/api/resource_proxy?url=${encodeURIComponent(img.src)}` |
| 56 | + } |
| 57 | + img.setAttribute('onerror', '') |
| 58 | + return new Promise((resolve) => { |
| 59 | + // 已加载完成的图片直接 resolve |
| 60 | + if (img.complete) { |
| 61 | + resolve({ img, success: img.naturalWidth > 0 }) |
| 62 | + return |
| 63 | + } |
| 64 | +
|
| 65 | + // 未加载完成的图片监听事件 |
| 66 | + img.onload = () => resolve({ img, success: true }) |
| 67 | + img.onerror = () => resolve({ img, success: false }) |
69 | 68 | }) |
70 | | - }, 1) |
| 69 | + }) |
| 70 | + Promise.all(loadPromises).finally(() => { |
| 71 | + setTimeout(() => { |
| 72 | + nextTick(() => { |
| 73 | + cloneContainerRef.value.appendChild(cElement) |
| 74 | + htmlToImage |
| 75 | + .toSvg(cElement, { |
| 76 | + pixelRatio: 1, |
| 77 | + quality: 1, |
| 78 | + onImageErrorHandler: ( |
| 79 | + event: Event | string, |
| 80 | + source?: string, |
| 81 | + lineno?: number, |
| 82 | + colno?: number, |
| 83 | + error?: Error, |
| 84 | + ) => { |
| 85 | + console.log(event, source, lineno, colno, error) |
| 86 | + }, |
| 87 | + }) |
| 88 | + .then((dataUrl) => { |
| 89 | + return fetch(dataUrl) |
| 90 | + .then((response) => { |
| 91 | + return response.text() |
| 92 | + }) |
| 93 | + .then((text) => { |
| 94 | + const parser = new DOMParser() |
| 95 | + const svgDoc = parser.parseFromString(text, 'image/svg+xml') |
| 96 | + cloneContainerRef.value.style.display = 'none' |
| 97 | + const svgElement = svgDoc.documentElement |
| 98 | + svgContainerRef.value.appendChild(svgElement) |
| 99 | + svgContainerRef.value.style.height = svgElement.scrollHeight + 'px' |
| 100 | + }) |
| 101 | + }) |
| 102 | + .finally(() => { |
| 103 | + loading.value = false |
| 104 | + }) |
| 105 | + .catch((e) => { |
| 106 | + loading.value = false |
| 107 | + }) |
| 108 | + }) |
| 109 | + }, 1) |
| 110 | + }) |
71 | 111 | } |
72 | 112 |
|
73 | 113 | const exportPDF = () => { |
74 | 114 | loading.value = true |
75 | 115 | setTimeout(() => { |
76 | 116 | nextTick(() => { |
77 | 117 | html2Canvas(svgContainerRef.value, { |
| 118 | + scale: 2, |
78 | 119 | logging: false, |
79 | 120 | }) |
80 | 121 | .then((canvas) => { |
|
0 commit comments