Display PDF in your Flutter App using  'flutter_plugin_pdf_viewer' plugin.

In this article , I will explain you how to use flutter plugin "flutter_plugin_pdf_viewer" to load pdf from URL .


Display PDF in your Flutter App using  'flutter_plugin_pdf_viewer' plugin.


Step By Step :-


1) Create flutter app by using command "flutter create appname " .


flutter create app for flutter_plugin_pdf_viewer


here my appname is pdfviewer.

2) after creating app , goto the "pubspec.yaml" file and under dependencies: section add dependency "flutter_plugin_pdf_viewer: any".




dependency for flutter_plugin_pdf_viewer


3) after adding dependency goto the "main.dart" file.
4) Now,We need to create a RaisedButton,

remove all previous code.when we click on button we redirected to pdf Viewer page .

You may also like:- How to Create a Music Player app in flutter using Firebase (Cloud firestore,firebase storage) and 'music_player' plugin ?



5) Code for main.dart page :-


import 'package:flutter/material.dart';
import 'package:pdfviewer/Viewpdf.dart';


void main() => runApp(
                   MyApp()
                );


class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}


class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "Pdf Viewer",
      theme: ThemeData(
        primarySwatch: Colors.deepOrange
      ),
      home:Scaffold(
        appBar: AppBar(
          title: Text(
            "PDF Viewer"
          ),
        ),
        body:Builder(builder: (context)=>
         Center(
          child: Column(
            children: <Widget>[
                  SizedBox(
                    height:50.0,
                  ),
                RaisedButton(
                  padding: EdgeInsets.all(10.0),
                  onPressed: (){
                    Navigator.push(
context, MaterialPageRoute(
builder: (context)=>Viewpdf()));
                  },
                  color: Colors.deepOrangeAccent[200],
                  child: Text(
                    "Open from url",
                    style: TextStyle(
                        color: Colors.white,
                        fontSize: 20.0
                    ),
                  ),
                  ),
            ],
            ),
        ),
      ),)
    );
  }
}

6) In Viewpdf.dart page we need to import package.

package for flutter_plugin_pdf_viewer

7) Logic for fetching pdf through url using

"flutter_pdf_plugin_viewer" plugin.
loadfile() async {
document = await PDFDocument.fromURL(
"http://www.africau.edu/images/default/sample.pdf"
);
    setState(() {
      document=document;
    });
  }



8) Code for Viewpdf.dart page :-

import 'package:flutter/material.dart';
import 'package:flutter_plugin_pdf_viewer/
flutter_plugin_pdf_viewer.dart';
class Viewpdf extends StatefulWidget {
  @override
  _ViewpdfState createState() => _ViewpdfState();
}
class _ViewpdfState extends State<Viewpdf> {
  PDFDocument document;
  @override
  void initState() {
    super.initState();
    loadfile();
  }
   loadfile() async {
    document = await PDFDocument.fromURL(
"http://www.africau.edu/images/default/sample.pdf"
);
    setState(() {
      document=document;
    });
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('Example'),
        ),
        body: Center(
         child: PDFViewer(document: document)),
    );
  }
}


9) Output :-
output for flutter_plugin_pdf_viewer