title: Generate a PDF using Streams
body: <p>I am trying to convert an InputStream into a byte array to write it in a file, to generate a PDF.</p>
  
  <p>I have a File type with the url of a PDF, and with that, i have the inputStream of that.</p>
  
  <pre><code>File fichero_pdf = new File("C:/Users/agp2/Desktop/PDF_TRIAXE.pdf");
  InputStream stream4 = new FileInputStream(fichero_pdf);
  </code></pre>
  
  <p>Until here everything is perfect, the problem appears when i try to transform this InputStream to a byte[] and write it in a new File.
  I have these two methods:</p>
  
  <p>to convert the Stream to a byte[]:</p>
  
  <pre><code>private static byte[] getArrayFromInputStream(InputStream is) {
  
      BufferedReader br = null;
      StringBuilder sb = new StringBuilder();
  
  
      String line;
      try {
  
          br = new BufferedReader(new InputStreamReader(is));
          while ((line = br.readLine()) != null) {
              sb.append(line+"\n");
          }
  
      } catch (IOException e) {
          e.printStackTrace();
      } finally {
          if (br != null) {
              try {
                  br.close();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }
  
      return sb.toString().getBytes();
  
  }
  </code></pre>
  
  <p>To write the byte[] in the new file:</p>
  
  <pre><code>...
  
  File file=new File(dto.getTitulo());
    InputStream stream=dto.getContenido();      
                 byte[] array=getStringFromInputStream(stream);
                 OutputStream salida=new FileOutputStream(file);
                 salida.write(array);
                 salida.close();
                 stream.close();
                 helper.addAttachment(file.getName(), file);
             }
              mailSender.send(message);
  ...
  </code></pre>
  
  <p>The Email is sent at perfection, but when i can't open the .pdf.
  Also, i compare the code of the new pdf with the code of the first, and is a little bit different.</p>
  
  <p>I need to create a valid pdf file from an inputStream.</p>