Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

best way to open a remote pdf in xamarin forms pcl project

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?

like image 298
DEV Avatar asked Feb 05 '23 13:02

DEV


2 Answers

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/

like image 142
Adam Avatar answered Feb 07 '23 03:02

Adam


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)
like image 26
hvaughan3 Avatar answered Feb 07 '23 02:02

hvaughan3