A virtual teacher who reveals to you the great secrets of Base64

Convert Base64 to PDF in Java

To convert a Base64 string to PDF file in Java you have to use the Base64.Decoder class and any convenient way to write a byte array to file (it is important to note that Base64.getDecoder().decode() returns a byte array, not a string).

import java.io.File;
import java.io.FileOutputStream;
import java.util.Base64;

class Base64DecodePdf {
  public static void main(String[] args) {
    File file = new File("./test.pdf");

    try ( FileOutputStream fos = new FileOutputStream(file); ) {
      // To be short I use a corrupted PDF string, so make sure to use a valid one if you want to preview the PDF file
      String b64 = "JVBERi0xLjUKJYCBgoMKMSAwIG9iago8PC9GaWx0ZXIvRmxhdGVEZWNvZGUvRmlyc3QgMTQxL04gMjAvTGVuZ3==";
      byte[] decoder = Base64.getDecoder().decode(b64);

      fos.write(decoder);
      System.out.println("PDF File Saved");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}
Comments (32)

I hope you enjoy this discussion. In any case, I ask you to join it.

  • Arturo,
    I get error getDecoder (). Decode (b64) that I have to do to initialize or fix that error
    • Administrator,
      Hi! Can you please specify the full error message and your Java version?
  • Sudhik,
    Thank You. I was writing the PDF file with String from decoder and it would show blank pages. When I write as byte[] it shows the content.
  • Dipak,
    Hello! Thanks for the code snippet. When I run the program, it created a PDF file but when I try open the file, it say "There was an error opening the file. The file is damaged and could not be opened". Please help!
    • Administrator,
      Hello,
      To be short, the code snippet contains only first bytes of a PDF file, but as I pointed out in the example, you should specify a valid Base64 string (for example, use this one).
    • kolyfish,
      ME TOO !!! HELP
  • Praveen,
    Hi,

    What if my encoded string size is more than 65535. Will it work?
  • Marwa,
    Hi , i've text more than 65535 so above is giving error string too long , will you help me how can i pass a long string like that to the decoder
  • PraveenRaj13,
    hi

    im try this code but script pass but pdf document open without content
    • Administrator,
      Hello,
      Please read the code carefully. There is a comment explaining what's the problem.
      • PraveenRaj13,
        Hi Admin

        Im using correct PDF Base64 content but unfortunately its not working manually its working fine with out issue please help us
        • Administrator,
          Please try to use the Base64 string from this page and let me know if it works.
  • Rajeev,
    If the string is more than 65535, I am getting error. Is there a work around for this?
  • john,
    hi, the code works, but i need to display the pdf on a web page, could you help me?
    • Administrator,
      Hi,
      You can embed it via Data URI.
      • john,
        but can I show it on a content type apllication/pdf?
  • Dylan,
    Hello!
    I'm trying to print the decoder result on the web page with an out.print(), but when I see the content, some characters change.
    How do I solve that problem?
  • vero,
    thank you so much, it realy helps me. :D
  • Silva,
    Hi;

    I have an encrypted binary file has been base64 encoded. I want to decode it. How can I do that?
  • maria,
    Hi there, I can trying to follow your code to make the pdfs display on the webpage. But they do not. I upload jpg, png and pdf files as byte[] into a database and then am retrieving them to display. Jpgs and pngs are displaying without problems but pdfs do not. I am working with Java Spring Boot. I use <img src="data:documents/jpg;base64, ${doc}" alt="${ name }"> to display files from database. Can you make suggestions?

    I hardcoded the strings you provided in your tutorial. The red dot displays without any problems. The pdf string does not :(
  • Aline,
    Hi, I'm trying to convert base64 to String (text), but when converting it's coming out all weird, a lot of special characters, without a specific text, would you know how to convert the text to normal writing.

    Exemple: h��.�5Ĉi�I�r�\#-߸��D׈3H��l��uz�(q��g&���DԠ��yg��ȨA����.
  • nqhXncMU,
    -1 OR 2+23-23-1=0+0+0+1
  • nqhXncMU,
    -5) OR 416=(SELECT 416 FROM PG_SLEEP(15))--
  • gBqsPxAZ,
    1 waitfor delay '0:0:15' --
  • nqhXncMU,
    -1); waitfor delay '0:0:15' --
  • ncMUFCMU,
    ak5Ts0bp') OR 44=(SELECT 44 FROM PG_SLEEP(15))--
  • ncMUFCMU,
    if(now()=sysdate(),sleep(15),0)
  • ncMUFCMU,
    if(now()=sysdate(),sleep(15),0)
  • ncMUFCMU,
    1 waitfor delay '0:0:15' --
  • smertitou,
    How to repair a malformed base64 in java ?
  • Ams,
    Hi I have used this for valid base64 encoded string ,but when I open the pdf it says something went wrong can't open this file,please help
  • fHLvlxbf,
    -1; waitfor delay '0:0:15' --
Add new comment

If you have any questions, remarks, need help, or just like this page, please feel free to let me know by leaving a comment using the form bellow.
I will be happy to read every comment and, if necessary, I will do my best to respond as quickly as possible. Of course, spammers are welcome only as readers.