// 발주서 다이얼로그 컨테이너 DOM → PDF base64 변환 헬퍼. // 영업관리 estimate template2 (lib + sales/estimate/template2/pop/page.tsx 372-438) 패턴 단순화 버전. // - 다이얼로그 안의 양식 컨테이너를 html2canvas-pro 로 캡처 (scale=2, 흰배경, input/textarea → 텍스트) // - A4 비율로 jsPDF 에 그려 datauristring 반환 (메일 첨부) 또는 save (파일 다운로드) export async function generatePurchaseOrderPdf( container: HTMLElement, opts?: { filename?: string; download?: boolean }, ): Promise { const html2canvas = (await import("html2canvas-pro")).default; const jspdfModule: any = await import("jspdf"); const jsPDF = jspdfModule.jsPDF ?? jspdfModule.default; const canvas = await html2canvas(container, { scale: 2, useCORS: true, backgroundColor: "#ffffff", onclone: (doc) => { // input/textarea/select → 텍스트로 교체 (편집 윤곽 제거) doc.querySelectorAll("input, textarea").forEach((el) => { const v = (el as HTMLInputElement | HTMLTextAreaElement).value ?? ""; const span = doc.createElement("span"); span.textContent = v; (span.style as any).whiteSpace = "pre-wrap"; el.parentNode?.replaceChild(span, el); }); // ShadCN Select 트리거 안의 표시 텍스트만 추출 — 화살표/아이콘은 제거 doc.querySelectorAll('[role="combobox"]').forEach((el) => { const text = el.textContent ?? ""; const span = doc.createElement("span"); span.textContent = text.trim(); el.parentNode?.replaceChild(span, el); }); }, }); const pdf = new jsPDF("p", "mm", "a4"); const imgData = canvas.toDataURL("image/jpeg", 0.85); const imgWidth = 210; const pageHeight = 297; const imgHeight = (canvas.height * imgWidth) / canvas.width; let heightLeft = imgHeight; let position = 0; pdf.addImage(imgData, "JPEG", 0, position, imgWidth, imgHeight, undefined, "FAST"); heightLeft -= pageHeight; while (heightLeft > 0) { position = heightLeft - imgHeight; pdf.addPage(); pdf.addImage(imgData, "JPEG", 0, position, imgWidth, imgHeight, undefined, "FAST"); heightLeft -= pageHeight; } if (opts?.download) { pdf.save(opts.filename ?? "purchase_order.pdf"); } return pdf.output("datauristring") as string; }