4

Whats is the best way to load a pdf on server in the xamarin pcl App for both Ios and Android. Is there a good nuget or we have to write the custom renderers?

3 Answers 3

7

Opening a PDF in the app, you have a few options.

iOS has had PDF support in its WebView for a while. Android doesn't. If you want to do it in a WebView you can do this:

var webView = new WebView(); webView.Source = new UrlWebViewSource() { Url = "https://example.com/my.pdf" }; 

If you are only supporting Android API 21+ and higher, you can use the PdfRenderer for Android only.

If you need to support older devices, or want a cross platform approach, you have 2 options. If its a public PDF document, you can take an easy approach and use Google's PDF Viewer

var webView = new WebView(); var pdfUrl = "https://example.com/my.pdf"; var googleUrl = "http://drive.google.com/viewerng/viewer?embedded=true&url="; webView.Source = new UrlWebViewSource() { Url = googleUrl + pdfUrl }; 

If it's secure and you have to download it to your app first, use Mozilla's PDFJS Viewer. Xamarin have a walkthrough here: https://developer.xamarin.com/recipes/cross-platform/xamarin-forms/controls/display-pdf/

Sign up to request clarification or add additional context in comments.

7 Comments

Nice! Did not know later versions of Android support it.
I'm actually just going to try and confirm that. I know that they included PdfRenderer in API21+. But I was sure it could have a PDF in the WebView in recent versions, though I always did an internal PDF reader anyway, because I had to support down to API 15.
@hvaughan3 - my bad, just tested wtih 7.0 and no PDF loading in a WebView. :( I was thinking of the PdfRenderer. Thanks for pointing it out.
@AdamPedley have you found a solution? With webView.Source = new UrlWebViewSource() { Url = "example.com/my.pdf" }; I see nothing...
@AlessandroCaliaro - use the Mozilla PDFJS approach if you want a cross platform solution in a webview.
|
1

Like Tim said, on iOS you can use a WebView pointed to the remote PDF and get on with your life.

On Android the WebView cannot handle PDFs by default (W.T.F!!).

So that means you must do one of the following:

  1. Use the remote PDF:

    • use Google Drives URL for it https://drive.google.com/viewerng/viewer?url=<PDF URL HERE>
  2. Download the PDF locally:

    • Use PDF.js to display it in a WebView (Xamarin Forms link)
    • Use an Intent on Android to ask Android what apps the user has installed already that can handle PDFs (SO link)

1 Comment

Looks like we had the same idea :)
0

I'm not aware of any existing Xamarin Forms PDF plugins that will do this for you.

You will have to resort to custom renderers to achieve this. Unless it is okay to open the remote PDF in a browser instead of in your app. In that case you could use Device.OpenUri(...) to open it.

Another option would be to incorporate a WebView in your Xamarin Forms app, and open the PDF in there. That would prevent users from actually leaving the app when they open the PDF.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.