title: How to Convert Pdf to base64 and Encode / Decode
body: <p>OK, I have some pdf need convert to base64 by base64encoder.</p>
  
  <p>finally, I use decoder to convert back to pdf format but my content is lost.</p>
  
  <p>my code :</p>
  
  <pre><code>byte[] input_file = Files.readAllBytes(Paths.get("C:\\user\\Desktop\\dir1\\dir2\\test3.pdf"));
      byte[] encodedBytes = Base64.getEncoder().encode(input_file);
  
      String pdfInBase64 = new String(encodedBytes);
      String originalString = new String(Base64.getDecoder().decode(encodedBytes));
  
      System.out.println("originalString : " + originalString);
  
      FileOutputStream fos = new FileOutputStream("C:\\user\\Desktop\\dir1\\dir2\\newtest3.pdf");
      fos.write(originalString.getBytes());
      fos.flush();
      fos.close();
  </code></pre>
  
  <p>result :</p>
  
  <p>Encode : <a href="https://pastebin.com/fnMACZzH" rel="noreferrer">https://pastebin.com/fnMACZzH</a></p>
  
  <p><a href="https://i.stack.imgur.com/FZ6ki.png" rel="noreferrer">Before base64encode</a></p>
  
  <p><a href="https://i.stack.imgur.com/O5a70.png" rel="noreferrer">After decode</a></p>
  
  <p>Thank You </p>