Essential Kit Tutorials
DocumentationDownload
Latest(v3)
Latest(v3)
  • Introduction
  • Features Overview
  • Whats new in v3
    • Version 3 vs Version 2
    • Release Notes
    • Upgrade from V2
  • Plugin Overview
    • Settings
    • Folder Structure
    • Installation FAQ
    • Upgrade Guide
  • Features
    • 📒Address Book
      • Setup
      • Usage
      • Testing
      • FAQ
    • App Shortcuts
      • Setup
      • Usage
    • 🆕App Updater
      • Setup
      • Usage
      • Testing
    • 💲Billing Services
      • Setup
        • iOS
        • Android
      • Usage
      • Testing
        • iOS
        • Android
      • FAQ
    • ☁️Cloud Services
      • Setup
        • iOS
        • Android
      • Usage
      • Testing
      • FAQ
    • 🔗Deep Link Services
      • Setup
        • iOS
        • Android
      • Usage
      • Testing
      • FAQ
    • 🛠️Utilities (Extras)
      • Usage
    • 💯Game Services
      • Setup
        • iOS
        • Android
      • Usage
      • FAQ
    • 📸Media Services
      • Setup
      • Usage
      • FAQ
    • 📆Native UI
      • Setup
      • Usage
      • FAQ
      • Examples
        • Login Dialog
    • Network Services
      • Setup
      • Usage
      • FAQ
    • ⏰Notification Services
      • Setup
        • iOS
        • Android
      • Usage
      • Examples
        • Nudge to come-back to the game
      • FAQ
    • ⭐Rate My App
      • Setup
      • Usage
      • FAQ
    • 🤝Sharing
      • Setup
      • Usage
        • Message Composer
        • Mail Composer
        • Social Share Composer
        • Share Sheet
      • FAQ
      • Examples
        • Add Attachment Example
    • Task Services
      • Setup
      • Usage
    • 🌏Web View
      • Setup
      • Usage
      • FAQ
      • Examples
        • Loading Pdf File
  • Notes
    • Resolving Android Gradle Build Errors
    • Google Play Services Authentication
    • Target API Level vs Min API Level
    • Handling Refunds for In-App Purchases (Billing Services)
Powered by GitBook
On this page
  • Loading web pdf
  • Loading local pdf

Was this helpful?

Edit on GitHub
  1. Features
  2. Web View
  3. Examples

Loading Pdf File

PreviousExamplesNextResolving Android Gradle Build Errors

Last updated 3 months ago

Was this helpful?

Android natively doesn't have option to render pdf file. So rendering it is a bit tricky but solutions exist.

Loading web pdf

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.

Just prefix your url with "" and call LoadUrl of web view.

string yourPdfUrl  = "https://yourwebsite.com/sample.pdf"
string urlPath     = "http://docs.google.com/gview?embedded=true&url=" + yourPdfUrl;

//...

webView.LoadUrl(URLString.URLWithPath(urlPath));

Loading local pdf

Here we will make use of Mozilla's pdf.js project for rendering the pdf locally.

  1. Place the above mozilla.zip folder in Assets/StreamingAssets folder

  2. Unzip its contents and delete mozilla.zip

  3. Prefix your local pdf file path with below string (PDF_VIEWER_PATH)

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));

Load Local Pdf File
//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);
        }
    }
    
    
}

🌏
http://docs.google.com/gview?embedded=true&url=
1MB
mozilla.zip
archive