diff options
| author | dujinkim <dujin.kim@dtsolution.co.kr> | 2025-08-11 00:19:29 +0000 |
|---|---|---|
| committer | dujinkim <dujin.kim@dtsolution.co.kr> | 2025-08-11 00:19:29 +0000 |
| commit | 71f4e15800b0cf771d1dddab6cc46fc7c2a17c51 (patch) | |
| tree | 20d7d54358ec6bfcd81a69de93b75b156924add6 /lib/pdftron | |
| parent | 7d39ab4b1e9f92d14d640506d9639a4b054154a9 (diff) | |
(최겸) PQ 기본계약DocxToPdf 한글화 적용
Diffstat (limited to 'lib/pdftron')
| -rw-r--r-- | lib/pdftron/serverSDK/createBasicContractPdf.ts | 133 |
1 files changed, 35 insertions, 98 deletions
diff --git a/lib/pdftron/serverSDK/createBasicContractPdf.ts b/lib/pdftron/serverSDK/createBasicContractPdf.ts index a2e0b350..706508e6 100644 --- a/lib/pdftron/serverSDK/createBasicContractPdf.ts +++ b/lib/pdftron/serverSDK/createBasicContractPdf.ts @@ -1,4 +1,7 @@ const { PDFNet } = require("@pdftron/pdfnet-node"); +const fs = require('fs').promises; +const path = require('path'); +import { file as tmpFile } from "tmp-promise"; type CreateBasicContractPdf = ( templateBuffer: Buffer, @@ -15,99 +18,43 @@ export const createBasicContractPdf: CreateBasicContractPdf = async ( templateBuffer, templateData ) => { - const main = async () => { - await PDFNet.initialize(process.env.NEXT_PUBLIC_PDFTRON_SERVER_KEY); + const result = await PDFNet.runWithCleanup(async () => { console.log("🔄 PDFTron 기본계약서 PDF 변환 시작"); console.log("📝 템플릿 데이터:", JSON.stringify(templateData, null, 2)); - // 템플릿 데이터가 있는 경우 변수 치환 후 PDF 변환 - if (Object.keys(templateData).length > 0) { - console.log("🔄 템플릿 변수 치환 시작"); - - try { - // createReport.ts 방식처럼 템플릿 변수 치환 (UTF-8 인코딩 지원) - const options = new PDFNet.Convert.OfficeToPDFOptions(); - - // UTF-8 인코딩 명시 설정 시도 - try { - options.setCharset("UTF-8"); - console.log("✅ UTF-8 인코딩 설정 완료"); - } catch (charsetError) { - console.warn("⚠️ UTF-8 인코딩 설정 실패, 기본 설정 사용:", charsetError); - } - - // 템플릿 데이터를 UTF-8로 명시적으로 인코딩 - const templateDataJson = JSON.stringify(templateData, null, 2); - const utf8TemplateData = Buffer.from(templateDataJson, 'utf8').toString('utf8'); - console.log("📝 UTF-8 인코딩된 템플릿 데이터:", utf8TemplateData); - - const tempPath = `/tmp/temp_template_${Date.now()}.docx`; - - // 파일도 UTF-8로 저장 (바이너리 데이터는 그대로 유지) - require('fs').writeFileSync(tempPath, templateBuffer, { encoding: null }); // 바이너리로 저장 + // 임시 파일 생성 + const { path: tempDocxPath, cleanup } = await tmpFile({ + postfix: ".docx", + }); + + try { + // 템플릿 버퍼를 임시 파일로 저장 + await fs.writeFile(tempDocxPath, templateBuffer); + + let resultDoc; + + // 템플릿 데이터가 있는 경우 변수 치환, 없으면 단순 변환 + if (templateData && Object.keys(templateData).length > 0) { + console.log("🔄 템플릿 변수 치환 시작"); - // Office 템플릿 생성 및 변수 치환 - const templateDoc = await PDFNet.Convert.createOfficeTemplateWithPath( - tempPath, - options + const template = await PDFNet.Convert.createOfficeTemplateWithPath( + tempDocxPath ); - - const filledDoc = await templateDoc.fillTemplateJson(utf8TemplateData); - - // 임시 파일 삭제 - require('fs').unlinkSync(tempPath); - - console.log("✅ 템플릿 변수 치환 및 PDF 변환 완료"); - - const buffer = await filledDoc.saveMemoryBuffer( - PDFNet.SDFDoc.SaveOptions.e_linearized + resultDoc = await template.fillTemplateJson( + JSON.stringify(templateData) ); - - return { - result: true, - buffer, - }; - } catch (templateError) { - console.warn("⚠️ 템플릿 변수 치환 실패, 기본 변환 수행:", templateError); - - // 템플릿 처리 실패 시 기본 PDF 변환만 수행 (UTF-8 인코딩 적용) - const fallbackOptions = new PDFNet.Convert.OfficeToPDFOptions(); - try { - fallbackOptions.setCharset("UTF-8"); - } catch (charsetError) { - console.warn("⚠️ 폴백 UTF-8 인코딩 설정 실패:", charsetError); - } + console.log("✅ 템플릿 변수 치환 및 PDF 변환 완료"); + } else { + console.log("📄 단순 PDF 변환 수행"); - const buf = await PDFNet.Convert.office2PDFBuffer(templateBuffer, fallbackOptions); - const templateDoc = await PDFNet.PDFDoc.createFromBuffer(buf); + resultDoc = await PDFNet.Convert.office2PDF(tempDocxPath); - const buffer = await templateDoc.saveMemoryBuffer( - PDFNet.SDFDoc.SaveOptions.e_linearized - ); - - return { - result: true, - buffer, - }; + console.log("✅ 단순 PDF 변환 완료"); } - } else { - // 템플릿 데이터가 없는 경우 단순 변환 (UTF-8 인코딩 적용) - console.log("📄 단순 PDF 변환 수행 (UTF-8 인코딩)"); - - const simpleOptions = new PDFNet.Convert.OfficeToPDFOptions(); - try { - simpleOptions.setCharset("UTF-8"); - console.log("✅ 단순 변환 UTF-8 인코딩 설정 완료"); - } catch (charsetError) { - console.warn("⚠️ 단순 변환 UTF-8 인코딩 설정 실패:", charsetError); - } - - const buf = await PDFNet.Convert.office2PDFBuffer(templateBuffer, simpleOptions); - const templateDoc = await PDFNet.PDFDoc.createFromBuffer(buf); - - const buffer = await templateDoc.saveMemoryBuffer( + + const buffer = await resultDoc.saveMemoryBuffer( PDFNet.SDFDoc.SaveOptions.e_linearized ); @@ -115,23 +62,13 @@ export const createBasicContractPdf: CreateBasicContractPdf = async ( result: true, buffer, }; + + } finally { + // 임시 파일 정리 + await cleanup(); } - }; - - const result = await PDFNet.runWithCleanup( - main, + }, process.env.NEXT_PUBLIC_PDFTRON_SERVER_KEY - ) - .catch((err: any) => { - console.error("❌ PDFTron 기본계약서 PDF 변환 오류:", err); - return { - result: false, - error: err, - }; - }) - .then(async (data: any) => { - return data; - }); - + ); return result; }; |
