Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Server-Side Generation of PDF Streams from Office Documents Using Aspose Java API

Tech May 16 1

Repository Configuration

If the default Maven repositories fail to resolve Aspose artifacts, explicitly configure the official source in your project settings:

<repositories>
    <repository>
        <id>aspose-java-repo</id>
        <url>https://releases.aspose.com/java/repo</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>

Core Dependencies

Word Processor Support

Include the following dependency for DOC and DOCX handling:

<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-words</artifactId>
    <version>20.12</version>
    <type>pom</type>
</dependency>

Spreadsheeet Support

For XLS and XLSX formats:

<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-cells</artifactId>
    <version>20.12</version>
</dependency>

Presentation Support

Required for PPT and PPTX processing:

<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-slides</artifactId>
    <version>20.12</version>
    <classifier>jdk16</classifier>
</dependency>

Implementation Patterns

The following utility class demonstrates converting uploaded files into InputStream instances containing PDF data. This approach centralizes logic to reduce code duplication.

import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import com.aspose.cells.PdfSaveOptions;
import com.aspose.cells.Workbook;
import com.aspose.slides.Presentation;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;

public class DocumentConversionUtils {

    /**
     * Transforms a Word document into a binary PDF stream.
     */
    public static InputStream transformWordToPdf(MultipartFile file) throws Exception {
        try (ByteArrayOutputStream buffer = new ByteArrayOutputStream()) {
            Document doc = new Document(file.getInputStream());
            doc.save(buffer, SaveFormat.PDF);
            return new ByteArrayInputStream(buffer.toByteArray());
        }
    }

    /**
     * Transforms an Excel spreadsheet into a binary PDF stream.
     */
    public static InputStream transformExcelToPdf(MultipartFile file) throws Exception {
        try (ByteArrayOutputStream buffer = new ByteArrayOutputStream()) {
            Workbook workbook = new Workbook(file.getInputStream());
            PdfSaveOptions pdfConfig = new PdfSaveOptions();
            pdfConfig.setOnePagePerSheet(true);
            workbook.save(buffer, pdfConfig);
            return new ByteArrayInputStream(buffer.toByteArray());
        }
    }

    /**
     * Transforms a PowerPoint presentation into a binary PDF stream.
     */
    public static InputStream transformPptToPdf(MultipartFile file) throws Exception {
        try (ByteArrayOutputStream buffer = new ByteArrayOutputStream()) {
            Presentation pres = new Presentation(file.getInputStream());
            pres.save(buffer, SaveFormat.Pdf);
            return new ByteArrayInputStream(buffer.toByteArray());
        }
    }
}

Licensing and Watermarks

Free versions of the library generate watermarked output. To remove this limitation, obtain a valid license key or XML file. Place this file in your src/main/resources directory.

import com.aspose.words.License;
import java.io.InputStream;

public void loadLicense() {
    try {
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        InputStream licStream = loader.getResourceAsStream("license.xml");
        if (licStream != null) {
            License license = new License();
            license.setLicense(licStream);
        }
    } catch (Exception e) {
        // Handle missing license configuration
    }
}
Tags: Java

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.