I have a requirement to display an Image uploaded to an entity in a spreadsheet generated by a Jasper Report. This same image must also be displayable in a table on a screen.
The entity itself has a FileDescriptor object and the file itself is stored in FileStorage on the server. I can display it on a table with no issues.
How do I get that into a Jasper Report as an Image? My Jasper Report is currently based on SQL, so I would need to be able to load it with SQL somehow. The solutions I’ve seen refer to using a parameter, but that won’t work when each image is attached to its own entity.
If the image were stored as a BLOB or ByteA in the database, then I’ve seen documentation on how it could be loaded: Using Images stored in a database for reports. But Cuba Platform doesn’t handle images that way, does it? Can the user upload an image to a ByteArray in the database? If I manage to do that, how can I display that ByteArray as an image on a screen?
A long time ago I wrote my own Custom Report object that interfaces with Cuba Reports. So, it’s kind of a mashup between your two options.
The version I use right now passes parameters by putting them in the root band of the report. It also passes the Cuba/Ds JDBC connection to Jasper. The executed Jasper report has its own SQL statement and executes it over the Cuba/Ds connection.
I have a partial solution already, but I am not happy with it. I created a BLOB/ByteArray object in my database in addition to a FileDescriptor field. I upload to the FileDescriptor to make Cuba happy and allow Images to be displayed on screens. I then copy the image from the FileDescriptor to the BLOB and store that directly in the database.
In the JRXML, I add the BLOB object to the SQL with a type of java.awt.Image. I embed an image in the report with the same type(java.awt.Image) and set the field expression to be the column in my SQL.
It works, but I now have two copies of each image on my server - one in your file storage and the other in the database.
In a perfect world, I would be able to tell Cuba to load the image directly to the BLOB instead of FileDescriptors and use those directly in my Data Containers.
In a perfect world, I would be able to tell Cuba to load the image directly to the BLOB instead of FileDescriptors and use those directly in my Data Containers.
You can override your filestore implementation and store files in a database instead of the filesystem. com.haulmont.cuba.core.app.FileStorageAPI is a standard spring bean that can be overridden and registered in the core spring.xml. You can find a sample implementation in a project cuba-aws/AmazonS3FileStorage.java at master · cuba-platform/cuba-aws · GitHub
Or
As far as I remember JasperReport supports function in parameters. Reporting addon adds a function that allows using bitmap images in a JasperReport. The function can be used as:
Function is registered as a standard report parameter in a parameters map com.haulmont.yarg.formatters.impl.jasper.CubaJRFunction.
So your function can load an image from the filesystem by file descriptor identifier.