Product: Server SDK
Product Version: 11.4.0
Please give a brief summary of your issue:
I’m creating a PDF file using the Server SDK, but the function stops running.
(Think of this as an email subject)
Please describe your issue and provide steps to reproduce it:
export const createDocs: CreateDocs = async (
fileDatas,
additionalFiles,
additialFileOption,
apiKey
) => {
const main = async () => {
try {
const { path: tempDocxPath, cleanup } = await tmpFile({
postfix: ".docx",
});
await PDFNet.initialize(process.env.NEXT_PUBLIC_PDFTRON_SERVER_KEY);
const mainDoc = await PDFNet.PDFDoc.create();
for (const fileData of fileDatas) {
const { fileExt, fileBuffer, reportData, annotList, approvalFormData } =
fileData;
if (docxExts.includes(fileExt)) {
try {
await fs.writeFile(tempDocxPath, fileBuffer);
let tempDoc;
if (reportData && Object.keys(reportData).length > 0) {
const template =
await PDFNet.Convert.createOfficeTemplateWithPath(tempDocxPath);
tempDoc = await template.fillTemplateJson(
JSON.stringify(reportData)
);
} else {
tempDoc = await PDFNet.Convert.office2PDF(tempDocxPath);
}
for (const annot of annotList) {
const { annotXfdf } = annot;
const fdfDoc = await PDFNet.FDFDoc.createFromXFDF(annotXfdf);
await tempDoc.fdfMerge(fdfDoc);
}
await insertSignFormField(tempDoc, approvalFormData);
await tempDoc.flattenAnnotations();
if (additialFileOption) {
const {
fontSize,
color,
opacity,
text,
rotation,
fontFamily,
split,
} = additialFileOption;
if (split) {
await createWatermarkWithTempPng(tempDoc, {
text,
fontSize,
color,
opacity,
rotation,
fontFamily,
});
} else {
await createWatermarksWithTempPng(tempDoc, {
text,
fontSize,
color,
opacity,
rotation,
fontFamily,
});
}
}
await mainDoc.insertPages(
(await mainDoc.getPageCount()) + 1,
tempDoc,
1,
await tempDoc.getPageCount(),
PDFNet.PDFDoc.InsertFlag.e_none
);
} catch (err) {
console.error({ err });
} finally {
await cleanup();
}
} else {
const pdfDoc = await PDFNet.PDFDoc.createFromBuffer(fileBuffer);
for (const annot of annotList) {
const { annotXfdf } = annot;
const fdfDoc = await PDFNet.FDFDoc.createFromXFDF(annotXfdf);
await pdfDoc.fdfMerge(fdfDoc);
}
await insertSignFormField(pdfDoc, approvalFormData);
await pdfDoc.flattenAnnotations();
if (additialFileOption) {
const {
fontSize,
color,
opacity,
text,
rotation,
fontFamily,
split,
} = additialFileOption;
if (split) {
await createWatermarksWithTempPng(pdfDoc, {
text,
fontSize,
color,
opacity,
rotation,
fontFamily,
});
} else {
await createWatermarkWithTempPng(pdfDoc, {
text,
fontSize,
color,
opacity,
rotation,
fontFamily,
});
}
}
await mainDoc.insertPages(
(await mainDoc.getPageCount()) + 1,
pdfDoc,
1,
await pdfDoc.getPageCount(),
PDFNet.PDFDoc.InsertFlag.e_none
);
}
}
if (additionalFiles && additionalFiles.length > 0) {
for (const additionalFile of additionalFiles) {
let fileDataBuffer = await fs.readFile(additionalFile.filepath);
if (DRM_USE && apiKey) {
const originalFileName = additionalFile.originalFilename;
const mimeType =
additionalFile.mimetype || "application/octet-stream";
const arrayBuffer = await bufferToArrayBuffer(fileDataBuffer);
const { arrayBuffer: decryptArrayBuffer } =
await decryptFileFromArrayBuffer(
arrayBuffer,
originalFileName!,
mimeType
);
fileDataBuffer = Buffer.from(decryptArrayBuffer);
}
const additionalDoc = await PDFNet.PDFDoc.createFromBuffer(
fileDataBuffer
);
if (
additialFileOption &&
additialFileOption.text.replaceAll(" ", "").length > 0
) {
const { text, fontSize, color, opacity, rotation, fontFamily } =
additialFileOption;
if (additialFileOption.split) {
await createWatermarksWithTempPng(additionalDoc, {
text,
fontSize,
color,
opacity,
rotation,
fontFamily,
});
} else {
await createWatermarkWithTempPng(additionalDoc, {
text,
fontSize,
color,
opacity,
rotation,
fontFamily,
});
}
}
await mainDoc.insertPages(
(await mainDoc.getPageCount()) + 1,
additionalDoc,
1,
await additionalDoc.getPageCount(),
PDFNet.PDFDoc.InsertFlag.e_none
);
}
}
const buffer = await mainDoc.saveMemoryBuffer(
PDFNet.SDFDoc.SaveOptions.e_linearized
);
return {
result: true,
buffer,
};
} catch (err) {
console.error({ err: err });
}
};
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;
};
There are no issues with 2 or 3 files, but when the number of files increases, the function completely stops working, and even if I call the function again, it doesn’t work properly.
(The more descriptive your answer, the faster we are able to help you)
Is this issue occurring because I am currently using a Demo key?
Please provide a link to a minimal sample where the issue is reproducible: