2

I'm using url_launcher: ^5.7.5 and when i'm passing a pdf url in the launch function it it keeps downloading the pdf instead of opening it on my browser,

onTap: () async {
    
   url="http://3.65.45.149/uploads/store/vendor_report/vendor_pickup_report_257.pdf";
       if (await canLaunch(url)){
         await launch(url,
         headers: { "Content-Type":"application/pdf",
                     "Content-Disposition":"inline"}, );
            print("browser url");
            print(url);
          }
              else
              // can't launch url, there is some error
              throw "Could not launch $url";
                                                    },
Noob Master
  • 91
  • 1
  • 10

4 Answers4

1

In the new version of URl_laucher you need to manually specify the launch mode like this:

return await launchUrl(Uri.parse(command),
        mode: LaunchMode.externalNonBrowserApplication);

for webview mode: Launch.inAppWebView;
cokeman19
  • 2,405
  • 1
  • 25
  • 40
Syed Muheeb
  • 11
  • 1
  • 4
0

I believe that in this case, would be better you be using webview_flutter: ^3.0.2 instead of laucher, hope that helps you!

  • so should i use webview instead of url_launcher ? – Noob Master Apr 13 '22 at 14:15
  • I think it can helps you if you're developing for mobile devices, because when you launch the url without the webview, it cannot open the google PDF reader, it is just literally launching the url, but when it open up a webview, it's just like a browser tab, and then the browser reader could be opened, let me know if it helps you. – Samuel Cunha Apr 14 '22 at 15:20
0

I tried everything but what worked for me was instead of launching your file URL directly, attach Google Drive embed link to it

url = 'https://docs.google.com/gview?embedded=true&url=${fileUrl}'

For your case fileUrl = 'http://3.65.45.149/uploads/store/vendor_report/vendor_pickup_report_257.pdf'

0

Updated for the newer versions of the url_launcher flutter package. The launch and canLaunch functions are deprecated.

You can simplify your code and extract it in a compact function:

void openPdfFromUrl(String url) {
  debugPrint('opening PDF url = $url');
  var googleDocsUrl = 'https://docs.google.com/gview?embedded=true&url=${Uri.encodeQueryComponent(url)}';
  debugPrint('opening Google docs with PDF url = $googleDocsUrl');
  final Uri uri = Uri.parse(googleDocsUrl);
  launchUrl(uri);
}
Domenico
  • 1,331
  • 18
  • 22