Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter url_launcher downloading the pdf file instead of opening it in browser

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";
                                                    },
like image 264
Noob Master Avatar asked Aug 31 '25 20:08

Noob Master


2 Answers

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;
like image 146
Syed Muheeb Avatar answered Sep 03 '25 15:09

Syed Muheeb


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);
}
like image 35
Domenico Avatar answered Sep 03 '25 14:09

Domenico