PDFBOX-6032: add configurable default image factory#210
PDFBOX-6032: add configurable default image factory#210kimilg wants to merge 3 commits intoapache:trunkfrom
Conversation
kimilg
commented
Jul 12, 2025
- This PR addresses PDFBOX-6032 by introducing a new overload of PDImageXObject#createFromByteArray that accepts a user-supplied defaultFactory parameter for customizable image conversion behavior.
- The goal is to allow users to provide a defaultFactory for custom image conversion. This factory is used directly for formats like BMP and GIF, and acts as a fallback for PNG and TIFF when the optimized conversion path fails and would otherwise fall back to LosslessFactory.
PDFBOX-6032: change test method name
| * PDImageXObject. | ||
| * @throws IllegalArgumentException if the image type is not supported. | ||
| */ | ||
| public static PDImageXObject createFromByteArray(PDDocument document, byte[] byteArray, String name, DefaultFactory defaultFactory) throws IOException |
There was a problem hiding this comment.
Here I'm a bit confused, also by the naming stuff.
I would rather extend the existing method with a parameter. I think "DefaultFactory" is a bad name. I.e., the way I would implement this would be
// Method with signature of the existing method is just forwarding to the other new overload
public static PDImageXObject createFromByteArray(PDDocument document, byte[] byteArray, String name)
{
return createFromByteArray(document,byteArray,name, null);
}
// Extend the existing method with the one parameter
public static PDImageXObject createFromByteArray(PDDocument document, byte[] byteArray, String name, DefaultFactory defaultFactory) throws {
// .. logic like it is now BUT instead of just encoding this lossless we first check if we have a *fallback*
if( defaultFactory != null )
return defaultFactory.create...(...)
// otherwise just do the PNGEncoder Lossless stuff like it is now.
}
And therefor I think defaultFactory is bad name. It should be a fallback.
There was a problem hiding this comment.
I've proposed DefaultFactory and I'm not really happy with that name.
Fallback won't be correct as it isn't a fallback in all cases. It is for png and tiff which can't be processed but it isn't for bmp and gif.
How about CustomFactory?
There was a problem hiding this comment.
Thanks for the feedback.
I’ve removed the duplicate logic, and the code feels more compact and cleaner now.
Naming the functional interface is tricky - a precise name gets too long, but a short one feels too vague.
I renamed it to CustomFactory because it more clearly reflects its intended usage than DefaultFactory.
PDFBOX-6032: change test comment PDFBOX-6032: change format in createFromByteArray
1f2c526 to
28ca17d
Compare
| import org.apache.pdfbox.pdmodel.PDDocument; | ||
|
|
||
| @FunctionalInterface | ||
| public interface CustomFactory { |
There was a problem hiding this comment.
Some JavaDoc to describe what this interface is about would be nice.
But other than that, the changes look fine for me.
There was a problem hiding this comment.
Right, I forgot to add it earlier. I've now added a JavaDoc comment to this class.
|
Thanks for the PR; I forgot the magic words in the commit message, please close the PR yourself. |