summaryrefslogtreecommitdiff
path: root/lib/pdftron/serverSDK
diff options
context:
space:
mode:
Diffstat (limited to 'lib/pdftron/serverSDK')
-rw-r--r--lib/pdftron/serverSDK/createBasicContractPdf.ts133
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;
};