Class BrotliStreamCompressionStrategy
- All Implemented Interfaces:
-
IStreamCompressionStrategy
IStreamCompressionStrategy that uses Brotli compression algorithm.
Brotli is a modern compression algorithm that typically provides better compression ratios than traditional Flate/Deflate compression, especially for text-heavy content. This strategy can be registered with a PDF document to use Brotli compression for all stream objects.
The compression level from iText (0-9) is automatically mapped to Brotli's compression levels (0-11) for compatibility with existing iText compression settings.
Example usage:
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outputStream));
pdfDoc.getDiContainer().register(IStreamCompressionStrategy.class,
new BrotliStreamCompressionStrategy());
- See Also:
-
IStreamCompressionStrategyBrotliOutputStream
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected intconvertCompressionLevel(int compressionLevel) Converts iText compression levels (0-9) to Brotli compression levels (0-11).createNewOutputStream(OutputStream original, PdfStream stream) Creates a new Brotli output stream that wraps the original stream.Returns the decode parameters for Brotli decompression.Returns the PDF filter name for Brotli compression.
-
Constructor Details
-
BrotliStreamCompressionStrategy
public BrotliStreamCompressionStrategy()Constructs a newBrotliStreamCompressionStrategyinstance.
-
-
Method Details
-
getFilterName
Returns the PDF filter name for Brotli compression.The filter name /BrotliDecode is used in the PDF stream dictionary to indicate that the stream is compressed using Brotli compression.
- Specified by:
-
getFilterNamein interfaceIStreamCompressionStrategy - Returns:
-
PdfName.BrotliDecoderepresenting the Brotli filter
-
getDecodeParams
Returns the decode parameters for Brotli decompression.Brotli compression does not require additional decode parameters, so this method returns
null.- Specified by:
-
getDecodeParamsin interfaceIStreamCompressionStrategy - Returns:
-
nullas no decode parameters are needed for Brotli
-
createNewOutputStream
Creates a new Brotli output stream that wraps the original stream.This method creates a
BrotliOutputStreamconfigured with the compression level specified in the PDF stream. The compression level is automatically converted from iText's 0-9 scale to Brotli's 0-11 scale.- Specified by:
-
createNewOutputStreamin interfaceIStreamCompressionStrategy - Parameters:
-
original- the original output stream to wrap -
stream- the PDF stream being compressed (used to get compression level) - Returns:
-
a new
BrotliOutputStreamthat compresses data before writing to the original stream - Throws:
-
PdfException- if an I/O error occurs while creating the Brotli output stream
-
convertCompressionLevel
protected int convertCompressionLevel(int compressionLevel) Converts iText compression levels (0-9) to Brotli compression levels (0-11).This method maps the compression level from iText's standard 0-9 range to Brotli's 0-11 range using linear interpolation. If the input level is out of range, it is set to the default level of 6.
Mapping formula:
brotliLevel = round(iTextLevel * (11.0 / 9.0))- Parameters:
-
compressionLevel- the iText compression level (0-9) - Returns:
- the corresponding Brotli compression level (0-11)
-