Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert base64 to pdf?

Tags:

android

Given a base64 input, how would I convert it into a PDF file in Android?

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        setContentView(R.layout.activity_main);
        // Get the PDF file to encode it into Base64
        File file = new File("/mnt/sdcard/download/Base64.pdf");
        try {
            // Files is from Guava library
            Files.toByteArray(file);
            // Encoded into Base64
            byte[] temp = Base64.encode(Files.toByteArray(file), Base64.DEFAULT);
            // For testing purposes, wrote the encoded string to file
            writeToFile(Base64.encodeToString(temp, Base64.DEFAULT).toString());

        } catch (IOException e) {
            Log.d(tag, "File.toByteArray() error");
            e.printStackTrace();
        }
    }

I was able to use http://www.webutils.pl/index.php?idx=base64 to test the PDF file was being encoded properly. But I would like to be able to decode the base64 back to PDF myself. How would I do that?

Edit 1

Tried the below code as suggested by Saneesh CS

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        setContentView(R.layout.activity_main);
        // Get the PDF file to encode it into Base64
        File file = new File("/mnt/sdcard/download/Base64.pdf");
        try {
            // Files is from Guava library
            Files.toByteArray(file);
            // Encoded into Base64
            byte[] temp = Base64.encode(Files.toByteArray(file), Base64.DEFAULT);
            // Tried with the below line, but same result.
            byte[] temp1 = Base64.encode(Files.toByteArray(file), 0);
            // For testing purposes, wrote the encoded string to file
//          writeToFile(Base64.encodeToString(temp, Base64.DEFAULT).toString());
            final File dwldsPath = new File(Environment.getExternalStorageDirectory(), "test7.pdf");
            byte[] pdfAsBytes = Base64.decode(temp, 0);
            FileOutputStream os;
            os = new FileOutputStream(dwldsPath, false);
            os.write(pdfAsBytes);
            os.flush();
            os.close();
        } catch (IOException e) {
            Log.d(tag, "File.toByteArray() error");
            e.printStackTrace();
        }
    }

Above code works.

like image 895
Noman Arain Avatar asked Nov 13 '13 15:11

Noman Arain


People also ask

How do I download a PDF from Base64?

Paste your string in the “Base64” field. Press the “Decode Base64 to PDF” button. Click on the filename link to download the PDF.

What is Base64 PDF?

Base64 encoding is used to encode binary data, such as a PDF file, into an ASCII string format that is compatible with systems that can only handle text. For example, email attachments and binary uploads in HTML forms are converted and transmitted as Base64 encoded data.

How do I convert Base64 to Notepad ++?

To encode or decode Base64 data you need to first highlight the entire range of data you want to be encoded or decoded. Next, click on “Plugins” in the top bar, then “MIME Tools”. In the second level of the menu you can see all of the Base64 encode and decode options.


1 Answers

final File dwldsPath = new File(DOWNLOADS_FOLDER + fileName + ".pdf");
byte[] pdfAsBytes = Base64.decode(txt, 0);
FileOutputStream os;
os = new FileOutputStream(dwldsPath, false);
os.write(pdfAsBytes);
os.flush();
os.close();

Try this code. txt is the base64 encoded string.

like image 80
San Avatar answered Oct 14 '22 14:10

San