Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Does anyone know how to create pdf from base64 string and show in xamarin forms ?

VIVEKNEGIVIVEKNEGI USMember ✭✭✭

Hi all,

Does anyone know how to display base64 string to webview in Xamarin forms and then I need to download this base64 string in pdf file.

Answers

  • JarvanJarvan Member, Xamarin Team Xamurai

    Read the string and save it as a local file. Then open the pdf file using a webView. The WebView control can display PDF files on the iOS platform, but not on the Android and Windows Phone platforms due to lack of platform support. You could achieve the reading function in native using DependencyService.

    byte[] byteBuffer = Encoding.UTF8.GetBytes(pdfString);
    
    public string SaveBinary(string filename, byte[] bytes)
    {
        string filepath = GetFilePath(filename);
        if (File.Exists((filepath)))
        {
            File.Delete(filepath);
        }
        File.WriteAllBytes(filepath, bytes);
    
        return filepath;
    }
    
    string GetFilePath(string filename)
    {
        return Path.Combine(GetPath(), filename);
    }
    
    string GetPath()
    {
        return System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
    }
    

    Refer to:
    https://forums.xamarin.com/discussion/125846/convert-base64-string-to-pdf-file
    https://stackoverflow.com/questions/46452194/xamarin-forms-open-pdf-in-browser?rq=1

Sign In or Register to comment.