-
Notifications
You must be signed in to change notification settings - Fork 910
PDFBOX-6032: add configurable default image factory #210
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
PDFBOX-6032: add configurable default image factory #210
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. |