In my invoiceGenerator i need to get an Image in the invoice that's stored in the postgres db as a byte[]
since i changed this image input from a local path hardcoded in method to an entity ImageDataFile in the database i have problems with itext7 recognising the file.
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
PdfWriter writer = new PdfWriter(outputStream);
PdfDocument pdfDocument = new PdfDocument(writer);
pdfDocument.setDefaultPageSize(PageSize.A4);
Document document = new Document(pdfDocument);
float twocol1 = 280f;
float[] twocolwidth1 = {twocol1, twocol1};
float twocol = 200f;
float[] twoColumnWidth = {twocol, twocol};
ImageDataFile imageDataFileTopPage = imageRepos.findByName("goldencarrotImagekleinste.jpg")
.orElseThrow(() -> new ResourceNotFoundException("image not found"));
ImageData imageTopPageData = ImageDataFactory.create(imageDataFileTopPage.getImageData());
Image imageTopPage = new Image(imageTopPageData);
as a byte[] variable it also didn't work because the imageDataFactory.create from itext7 doesnt recognise the file.
uhhh ohh my fault stupid, the first plan works now because i forgot the "!" sign in the while loop imageUtils.decompress. that declares why the byte[] wasn't recognised it was compressed....``
byte[] imageTopPageBytes = imageService.getImage("goldencarrotImagekleinste.jpg");
ImageData imageTopPageData = ImageDataFactory.create(imageTopPageBytes);
Image imageTopPage = new Image(imageTopPageData);
ImageService{
public byte[] getImage(String name){
ImageDataFile imageData = imageRepos.findByName(name).orElseThrow(() ->
new ResourceNotFoundException("Image not found"));
return ImageUtil.decompressImage(imageData.getImageData());
}
}
ImageUtils{
public static byte[] decompressImage(byte[] data) {
Inflater inflater = new Inflater();
inflater.setInput(data);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
byte[] tmp = new byte[4 * 1024];
try {
while (inflater.finished()) { // <-- must be (!inflater.fisished())
int count = inflater.inflate(tmp);
outputStream.write(tmp, 0, count);
}
outputStream.close();
} catch (DataFormatException | IOException e) {
throw new RuntimeException(e);
}
return outputStream.toByteArray();
}
beginnersmistake haha forgive me guys any thanks