-
Notifications
You must be signed in to change notification settings - Fork 704
Add Brotli compression PDF support #1555
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
70506b2
Add Brotli (/BrotliDecode) stream filter support
andreasrosdalw 2eebca1
Fixes
andreasrosdalw ca8dfd0
Remove accidentally-added empty FilterHandler.java
andreasrosdalw f3aca4c
Remove accidentally-added empty FilterHandlers.java
andreasrosdalw c4926af
Ensure Brotli is available
andreasrosdalw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
openpdf-core/src/main/java/org/openpdf/text/pdf/codec/BrotliFilter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| /* | ||
| * Copyright 2026 OpenPDF | ||
| * | ||
| * Licensed under the LGPL 2.1 or MPL 2.0. | ||
| */ | ||
| package org.openpdf.text.pdf.codec; | ||
|
|
||
| import com.aayushatharva.brotli4j.Brotli4jLoader; | ||
| import com.aayushatharva.brotli4j.decoder.DecoderJNI; | ||
| import com.aayushatharva.brotli4j.decoder.DirectDecompress; | ||
| import com.aayushatharva.brotli4j.encoder.BrotliOutputStream; | ||
| import com.aayushatharva.brotli4j.encoder.Encoder; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.OutputStream; | ||
|
|
||
| /** | ||
| * Brotli compression / decompression helper used to support PDF streams that use the | ||
| * {@code /BrotliDecode} filter. Brotli is being standardised as a stream filter for | ||
| * PDF 2.0 (ISO 32000-2) through ISO/TS 32001. | ||
| * <p> | ||
| * Backed by <a href="https://github.com/hyperxpro/Brotli4j">brotli4j</a>. The native | ||
| * library is loaded lazily on first use; consumers that never touch Brotli pay no cost. | ||
| */ | ||
| public final class BrotliFilter { | ||
|
|
||
| private BrotliFilter() { | ||
| } | ||
|
|
||
| private static void ensureAvailable() throws IOException { | ||
| try { | ||
| Brotli4jLoader.ensureAvailability(); | ||
| } catch (Exception e) { | ||
| throw new IOException("Brotli native library could not be loaded. " | ||
| + "Add a brotli4j native dependency for your platform.", e); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Compresses the given data using Brotli (default quality). | ||
| * | ||
| * @param in the data to compress | ||
| * @return the Brotli-compressed bytes | ||
| * @throws IOException on compression error | ||
| */ | ||
| public static byte[] encode(byte[] in) throws IOException { | ||
| ensureAvailable(); | ||
| return Encoder.compress(in); | ||
| } | ||
|
|
||
| /** | ||
| * Returns a new streaming Brotli encoder that writes its compressed output to | ||
| * {@code out}. Closing the returned stream flushes and finishes the Brotli frame. | ||
| * | ||
| * @param out the destination for the compressed bytes | ||
| * @return an {@link OutputStream} that Brotli-compresses everything written to it | ||
| * @throws IOException if the encoder cannot be created | ||
| */ | ||
| public static OutputStream encoder(OutputStream out) throws IOException { | ||
| ensureAvailable(); | ||
| return new BrotliOutputStream(out); | ||
| } | ||
|
|
||
| /** | ||
| * Decompresses Brotli-encoded data. | ||
| * | ||
| * @param in the compressed data | ||
| * @return the decoded bytes | ||
| * @throws IOException if the data cannot be decoded | ||
| */ | ||
| public static byte[] decode(byte[] in) throws IOException { | ||
| ensureAvailable(); | ||
| DirectDecompress result = DirectDecompress.decompress(in); | ||
| if (result.getResultStatus() != DecoderJNI.Status.DONE) { | ||
| throw new IOException("Brotli decoding failed: " + result.getResultStatus()); | ||
| } | ||
| return result.getDecompressedData(); | ||
| } | ||
| } | ||
|
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.