summaryrefslogtreecommitdiff
path: root/lib/pdftron/serverSDK/createReport.ts
blob: afcec7a24b2816d1a1746e4264b5709a135e6a61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
const { PDFNet } = require("@pdftron/pdfnet-node");
const path = require("path");

type CreateReport = (
  coverPage: Buffer,
  reportTempPath: string,
  reportDatas: {
    [key: string]: any;
  }[]
) => Promise<{
  result: boolean;
  buffer?: ArrayBuffer;
  error?: any;
}>;

// 웹 경로를 실제 파일 시스템 경로로 변환
function convertWebPathToFilePath(webPath: string): string {
  const isProduction = process.env.NODE_ENV === "production";
  const nasPath = process.env.NAS_PATH || "/evcp_nas";
  
  // /api/files/vendorFormData/xxx.docx → vendorFormData/xxx.docx
  // /vendorFormData/xxx.docx → vendorFormData/xxx.docx
  let relativePath = webPath
    .replace(/^\/api\/files\//, '')  // 운영 환경 경로 제거
    .replace(/^\//, '');              // 개발 환경 슬래시 제거
  
  if (isProduction) {
    // 운영: /evcp_nas/vendorFormData/xxx.docx
    return path.join(nasPath, relativePath);
  } else {
    // 개발: public/vendorFormData/xxx.docx
    return path.join(process.cwd(), "public", relativePath);
  }
}

export const createReport: CreateReport = async (
  coverPage,
  reportTempPath,
  reportDatas
) => {
  const main = async () => {
    await PDFNet.initialize(process.env.NEXT_PUBLIC_PDFTRON_SERVER_KEY);

    const mainDoc = await PDFNet.PDFDoc.create();
    const buf = await PDFNet.Convert.office2PDFBuffer(coverPage);
    const coverPDFDoc = await PDFNet.PDFDoc.createFromBuffer(buf);

    const options = new PDFNet.Convert.OfficeToPDFOptions();

    await mainDoc.insertPages(
      (await mainDoc.getPageCount()) + 1,
      coverPDFDoc,
      1,
      await coverPDFDoc.getPageCount(),
      PDFNet.PDFDoc.InsertFlag.e_none
    );

    for (const reportData of reportDatas) {
      const resportDataJson = JSON.stringify(reportData);

      // 웹 경로를 실제 파일 시스템 경로로 변환
      const actualFilePath = convertWebPathToFilePath(reportTempPath);
      
      console.log(`📄 템플릿 파일 경로 변환:`);
      console.log(`  - 웹 경로: ${reportTempPath}`);
      console.log(`  - 실제 경로: ${actualFilePath}`);

      const templateDoc = await PDFNet.Convert.createOfficeTemplateWithPath(
        actualFilePath,
        options
      );

      const pdfdoc = await templateDoc.fillTemplateJson(resportDataJson);

      await mainDoc.insertPages(
        (await mainDoc.getPageCount()) + 1,
        pdfdoc,
        1,
        await pdfdoc.getPageCount(),
        PDFNet.PDFDoc.InsertFlag.e_none
      );
    }

    // await mainDoc.save("test1.pdf", PDFNet.SDFDoc.SaveOptions.e_linearized);

    const buffer = await mainDoc.saveMemoryBuffer(
      PDFNet.SDFDoc.SaveOptions.e_linearized
    );

    return {
      result: true,
      buffer,
    };
  };

  const result = await PDFNet.runWithCleanup(
    main,
    process.env.NEXT_PUBLIC_PDFTRON_SERVER_KEY
  )
    .catch((err: any) => {
      return {
        result: false,
        error: err,
      };
    })
    .then(async (data: any) => {
      return data;
    });

  return result;
};