Server-Side Generation of PDF Streams from Office Documents Using Aspose Java API
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
}
}