How to Convert Pdf to base64 and Encode / Decode

OK, I have some pdf need convert to base64 by base64encoder.

finally, I use decoder to convert back to pdf format but my content is lost.

my 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();

result :

Encode : https://pastebin.com/fnMACZzH

Before base64encode

After decode

Thank You

You can decode base64 encoded String and pass that byte[] to FileOutputStream write method to fix this issue.

        String filePath = "C:\\Users\\xyz\\Desktop\\";
        String originalFileName = "96172560100_copy2.pdf";
        String newFileName = "test.pdf";

        byte[] input_file = Files.readAllBytes(Paths.get(filePath+originalFileName));

        byte[] encodedBytes = Base64.getEncoder().encode(input_file);
        String encodedString =  new String(encodedBytes);
        byte[] decodedBytes = Base64.getDecoder().decode(encodedString.getBytes());

        FileOutputStream fos = new FileOutputStream(filePath+newFileName);
        fos.write(decodedBytes);
        fos.flush();
        fos.close();

How to Convert Pdf to base64 and Encode / Decode, Learn how to do Base64 encoding and decoding of a PDF file using Java 8 and Apache Commons Codec. OK, I have some pdf need convert to base64 by base64encoder. finally, I use decoder to convert back to pdf format but my content is lost. my code : byte[] input_file = Files.readAllBytes(Paths.g

I have used Apache Commons library. The version I used for testing this Utility is commons-code-1.4.jar. There are multiple uses of Base64 Codec. The one I had a need to was during Docusign eSignature webservice call where the files attached were sent in a Base64 encoded format.

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.codec.binary.Base64;

public class FileCodecBase64 {

    private static final boolean IS_CHUNKED = true;

    public static void main(String args[]) throws Exception {

        /* Encode a file and write the encoded output to a text file. */
        encode("C:/temp/something.pdf", "c:/temp/something-encoded.txt", IS_CHUNKED);

        /* Decode a file and write the decoded file to file system */
        decode("C:/temp/something-encoded.txt", "c:/temp/something-decoded.pdf");
    }

    /**
     * This method converts the content of a source file into Base64 encoded data and saves that to a target file.
     * If isChunked parameter is set to true, there is a hard wrap of the output  encoded text.
     */
    private static void encode(String sourceFile, String targetFile, boolean isChunked) throws Exception {

        byte[] base64EncodedData = Base64.encodeBase64(loadFileAsBytesArray(sourceFile), isChunked);

        writeByteArraysToFile(targetFile, base64EncodedData);
    }

    public static void decode(String sourceFile, String targetFile) throws Exception {

        byte[] decodedBytes = Base64.decodeBase64(loadFileAsBytesArray(sourceFile));

        writeByteArraysToFile(targetFile, decodedBytes);
    }

    /**
     * This method loads a file from file system and returns the byte array of the content.
     * 
     * @param fileName
     * @return
     * @throws Exception
     */
    public static byte[] loadFileAsBytesArray(String fileName) throws Exception {

        File file = new File(fileName);
        int length = (int) file.length();
        BufferedInputStream reader = new BufferedInputStream(new FileInputStream(file));
        byte[] bytes = new byte[length];
        reader.read(bytes, 0, length);
        reader.close();
        return bytes;

    }

    /**
     * This method writes byte array content into a file.
     * 
     * @param fileName
     * @param content
     * @throws IOException
     */
    public static void writeByteArraysToFile(String fileName, byte[] content) throws IOException {

        File file = new File(fileName);
        BufferedOutputStream writer = new BufferedOutputStream(new FileOutputStream(file));
        writer.write(content);
        writer.flush();
        writer.close();

    }
}

i hope this work

Convert PDF to Base64, Therefore, I need to convert the PDF file stream to Base64 encoded string but wondering how to test the proxy for decode to work via apigee? Copy the text on the left box and click on the arrow to start the conversion, you will be able to download the image on the button download!once it has finished the conversion If you select the Encode Text to base64option, it will allow you to convert any text to base64 just write it on the left side and click the arrow.

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;

public class Base64Sample {

public static void main(String... strings) throws Exception {
    String filePath = "/home/user/sample.pdf";
    File pdfFile = new File(filePath);
    byte[] encoded = Files.readAllBytes(Paths.get(pdfFile.getAbsolutePath()));
    Base64.Encoder enc = Base64.getEncoder();
    byte[] strenc = enc.encode(encoded);
    String encode = new String(strenc, "UTF-8");
    Base64.Decoder dec = Base64.getDecoder();
    byte[] strdec = dec.decode(encode);
    OutputStream out = new FileOutputStream("/home/user/out.pdf");
    out.write(strdec);
    out.close();
}
  }

Convert PDF stream to base64 string in APIGEE, Encodes pdf to base64 through Mirth transformer and Java script.Duration: 6:03 Posted: Jan 14, 2018 Convert Base64 to PDF online using a free decoding tool which allows you to decode Base64 as PDF and display it directly in the browser. In addition, you will receive some basic information about this PDF (MIME type, extension, size). And, of course, you will have a special link to download the PDF to your device.

if (resultCode == RESULT_OK) {

 InputStream is = null;

  try 

{

is = getContentResolver().openInputStream(imageReturnedIntent.getData());

} 

catch (FileNotFoundException e) 
{

 e.printStackTrace();

 }

 byte[] bytesArray = new byte[0];

try {

  bytesArray = new byte[is.available()];

 } 
catch (IOException e) {

 e.printStackTrace();

  }

  try
 {

  is.read(bytesArray);

String d = Base64.encodeToString(bytesArray, android.util.Base64.DEFAULT);


                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

HOW TO CONVERT A PDF FILE TO BASE64, Convert Files to Base64. web developer and programmer tools. World's simplest base64 file encoder. Just select your file or drag & drop it below, press Convert to � 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).

File to Base64 Converter - Encode Files to Base64, Decode from Base64 or Encode to Base64 with advanced formatting options. Enter our site for an easy-to-use online tool. To encode or decode standard input/output or any file content, Linux uses base64 encoding and decoding system. Data are encoded and decoded to make the data transmission and storing process easier. Encoding and decoding are not similar to encryption and decryption. Encoded data can be easily revealed by decoding.

Base64 Decode and Encode, GetBytes($pdf) $base64 =[Convert]::ToBase64String($bytes). or like this: $pdf = Get-Content dnoc_TEST.pdf -Encoding Byte $base64� You can use following approach: import org.apache.commons.codec.binary.Base64; // Encode data on your side using BASE64 byte[] bytesEncoded = Base64.encodeBase64(str.getBytes()); System.out.println("encoded value is " + new String(bytesEncoded)); // Decode data on other side, by processing encoded data byte[] valueDecoded = Base64.decodeBase64(bytesEncoded); System.out.println("Decoded value

Encode/decode PDF file to/from base64, Hi,. Backend sends JSON response with oneof the element having Base-64 encoded array of a PDF document. I would need to decode the Base-64 string value� Base64 encode your data in a hassle-free way, or decode it into human-readable format. Base64 encoding schemes are commonly used when there is a need to encode binary data that needs be stored and transferred over media that are designed to deal with textual data. This is to ensure that the data remains intact without modification during transport.

Comments
  • What happens if you just write encodedBytes to the file, instead of pumping it through a String and then writing the bytes of the String?
  • You should not be going via originalString. Keep the raw decoded bytes.
  • @LouisWasserman Oh, I see that, Thank you :)