2

I have a question about writing image to PDF using PDFBox.

My requirement is very simple: I get an image from a web service using Spring RestTemplate, I store it in a byte[] variable, but I need to draw the image into a PDF document.

I know that the following is provided:

final byte[] image = this.restTemplate.getForObject(
        this.imagesUrl + cableReference + this.format,
        byte[].class
);

JPEGFactory.createFromStream() for JPEG format, CCITTFactory.createFromFile() for TIFF images, LosslessFactory.createFromImage() if starting with buffered images. But I don't know what to use, as the only information I know about those images is that they are in THUMBNAIL format and I don't know how to convert from byte[] to those formats.

Thanks a lot for any help.

informatik01
  • 16,038
  • 10
  • 74
  • 104
saadoune
  • 409
  • 1
  • 5
  • 21

1 Answers1

3

(This applies to version 2.0, not to 1.8)

I don't know what you mean with THUMBNAIL format, but give this a try:

    final byte[] image = ... // your code
    ByteArrayInputStream bais = new ByteArrayInputStream(image);
    BufferedImage bim = ImageIO.read(bais);
    PDImageXObject pdImage = LosslessFactory.createFromImage(doc, bim);

It might be possible to create a more advanced solution by using

PDImageXObject.createFromFileByContent()

but this one uses a file and not a stream, so it would be slower (but produce the best possible image type).

To add this image to your PDF, use this code:

    PDDocument doc = new PDDocument();
    try
    {
        PDPage page = new PDPage();
        doc.addPage(page);

        PDPageContentStream contents = new PDPageContentStream(doc, page);

        // draw the image at full size at (x=20, y=20)
        contents.drawImage(pdImage, 20, 20);

        // to draw the image at half size at (x=20, y=20) use
        // contents.drawImage(pdImage, 20, 20, pdImage.getWidth() / 2, pdImage.getHeight() / 2);

        contents.close();
        doc.save(pdfPath);
    }
    finally
    {
        doc.close();
    }
Tilman Hausherr
  • 17,731
  • 7
  • 58
  • 97
  • Thank you for your answer, actually after testing an javax.imageio.IIOException: Unsupported Image Type is thrown at this line : **BufferedImage bim = ImageIO.read(bais);** – saadoune Mar 02 '16 at 17:21
  • @saadoune Can you save the image byte array to a file? FileOutputStream fos = new FileOutputStream(new File(....)); fos.write(array); fos.close(); and then open that file with IrfanView when press "I" (I like Information). What type is it? – Tilman Hausherr Mar 02 '16 at 17:25
  • When opening the image it shows a warning saying that it is a JPG file with incorrect extension but, in the help it says "compression : JPEG, CMYK, quality: 100, subsampling OFF". Thank you. – saadoune Mar 02 '16 at 17:45
  • Ouch, CMYK Jpegs are indeed a problem for java ImageIO. What you could try is to use https://github.com/haraldk/TwelveMonkeys , i.e. put the libraries for JPEG in your classpath. – Tilman Hausherr Mar 02 '16 at 17:48
  • Hello, thanks for your help and indications, i have now added twelvemokeyproject to my workspace, and image-jpeg as a dependency, anyway, how am i supposed to use (read/write) methods from his packages. Thanks again. – saadoune Mar 03 '16 at 11:29
  • You don't have to change any code. It is just important that the twelvemonkeys jar files are your classpath. You can still use ImageIO.read(). (See "Basic usage" in the link) – Tilman Hausherr Mar 03 '16 at 11:32
  • Unfortunately i still have the javax.imageio.IIOException: Unsupported Image Type, image-jpeg is in the classpath and no changes to the code. – saadoune Mar 03 '16 at 13:28
  • You need not one, but six jar files: twelvemonkeys-common-lang-3.2.1.jar twelvemonkeys-common-io-3.2.1.jar twelvemonkeys-common-image-3.2.1.jar twelvemonkeys-imageio-core-3.2.1.jar twelvemonkeys-imageio-metadata-3.2.1.jar twelvemonkeys-imageio-jpeg-3.2.1.jar – Tilman Hausherr Mar 03 '16 at 13:31
  • Indeed, that's the case, they are inportet when setting imageio-jpeg as a dependency. But still not working. – saadoune Mar 03 '16 at 13:34
  • Either it isn't in your classpath, or you've found a bug in twelvemonkeys. Can you share the file? If yes, please upload it somewhere. To verify that the JPEG plugin is installed and used at run-time, you could use the following code: Iterator readers = ImageIO.getImageReadersByFormatName("JPEG"); while (readers.hasNext()) { System.out.println("reader: " + readers.next()); } The first line should print: reader: com.twelvemonkeys.imageio.plugins.jpeg.JPEGImageReader@somehash – Tilman Hausherr Mar 03 '16 at 13:41