Optimize PDF Resources in Node.js

Optimize PDF Resources

Optimize resources in the document:

  1. Resources that are not used on the document pages are removed
  2. Equal resources are joined into a single object
  3. Unused objects are deleted

In case you want to optimize PDF resources, you can use AsposePdfOptimizeResource function. Please check the following code snippet in order to optimize PDF resources in Node.js environment.

CommonJS:

  1. Call require and import asposepdfnodejs module as AsposePdf variable.
  2. Specify the name of the PDF file for which resources will be optimized.
  3. Call AsposePdf as Promise and perform the operation for optimizing file. Receive the object if successful.
  4. Call the function AsposePdfOptimizeResource.
  5. Optimize a PDF resources. Thus, if ‘json.errorCode’ is 0, the result of the operation is saved in “ResultPdfOptimizeResource.pdf”. If the json.errorCode parameter is not 0 and, accordingly, an error appears in your file, the error information will be contained in ‘json.errorText’.

  const AsposePdf = require('asposepdfnodejs');
  const pdf_file = 'Aspose.pdf';
  AsposePdf().then(AsposePdfModule => {
      /*Optimize resources of PDF-file and save the "ResultPdfOptimizeResource.pdf"*/
      const json = AsposePdfModule.AsposePdfOptimizeResource(pdf_file, "ResultPdfOptimizeResource.pdf");
      console.log("AsposePdfOptimizeResource => %O", json.errorCode == 0 ? json.fileNameResult : json.errorText);
  });

ECMAScript/ES6:

  1. Import the asposepdfnodejs module.
  2. Specify the name of the PDF file for which resources will be optimized.
  3. Initialize the AsposePdf module. Receive the object if successful.
  4. Call the function AsposePdfOptimizeResource.
  5. Optimize a PDF resources. Thus, if ‘json.errorCode’ is 0, the result of the operation is saved in “ResultPdfOptimizeResource.pdf”. If the json.errorCode parameter is not 0 and, accordingly, an error appears in your file, the error information will be contained in ‘json.errorText’.

  import AsposePdf from 'asposepdfnodejs';
  const AsposePdfModule = await AsposePdf();
  const pdf_file = 'Aspose.pdf';
  /*Optimize resources of PDF-file and save the "ResultPdfOptimizeResource.pdf"*/
  const json = AsposePdfModule.AsposePdfOptimizeResource(pdf_file, "ResultPdfOptimizeResource.pdf");
  console.log("AsposePdfOptimizeResource => %O", json.errorCode == 0 ? json.fileNameResult : json.errorText);