How to Open PDF or Word Document in Flutter ??

3 min read
How to Open PDF or Word Document in the Flutter
How to Open PDF or Word Document in the Flutter

While building a flutter mobile application sometimes users need to open pdf or word documents with a Button press. Before jumping into this Check out how to Solve if InkWell Not Showing Ripple Effect In Flutter.聽So in this article, we will go through How to Open PDF or Word Document in Flutter.

Are you ready for the same? So let’s dive into it!!

How to Open PDF or Word Document in Flutter ??

A good and simple approach to this is the open_file package which lets you open a file with the given path. It supports numerous different file types:

import 'package:open_file/open_file.dart';

OpenFile.open("/sdcard/example.pdf");

In pubspec.yaml you need :

url_launcher: ^0.4.2+5

Includes :

import 'package:flutter/src/gestures/tap.dart';
import 'package:url_launcher/url_launcher.dart';

The code snippet will be like the below:

new RichText(
  text: new LinkTextSpan(
      url: 'http://docs.google.com/viewer?url=http://www.pdf995.com/samples/pdf.pdf',
      text: 'Show My Pdf'),
),

LinkTextSpan class like this:

class LinkTextSpan extends TextSpan {
  LinkTextSpan({TextStyle style, String url, String text})
      : super(
      style: style,
      text: text ?? url,
      recognizer: new TapGestureRecognizer()
        ..onTap = () {
          launch(url);
        });
}

without downloading the file, open from URL using the package: url_launcher

Import package will look like the below snippet:

import 'package:url_launcher/url_launcher.dart';

You can consider a code snippet like the below:

_launchURL() async {
  const url = 'https://flutter.dev/exapmle.pdf';
  if (await canLaunch(url)) {
    await launch(url);
  } else {
    throw 'Could not launch $url';
  }
}

Lots of amazing content coming up your way!!

Conclusion:

Thanks for being with us on a Flutter Journey !!! Stay Connected

So today, In this article, we have been through how to Open PDF or Word Document in Flutter.

Let us know if you have any doubts regarding Flutter Development!! We would love to assist you 馃檪

Keep Learning !!! Keep Fluttering !!!

Don’t forget to drop your valuable feedback in the comments!! It keeps the enthusiasm going on.

FlutterAgency.com聽is our portal Platform dedicated to Flutter Technology and聽Flutter Developers. The portal is full of cool resources from Flutter like聽Flutter Widget聽Guide,聽Flutter Projects,聽Code libs聽and etc.

FlutterAgency.com is one of the most popular online portals dedicated to聽Flutter Technology and daily thousands of unique visitors come to this portal to enhance their knowledge of聽Flutter.

Leave a Reply