Generate pdf byte array from image byte array with same size in java

42 Views Asked by At

I am trying to generate pdf byte array from image array and then storing in google bucket. Everything is working fine except resultant pdf page size. Input image size is reasonable (can be viewed without scrolling page) but when output file is uploaded to google bucket its size becomes more than what it had in inout image. I have to scroll to view the complete output file

Below is my code

public static byte[] receiptImageToPdf(byte[] receipt) throws IOException, DocumentException {
        Image image = Image.getInstance(receipt);
        Rectangle imageSize = new Rectangle( image.getWidth() + 1f,   image.getHeight() + 1f);
        image.scaleAbsolute(image.getWidth(), image.getHeight());
        Document document = new Document(imageSize, 0, 0, 0, 0);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        PdfWriter writer = PdfWriter.getInstance(document, out);
        document.open();
        document.add(image);
        document.close();
        writer.close();
        return out.toByteArray();
    }

Later:

byte[] pdfBytes = Util.receiptImageToPdf(imageBytes);
storage.createFrom(blobInfo, new ByteArrayInputStream(pdfBytes));

Downloaded file from google bucket enter image description here

I need to generate byte array and then upload to google bucket which can be fit to one page as per input image.

0

There are 0 best solutions below