Getting issue while view pdf in .net maui webview for android platform

Thombre, Ashish 130 Reputation points
2024-01-09T06:24:42.1566667+00:00
		InitializeComponent();

        Microsoft.Maui.Handlers.WebViewHandler.Mapper.AppendToMapping("MyCustomization", (handler, view) =>
        {
#if ANDROID
            handler.PlatformView.Settings.JavaScriptEnabled = true;
            handler.PlatformView.Settings.AllowFileAccess = true;
            handler.PlatformView.Settings.AllowFileAccessFromFileURLs = true;
            handler.PlatformView.Settings.AllowUniversalAccessFromFileURLs = true;
#endif
        });


        string path;
        using (var http = new HttpClient())
        {
            var stream = await http.GetStreamAsync("https://www.africau.edu/images/default/sample.pdf");
            path = Path.Combine(FileSystem.AppDataDirectory, "sample.pdf");
            using Stream streamToWriteTo = File.Open(path, FileMode.Create);
            await stream.CopyToAsync(streamToWriteTo);
        }
        if (File.Exists(path))
        {
#if ANDROID
            Android.Net.Uri uri = Android.Net.Uri.FromFile(new Java.IO.File(path));
            string pdfFilePath = string.Format("file:///android_asset/web/viewer.html?file={0}", uri);
            pdfWebView.Source = new UrlWebViewSource { Url = pdfFilePath };
#endif
        }

i am using above code but getting issue web page not available, please refer attached video.

i want to download pdf and show it in webview for android platform

2024-01-09_10h23_42

Developer technologies | .NET | .NET MAUI
0 comments No comments
{count} vote

1 answer

Sort by: Most helpful
  1. Cliff 1 Reputation point MVP Volunteer Moderator
    2025-03-26T23:53:23.24+00:00

    Your trying to pull the file from a URI that doesn't exist as Android is Linux based and the file system isn't the same as windows. I suggest you use the FileSystemHelpers to load your file. Dependant on where your file is, if it's in the Resources/Raw folder odd a lot easier to find. You can read more here. https://learn.microsoft.com/en-us/dotnet/maui/platform-integration/storage/file-system-helpers?view=net-maui-9.0&tabs=android I personally use the PDF viewer from Sync fusion which is free and does all the hard work for you rather than use the browser.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.