Flutter PDF Download from URL

Here I explain how to develop Flutter PDF Download from URL method. When the user click download button the particular file will be automatically downloaded from our server. For that we have to store the file in our own server it may be free hosting like GitHub or upload via Shared hosting from top providers. But the recommended way is own server because then only we able to set user friendly URL to get from client side.

When the particular user click the download button, it’s automatically stored on user file manager download folders. It’s very common in Java & PHP programming language but in Flutter Dart some developers are facing issues. So in this tutorial we are clearly explain how to call and download any file formats such as PDF, Excel, Images (PNG, JPEG etc).

Steps

  1. Add http packages on your code
  2. Make the request using http request
  3. Convert the Response
  4. Fetch & Download the data

For more once go through official documentation & the demo example also available on the particular tutorial. Once visit the link to get more idea to get PDF file from server side. Also read our Flutter projects source code in free of cost. We have collection of application like Food delivery app, online shopping, construction, auction and more categories are available.

Flutter Downloader Plugin

The plugin used for manage & download files from server to client side. Through this we can easily able to manage our entire documents such as PDF or image oriented files. However Android Studio integration also explained in our tutorial in the section of Android guidelines.

final taskId = await FlutterDownloader.enqueue(
  url: 'your download link',
  savedDir: 'the path of directory where you want to save downloaded files',
  showNotification: true, // show download progress in status bar (for Android)
  openFileFromNotification: true, // click on notification to open downloaded file (for Android)
);

To know more once read official doc

PDF Document View & Save – Flutter PDF Download

Actually PDF format is very common and most of the users are preferred this one because it’s support major mobile device. Moreover the patterns also not changes which means designs, alignment and more features are available. In the word extension sometimes it was collapsed and entire alignment will be mixed or overlapped.

If you want to download or save the file from particular mentioned space then use below code to get more accessing features. The alternative code’s are mentioned below, once use this code if you are facing any issues then please share the exact answers. Because may be it’s very helpful for upcoming people’s who are struggle to download the PDF file via Flutter framework.

Future<String> downloadFile(String url, String fileName, String dir) async {
        HttpClient httpClient = new HttpClient();
        File file;
        String filePath = '';
        String myUrl = '';
    
        try {
          myUrl = url+'/'+fileName;
          var request = await httpClient.getUrl(Uri.parse(myUrl));
          var response = await request.close();
          if(response.statusCode == 200) {
            var bytes = await consolidateHttpClientResponseBytes(response);
            filePath = '$dir/$fileName';
            file = File(filePath);
            await file.writeAsBytes(bytes);
          }
          else
            filePath = 'Error code: '+response.statusCode.toString();
        }
        catch(ex){
          filePath = 'Can not fetch url';
        }
    
        return filePath;
      }

Leave a Reply