Hi All , We are trying to compress PDF document using ASPOSE Library. PDF size is approx 550 KB but its is taking 6 to 7 second to compress. Is this a expected behavior or we can do anything to enhance the performance.
Below is the code snippet
public String compressFile(
@DisplayName(“Base64 Encoded PDF File”) @Summary(“PDF file in Base64 encoded format”) String base64File)
throws Exception {
String compressedBase64File = “”;
int recursionCounter = 0;
InputStream licenseStream = null;
try {
LOGGER.info("PDF Compression Starts");
byte[] origFileInBytes = Base64.getDecoder().decode(base64File);
int currentSizeOfFile = origFileInBytes.length;
// If the File is larger than 200KB then only compressing
if (currentSizeOfFile > 204800) {
LOGGER.debug("Compression Continues! Size of File is more than 200Kb::" + currentSizeOfFile);
// Loading the Aspose License
License pdfLicense = new License();
licenseStream = HtmlpdfformatterOperations.class.getClassLoader()
.getResourceAsStream("License/Aspose.PDF.Java.lic");
// If License file fails to load
if (licenseStream == null) {
throw new FileNotFoundException("License File not found!");
}
LOGGER.debug("Loading the License");
pdfLicense.setLicense(licenseStream);
// Doing the compression
compressedBase64File = compressionImpl(base64File);
// Decoding the base64 file into byte array
byte[] fileInBytes = Base64.getDecoder().decode(compressedBase64File);
// Compressed File Size
int compressedSize = fileInBytes.length;
// POST Compression, if the file is still more than 200 KB , 204800 Bytes
// and recursionCounter is less than or equal to 3
while(compressedSize > 204800 && recursionCounter <3) {
// Compress it again
LOGGER.debug("recursionCounter value is: " + recursionCounter);
LOGGER.debug("Re-Compressing, Size of previously compressed file: " + compressedSize);
compressedBase64File = compressionImpl(base64File);
recursionCounter++;
}
return compressedBase64File;
} // Returning the file as is, its less than 200 KB
else {
compressedBase64File = base64File;
LOGGER.info("Compression skipped! Size of File is less than 200Kb: " + currentSizeOfFile);
return compressedBase64File;
}
} catch (FileNotFoundException exc) {
LOGGER.error("FileNotFoundException occured: " + exc);
throw new FileNotFoundException(exc.getMessage());
} catch (IOException exc) {
LOGGER.error("IOException occured: " + exc.getMessage());
throw new IOException();
} catch (InvalidPasswordException exc) {
LOGGER.error("InvalidPasswordException occured: " + exc.getMessage());
throw new InvalidPasswordException(exc.getMessage());
} catch (PdfException exc) {
LOGGER.error("PdfException occured: " + exc.getMessage());
throw new PdfException(exc.getMessage());
} catch (Exception exc) {
LOGGER.error("Exception occured: " + exc);
throw new Exception(exc.getMessage());
} finally {
if (licenseStream != null)
licenseStream.close();
recursionCounter=0;
}
}
/*
* Actual Compression Implementation
*/
private String compressionImpl(String base64File) throws Exception {
InputStream fileInputStream = null;
ByteArrayOutputStream compressedPdfBytesStream = null;
com.aspose.pdf.Document pdfDocument = null;
try {
String outputFile = "";
// Decoding the base64 file into byte array
byte[] fileInBytes = Base64.getDecoder().decode(base64File);
// Creating input stream from the byte array
fileInputStream = new ByteArrayInputStream(fileInBytes);
// Creating the output Stream to Store the compressed PDF
compressedPdfBytesStream = new ByteArrayOutputStream();
// Creating the Aspose Document Object for PDF Operations
pdfDocument = new com.aspose.pdf.Document(fileInputStream);
LOGGER.debug("Pages in the document is: " + pdfDocument.getPages().size());
// Init OptimizationOptions
OptimizationOptions opt = new OptimizationOptions();
// Set ImageCompression option
opt.getImageCompressionOptions().setCompressImages(true);
// Set ImageQuality option
opt.getImageCompressionOptions().setImageQuality(10);
// Set ResizeImage option
opt.getImageCompressionOptions().setResizeImages(true);
// Set MaxResolution option
opt.getImageCompressionOptions().setMaxResolution(100);
// Performing the compression on original doc
pdfDocument.optimizeResources(opt);
// Saving the compressed PDF in Output Stream
pdfDocument.save(compressedPdfBytesStream);
// Size of compressed file
LOGGER.debug("New Size of compressed file is: " + compressedPdfBytesStream.toByteArray().length);
outputFile = Base64.getEncoder().encodeToString(compressedPdfBytesStream.toByteArray());
return outputFile;
} finally {
//Closing all the open streams and objects
if (pdfDocument != null)
pdfDocument.close();
if (fileInputStream != null)
fileInputStream.close();
if (compressedPdfBytesStream != null)
compressedPdfBytesStream.close();
}
}
Aspose version: 21.8