title: Insufficient Data For An Image (PDF File Generation)
body: <p>I'm using <strong>PDFBox</strong> to generate PDF files, however when I try to draw an image which I receive from an array of bytes I get the following error:  </p>
  
  <blockquote>
    <p><strong>Insufficient data for an image</strong> </p>
  </blockquote>
  
  <p>This is the basic structure of my code:</p>
  
  <pre><code>    public ByteArrayOutputStream generatePDF() {                
  
          .. Variable Declaration
  
          // Creating Document
          document = new PDDocument();
  
          // Creating Pages       
          for(int i = 0; i &lt; arrayVar.length; i++) {
  
              // Adding page to document
              page = new PDPage();
  
              // Creating FONT Attributes
              fontNormal = PDType1Font.HELVETICA;
              fontBold = PDType1Font.HELVETICA_BOLD;
  
              // Building Front &amp; Back Invoice Images         
              singleImageMap = // Getting Map With Array Of Bytes from Web Service Call;
  
              if(singleImageMap != null &amp;&amp; !singleImageMap.isEmpty()) {
  
                              arrayFront = Utils.readImage((byte[]) singleImageMap.get(Constants.WS_IMAGE_FRONT));
              arrayBack = Utils.readImage((byte[]) singleImageMap.get(Constants.WS_IMAGE_BACK));
  
              fileFront = new ByteArrayInputStream(arrayFront);
              fileBack = new ByteArrayInputStream(arrayBack);                 
                  bufferedImageFront = ImageIO.read(fileFront);
                  bufferedImageBack = ImageIO.read(fileBack);                 
  
                  rescaledFrontImg = Scalr.resize(bufferedImageFront, 500);
                  rescaledBackImg = Scalr.resize(bufferedImageBack, 500);
  
                  front = new PDJpeg(document, rescaledFrontImg);
                  back = new PDJpeg(document, rescaledBackImg);
  
              }
  
              // Next we start a new content stream which will "hold" the to be created content.
              contentStream = new PDPageContentStream(document, page);                
  
              // Let's define the content stream
              contentStream.beginText();
              contentStream.setFont(fontNormal, 8);
              contentStream.moveTextPositionByAmount(200, 740);
              contentStream.drawString("NAME: " + arrayVar[i].getParameter(Constants.NAME));
              contentStream.endText();
  
              if(front != null &amp;&amp; back != null) {
                  contentStream.drawImage(front, 55, 500);
                  contentStream.drawImage(back, 55, 260); 
              }           
  
              // Add Page
              document.addPage(page);
  
              // Let's close the content stream       
              contentStream.close();
  
          }
  
          // Let's create OutputStream object
          output = new ByteArrayOutputStream(); 
  
          // Finally Let's save the PDF
          document.save(output);
          document.close();
  
          return output;
      }
  </code></pre>
  
  <blockquote>
    <p>Since I receive a PNG file from the Web Service I do the conversion to JPG with the following method:</p>
  </blockquote>
  
  <pre><code>    public static byte[] readImage(byte[] file) throws Exception {
  
      ImageInputStream is = ImageIO.createImageInputStream(new ByteArrayInputStream(file));
  
      BufferedImage originalImage = ImageIO.read(is);         
      ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
      ImageIO.write(originalImage, "jpg", baos ); 
  
      byte[] imageInByte = baos.toByteArray();
  
      return imageInByte;
  }
  </code></pre>
  
  <p>As per this link:</p>
  
  <blockquote>
    <p><a href="https://issues.apache.org/jira/browse/PDFBOX-849" rel="nofollow">https://issues.apache.org/jira/browse/PDFBOX-849</a></p>
  </blockquote>
  
  <p>It points out that the error is because the PDJepg object should be created before the creation of the contentStream, but that's what I do in my code.</p>
  
  <p>I'm not sure if there is a problem with the structure of my code, or that maybe there is an error in the way I'm handling the image bytes I'm getting from the Web Service call.</p>
  
  <p>Does anyone has an idea of what could be the problem?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I did what Zelter Ady and indeed the image that I'm getting from the Web Service is valid since I was able to generate a physical file with it, so the problem should be somewhere around the manipulation of the image, the thing is I don't know what I'm missing.</p>