Please invoke PDFNet Initialize function before calling other functions

Product: @pdftron/pdfnet-node

Product Version: 10.5.0

Please give a brief summary of your issue:
I have an AWS Lambda that is running the pdfnet-node SDK to convert a word template to fill as a PDF document.

My code works fine the first time run but because Lambda containers stay “warm” for a period of time (which is a good thing), on subsequent calls I get this error:

message: 'Exception: \n' +
    '\t Message: [2023-11-27T16:08:52Z] The PDFNet Terminate function has been called. Please invoke PDFNet Initialize function before calling other functions.\n' +
    '\n' +
    '\t Conditional expression: !m_throw_error\n' +
    '\t Version      : 10.5.0-3831253\n' +
    '\t Platform     : Linux\n' +
    '\t Architecture : AMD64\n' +
    '\t Filename     : APIDataCollector.cpp\n' +
    '\t Function     : ThrowIfServerError\n' +
    '\t Linenumber   : 365\n',
  type: 'PDFWorkerError'

Please describe your issue and provide steps to reproduce it:
My code is the following. The runWithCleanup method w/ a finally including a shutdown I believe was from some code samples. If I should be using a different pattern please let me know.

export async function generateContent(templateUrl: string, data: any) {
    let base64_string: undefined | string = undefined;

    const response = await fetch(templateUrl);
    const templateData = await response.arrayBuffer();
    const templateFile = `${outputPath}${randomUUID()}.docx`;
    const outputFileName = `${outputPath}${randomUUID()}.pdf`;
    fs.appendFileSync(templateFile, Buffer.from(templateData));

    await applyLogo(data);
    data = prepData(data);

    let runError: Error | undefined = undefined;

    const main = async () => {
        try {
            console.log('Starting Main');
            const options = new PDFNet.Convert.OfficeToPDFOptions();

            const jsonData = JSON.stringify(data);
            // Create a TemplateDocument object from an input office file.
            const templateDoc = await PDFNet.Convert.createOfficeTemplateWithPath(templateFile, options);
            console.log('create template done');

            console.log('templateKeys', await templateDoc.getTemplateKeysJson());

            // Fill the template with data from a JSON string, producing a PDF document.
            const pdfdoc = await templateDoc.fillTemplateJson(jsonData);

            console.log('fill doc');

            // Save the PDF to a file.
            await pdfdoc.save(outputFileName, PDFNet.SDFDoc.SaveOptions.e_linearized);

            // And we're done!
            console.log('Saved');

            const buff = fs.readFileSync(outputFileName);
            base64_string = buff.toString('base64');

        } catch (err) {
            throw err;
        }

        console.log('Done.');
    };
    try {
        await PDFNet.runWithCleanup(main, LicenseKey);
    }
    catch (error) {
        console.error('Failure in main', error);
        runError = error;
    }
    finally {
        await PDFNet.shutdown();
    }

    if(runError) throw runError;
    return base64_string;
}

Please provide a link to a minimal sample where the issue is reproducible:

1 Like

Note, I did just test removing the following code and it does seem to resolve my issue but I would still like to confirm that this won’t have any unintended negative side effects. If the Lambda container destruction is sufficient then thats good enough for me.

finally {
        await PDFNet.shutdown();
    }
2 Likes

For reference, the sample code where I saw the shutdown call was in @pdftron/pdfnet-node-samples under OfficeTemplateTest.js. However, in the page for AWS Lambda it does not include that line.

1 Like

Hello Steve,

The await PDFNet.shutdown(); method is not meant to be called in a Lambda function, as the SDK is supposed to be running all the time. The SDK will shut down with the Lambda container destruction.

Please let me know if you have any further questions.

1 Like