> For the complete documentation index, see [llms.txt](https://assetstore.essentialkit.voxelbusters.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://assetstore.essentialkit.voxelbusters.com/features/web-view/examples/loading-pdf-file.md).

# Loading Pdf File

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 "<http://docs.google.com/gview?embedded=true&url=>" and call LoadURL on the web view.

```csharp
// Assume you have a webView instance (WebView) already created
void LoadPdfInWebView()
{
    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.

{% file src="/files/pk7xueeyVALdZxL3AERT" %}

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=";
```

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

{% code title="Load Local Pdf File" %}

```csharp
//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);
        }
    }
    
    
}
```

{% endcode %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://assetstore.essentialkit.voxelbusters.com/features/web-view/examples/loading-pdf-file.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
