Compress PDF or Word file in Node SDK

Reduce document size using JavaScript

Using the REST API in JavaScript, developers can compress documents in DOC, DOCX, RTF, PDF, and other formats. With the given solution, the output will be a high quality file after compression.

View code snippet

Compress document in JavaScript

Using our powerful JavaScript API, developers can compress files to optimize content and output file size. Reducing file size occurs by cleaning up unused data and resources. You can also compress images to make the output file size even smaller.

The result of content optimization and compression can be saved in the original file format or any available save format – DOCX, DOC, RTF, PDF, HTML and many others.

Reduce file size in JavaScript

As mentioned, our Node SDK allows you to programmatically reduce the size of a document in any supported format. For example, you can reduce the file size of PDF, DOCX, DOC, RTF, ODT, EPUB, HTML and other formats. And now you can try our powerful functionality and evaluate how to compress a document in JavaScript with the following example:

Upload a file you want to optimize/compress
Run code
Select the target format from the list
import * as fs from "fs";

const wordsApi = new WordsApi("####-####-####-####-####", "##################");

const requestDocument = fs.createReadStream("Input.docx");
const requestCompressOptions = new model.CompressOptions({
    imagesQuality: 75,
    imagesReduceSizeFactor: 1
})
const compressDocumentRequest = new model.CompressDocumentOnlineRequest({
    document: requestDocument,
    compressOptions: requestCompressOptions
});

wordsApi.compressDocumentOnline(compressDocumentRequest)
.then((compressDocumentResult) => {
    const requestDocument = compressDocumentResult.Document.Values();
    const convertDocument = new model.ConvertDocumentRequest({
        document: requestDocument,
        format: "docx"
    });

    wordsApi.convertDocument(convertDocument)
    .then((convertDocumentResult) => {
        console.log("Result of ConvertDocument: ", convertDocumentResult);
    });
});
Run code
  
Copy JavaScript code to the clipboard

How to compress a file Word, PDF, GIF, ZIP JavaScript REST API

  1. Install Aspose.Words Cloud for Node
  2. Add a library reference (import the library) to your JavaScript project
  3. Configure API using your app keys
  4. Load the source document to compress
  5. Compress documents and images, clean unused information
  6. Select the output file format
  7. Get the result of the document compression as a separate file

JavaScript library to use document compression

Install 'Aspose.Words Cloud SDK for Node.js' using NPM package manager. Run npm install asposewordscloud --save from the command line to install the SDK via the NPM package manager. As an alternative, you can manually clone Aspose.Words Cloud SDK for Node.js source code from GitHub and use it in your project. Please follow these Instructions to quickly get the necessary security credentials and access our REST API.

System Requirements

  • @types/request (version 2.48.3+)
  • lodash (version 4.17.15+)
  • lodash.template (version 4.5.0+)
  • request (version 2.88.0+)
  • request-debug (version 0.2.0+)

Refer to the Repository Documentation to see more details.

5%

Subscribe to Aspose Product Updates

Get monthly newsletters and offers directly delivered to your mailbox.

© Aspose Pty Ltd 2001-2024. All Rights Reserved.