[Server SDK] create pdf file is not working

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:

1 Like

Hello, I’m AiRon, an automated tech support bot :robot:
While you wait for one of our customer support representatives, I can help you with your question.


It looks like you’re experiencing issues with creating PDF files using the Apryse Server SDK when handling multiple files. This problem could be related to memory management, resource cleanup, or the usage of temporary files in your code.

Some things you can try:

  • Ensure that all resources are properly cleaned up after use. This includes closing PDFDoc objects and releasing any temporary files.

  • Verify that the PDFNet.runWithCleanup() method is correctly implemented to handle exceptions and clean up resources.

  • Consider implementing batch processing or handling files in smaller groups to reduce memory usage.

  • Check the logs for any specific error messages or exceptions that could indicate the source of the problem.

  • If using a demo key, ensure that there are no limitations affecting the number of files processed simultaneously.



Here are some resources that might help you:


Hopefully, this helps you solve your problem while you wait for a human to get back to you.

1 Like

We’re going to need additional information. Could you clarify “not working?” Is it stuck on a specific call or file? Is the entire function being ran asynchronously? Is this running in a container or similar environment? Is t here enough memory available?

1 Like