blob: 706508e645aaafb891c9f72d5d32a8439dd9dabc (
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
|
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,
templateData: {
[key: string]: any;
}
) => Promise<{
result: boolean;
buffer?: ArrayBuffer;
error?: any;
}>;
export const createBasicContractPdf: CreateBasicContractPdf = async (
templateBuffer,
templateData
) => {
const result = await PDFNet.runWithCleanup(async () => {
console.log("π PDFTron κΈ°λ³Έκ³μ½μ PDF λ³ν μμ");
console.log("π ν
νλ¦Ώ λ°μ΄ν°:", JSON.stringify(templateData, null, 2));
// μμ νμΌ μμ±
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("π ν
νλ¦Ώ λ³μ μΉν μμ");
const template = await PDFNet.Convert.createOfficeTemplateWithPath(
tempDocxPath
);
resultDoc = await template.fillTemplateJson(
JSON.stringify(templateData)
);
console.log("β
ν
νλ¦Ώ λ³μ μΉν λ° PDF λ³ν μλ£");
} else {
console.log("π λ¨μ PDF λ³ν μν");
resultDoc = await PDFNet.Convert.office2PDF(tempDocxPath);
console.log("β
λ¨μ PDF λ³ν μλ£");
}
const buffer = await resultDoc.saveMemoryBuffer(
PDFNet.SDFDoc.SaveOptions.e_linearized
);
return {
result: true,
buffer,
};
} finally {
// μμ νμΌ μ 리
await cleanup();
}
},
process.env.NEXT_PUBLIC_PDFTRON_SERVER_KEY
);
return result;
};
|