HTML JPG PDF XML DOCX
  Product Family
PDF

Compress PDF in Node.js Cloud API

API for working with optimizing PDF documents using Cloud Node.js SDK

Get Started

How to compress PDF via Node.js SDK

To compress PDF, we’ll use Aspose.PDF Cloud Node.js SDK. This Cloud SDK assists Node.js programmers in developing cloud-based PDF creator, annotator, editor, and converter apps using Node.js programming language via Aspose.PDF REST API. Simply create an account at Aspose for Cloud and get your application information. Once you have the App SID & key, you are ready to give the Aspose.PDF Cloud Node.js SDK. This method efficiently optimizes PDF documents using the Aspose.PDF Cloud SDK for Node.js. By automating the upload, optimization, and download processes, it simplifies developers’ workflows. Various optimization options ensure that the resulting PDF is smaller in size without compromising quality.

Package Manager Console Command


     
    npm install asposepdfcloud --save
     
     

Steps to compress PDF via Cloud Node.js

Aspose.PDF Cloud developers can easily load & compress PDF in just a few lines of code.

  1. Initialize the Aspose PDF API
  2. Upload the PDF Document to Cloud Storage
  3. Compress the PDF Document
  4. Download the Optimized PDF Document
  5. Execute the Optimization Process
 

Compress PDF using Node.js


    import credentials from "../../../Credentials/credentials.json"  with { type: "json" };    // json-file in this format: { "id": "*****", "key": "*******" }
    import fs from 'node:fs/promises';
    import path from 'node:path';
    import { PdfApi } from "../../src/api/api.js";
    import { OptimizeOptions } from "../../src/models/optimizeOptions.js";

    const configParams = {
        LOCAL_FOLDER: "C:\\Samples\\",
        PDF_DOCUMENT_NAME: "sample.pdf",
        TEMP_FOLDER : 'TempPdfCloud',
        LOCAL_RESULT_DOCUMENT_NAME: "output_sample.pdf",
    };

    const pdfApi = new PdfApi(credentials.id, credentials.key);

    const PdfCompress = {
        async uploadDocument (fileName, localFolder) {
            const fileNamePath = path.join(configParams.LOCAL_FOLDER, configParams.PDF_DOCUMENT_NAME);
            const fileData = await fs.readFile(fileNamePath);
            const storagePath = path.join(configParams.TEMP_FOLDER, configParams.PDF_DOCUMENT_NAME);
            await pdfApi.uploadFile(storagePath, fileData)
                .then(() => console.log("File: '" + configParams.PDF_DOCUMENT_NAME +"' successfully uploaded."));
        },

        async downloadResult() {
            const fileName = path.join( configParams.TEMP_FOLDER, configParams.PDF_DOCUMENT_NAME);
            const changedPdfData = await pdfApi.downloadFile(fileName);
            const filePath = path.join(configParams.LOCAL_FOLDER, configParams.LOCAL_RESULT_DOCUMENT_NAME);
            await fs.writeFile(filePath, changedPdfData.body);
            console.log("Downloaded: " + filePath);
        },

        async compressPdfDocument() {
            if ( pdfApi ) {

                const optimizeOptions = new OptimizeOptions();
                optimizeOptions.allowReusePageContent = true;
                optimizeOptions.compressImages = true;
                optimizeOptions.imageQuality = 100;
                optimizeOptions.linkDuplcateStreams = true;
                optimizeOptions.removeUnusedObjects = true;
                optimizeOptions.removeUnusedStreams = true;            
                optimizeOptions.unembedFonts = true;
    
                const response = await pdfApi.postOptimizeDocument(configParams.PDF_DOCUMENT_NAME, optimizeOptions, null, configParams.TEMP_FOLDER);
                if (response.body.code != 200)
                    console.error("compressPdfDocument(): Failed to compress the PDF document!");
                else
                    console.log("compressPdfDocument(): Successfully copressed the PDF document '" + configParams.PDF_DOCUMENT_NAME + "' !");
            }
        },
    };

    async function main() {
        try {
            await PdfCompress.uploadDocument();
            await PdfCompress.compressPdfDocument();
            await PdfCompress.downloadResult();

        } catch (error) {
            console.error("Error:", error.message);
        }
    }
 

With our Node.js library you can

Compress PDF documents with Aspose.PDF Cloud Node.js SDK.

  • Add PDF document’s header & footer in text or image format.
  • Add tables & stamps (text or image) to PDF documents.
  • Append multiple PDF documents to an existing file.
  • Work with PDF attachments, annotations, & form fields.
  • Apply encryption or decryption to PDF documents & set a password.
  • Delete all stamps & tables from a page or entire PDF document.
  • Delete a specific stamp or table from the PDF document by its ID.
  • Replace single or multiple instances of text on a PDF page or from the entire document.
  • Extensive support for converting PDF documents to various other file formats.
  • Extract various elements of PDF files & make PDF documents optimized.
  • You can try out our free App to test the functionality.