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 .
Step By Step :-
1) Create flutter app by using command "flutter create appname "
.
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".
3) after adding dependency goto the "main.dart" file.
4) Now,We need to create a RaisedButton,
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 ?
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()));
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.
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"
);
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';
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"
);
"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)),
);
}
}
You May Also like:- How to create Tinder Swipe Cards in Flutter using 'flutter_tindercard' package ?
9) Output :-
2 Comments
Thanks🙏
ReplyDeleteIf I want to List out and View pdf files from firebase storage then ? If I want to use fromAsset shall I keep the pdf files in aset folder and what changes to be done in .yaml file
ReplyDelete