Android natively doesn't have option to render pdf file. So rendering it is a bit tricky but solutions exist.
To load a pdf file, we can make use of google drive's embedded url utility to achieve similar functionality on both iOS and Android.
string yourPdfUrl = "https://yourwebsite.com/sample.pdf"
string urlPath = "http://docs.google.com/gview?embedded=true&url=" + yourPdfUrl;
//...
webView.LoadUrl(URLString.URLWithPath(urlPath));
Here we will make use of Mozilla's pdf.js project for rendering the pdf locally.
string PDF_VIEWER_PATH = "file:///android_asset/mozilla/pdfjs/pdf_viewer.html?file=";
string PDF_VIEWER_PATH = "file:///android_asset/mozilla/pdfjs/pdf_viewer.html?file=";
string yourFilePersistentDataPath = "/storage/0/emulated/.../sample.pdf";
string finalPath = PDF_VIEWER_PATH + "file://" + yourFilePersistentDataPath;
//...
webView.LoadUrl(URLString.URLWithPath(finalPath));
//Example to load sample.pdf placed in StreamingAssets
private void LoadLocalFile()
{
string PDF_VIEWER_PATH = "file:///android_asset/mozilla/pdfjs/pdf_viewer.html?file=";
string fileName = "sample.pdf"; // Change to your file
StartCoroutine(CopyToPersistentPath(Application.streamingAssetsPath, fileName, (string copiedFilePath) =>
{
m_activeWebView.LoadURL(URLString.FileURLWithPath(PDF_VIEWER_PATH+ "file://" + copiedFilePath));
//If you want to directly load file from streaming assets instead of copying to persistent path
//m_activeWebView.LoadURL(URLString.FileURLWithPath(PDF_VIEWER_PATH+ "file://android_asset/" + "sample.pdf"));
}));
}
private IEnumerator CopyToPersistentPath(string sourceFolder, string fileName, Action<string> onComplete)
{
string sourcePath = Path.Combine(sourceFolder, fileName);
string destinationPath = Path.Combine(Application.persistentDataPath, fileName);
using (UnityWebRequest request = UnityWebRequest.Get(sourcePath))
{
yield return request.SendWebRequest();
if (request.result != UnityWebRequest.Result.Success)
{
Debug.LogError("Error loading file: " + request.error);
onComplete?.Invoke(null);
yield break;
}
try
{
File.WriteAllBytes(destinationPath, request.downloadHandler.data);
Debug.Log("File copied to: " + destinationPath);
onComplete?.Invoke(destinationPath);
}
catch (IOException e)
{
Debug.LogError("Failed to write file: " + e.Message);
}
}
}