How to convert base64 to pdf?

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.


This code might help you. I have executed the attached code and it's working perfectly.

public static GetFilePathAndStatus getFileFromBase64AndSaveInSDCard(String base64, String filename,String extension){
    GetFilePathAndStatus getFilePathAndStatus = new GetFilePathAndStatus();
    try {
        byte[] pdfAsBytes = Base64.decode(base64, 0);
        FileOutputStream os;
        os = new FileOutputStream(getReportPath(filename,extension), false);
        os.write(pdfAsBytes);
        os.flush();
        os.close();
        getFilePathAndStatus.filStatus = true;
        getFilePathAndStatus.filePath = getReportPath(filename,extension);
        return getFilePathAndStatus;
    } catch (IOException e) {
        e.printStackTrace();
        getFilePathAndStatus.filStatus = false;
        getFilePathAndStatus.filePath = getReportPath(filename,extension);
        return getFilePathAndStatus;
    }
}

public static String getReportPath(String filename,String extension) {
    File file = new File(Environment.getExternalStorageDirectory().getPath(), "ParentFolder/Report");
    if (!file.exists()) {
        file.mkdirs();
    }
    String uriSting = (file.getAbsolutePath() + "/" + filename + "."+extension);
    return uriSting;

}
public static class GetFilePathAndStatus{
    public boolean filStatus;
    public String filePath;

}

Tags:

Android