fix(WEB): DocumentViewer PDF 이미지 누락 해결

- PDF 렌더링 시 이미지 누락 이슈 수정
- 해결 과정 문서 추가

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
유병철
2026-02-11 20:53:06 +09:00
parent 113d82c254
commit 4decb99856
2 changed files with 172 additions and 1 deletions

View File

@@ -161,6 +161,49 @@ export function DocumentViewer({
return clone;
};
// 이미지를 base64 data URL로 인라인 변환 (PDF 이미지 누락 해결)
const convertImagesToBase64 = async (original: HTMLElement, clone: HTMLElement): Promise<void> => {
const originalImages = Array.from(original.querySelectorAll('img'));
const clonedImages = Array.from(clone.querySelectorAll('img'));
await Promise.all(
originalImages.map(async (img, index) => {
const clonedImg = clonedImages[index];
if (!clonedImg) return;
const src = img.src;
if (!src || src.startsWith('data:')) return;
try {
const response = await fetch(src);
const blob = await response.blob();
const dataUrl = await new Promise<string>((resolve, reject) => {
const reader = new FileReader();
reader.onloadend = () => resolve(reader.result as string);
reader.onerror = reject;
reader.readAsDataURL(blob);
});
clonedImg.setAttribute('src', dataUrl);
} catch {
try {
if (img.complete && img.naturalWidth > 0) {
const canvas = document.createElement('canvas');
canvas.width = img.naturalWidth;
canvas.height = img.naturalHeight;
const ctx = canvas.getContext('2d');
if (ctx) {
ctx.drawImage(img, 0, 0);
clonedImg.setAttribute('src', canvas.toDataURL('image/png'));
}
}
} catch {
// 변환 실패 시 원본 src 유지
}
}
})
);
};
// 계산된 스타일을 인라인으로 적용
const applyStyles = (element: HTMLElement, computedStyle: CSSStyleDeclaration) => {
const importantStyles = [
@@ -204,8 +247,9 @@ export function DocumentViewer({
return;
}
// 인라인 스타일 적용된 HTML 복제
// 인라인 스타일 적용된 HTML 복제 + 이미지 base64 인라인 변환
const clonedElement = cloneWithInlineStyles(printAreaEl);
await convertImagesToBase64(printAreaEl, clonedElement);
const html = clonedElement.outerHTML;
// 서버 API 호출