Hello,
Your WebView is getting a file not found error because you can't use the Android file path format directly in MAUI.
There is already a full discussion and sample of how to present a PDF in MAUI in GitHub, please refer to The Webview does not display PDF #7399.
Update:
After testing your project with pdf.js to investigate. Please follow the steps below to display your local PDF file using pdf.js.
Step 1. Download the PDF.js file for version 2.14.305. After testing, the latest version of PDF.js will have an error that the PDF file cannot be displayed.
Step 2. Create a pdfjs
folder in the Raw
folder in your MAUI project and copy the web
and build
folders to the pdfjs
folder.
Step 3. Set your pdf file properties to MauiAssert.
Step 4. Refer to the following code to display a pdf file using a WebView.
<WebView x:Name="web_view" HeightRequest="500" WidthRequest="500"/>
public MainPage()
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;
#elif IOS
#endif
LoadPdfViewer();
private void LoadPdfViewer()
string pdfFilePath = $"file:///android_asset/pdfjs/web/viewer.html?file=file:///android_asset/test.pdf";
web_view.Source = new UrlWebViewSource { Url = pdfFilePath };
Best Regards,
Alec Liu.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
I have tried above solution but didn't see yet.
Here's my repo and please let me know if I'm missing anything
https://github.com/PrashantGorakhMane/PDFWebView
@Thombre, Ashish
You can not download files to the Raw folder at runtime.
This is because asset files are embedded resources inside the program and can only be declared at the outset.
Actually, you need to get the URI link of the downloaded file to display it, and you don't need to put the pdf file into the Raw
folder.
There is a common question, when you import the pdf .js plugin, do you include the pdfjs folder? If yes, please include pdfjs in the path, otherwise it will create an issue where the file does not exist.
There is a pdfjs folder under the raw folder
$"file:///android_asset/pdfjs/web/viewer.html?file=
This is the case where only the web
and build
folders are copied to Raw.
$"file:///android_asset/web/viewer.html?file=