diff options
Diffstat (limited to 'lib/pdftron/serverSDK')
| -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; }; |
