Flutter — Sharing PDF File(Text and Image)

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<activity
....
android:requestLegacyExternalStorage="true"
....
>
image_picker: ^0.7.3
esys_flutter_share: ^1.0.2
pdf: ^3.1.0
import 'dart:io';
import 'dart:typed_data';
import 'package:esys_flutter_share/esys_flutter_share.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as pw;

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'PDF Share Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.red,
),
home: MyHomePage(title: 'PDF Share - Home Page'),
);
}
}

class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);

final String title;

@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
TextEditingController text = new TextEditingController();
File _image;
final _picker = ImagePicker();
final _pdfText = pw.Document();
final _pdfImage = pw.Document();
Uint8List _pdfTextByte;
Uint8List _pdfImageByte;

Future<Uint8List> _addPDFPageText() async {
_pdfText.addPage(pw.Page(
pageFormat: PdfPageFormat.a4,
build: (pw.Context context) {
return pw.Center(
child: pw.Text(text.text),
); // Center
}));
_pdfTextByte = await _pdfText.save();
return _pdfTextByte;
}

Future<Uint8List> _addPDFPageImage() async {
final image = pw.MemoryImage(
_image.readAsBytesSync(),
);
_pdfImage.addPage(pw.Page(build: (pw.Context context) {
return pw.Center(
child: pw.Image(image),
); // Center
}));
_pdfImageByte = await _pdfImage.save();
return _pdfImageByte;
}

Future getImage() async {
final pickedFile = await _picker.getImage(source: ImageSource.gallery);

setState(() {
if (pickedFile != null) {
_image = File(pickedFile.path);
} else {
print('No image selected.');
}
});
}

_sharePdfText() {
_addPDFPageText().whenComplete(() async =>
await Share.file("pdfText", "pdfText" + ".pdf", _pdfTextByte, '*/*'));
}

_sharePdfImage() async {
_addPDFPageImage().whenComplete(() async => await Share.file(
"pdfImage", "pdfImage" + ".pdf", _pdfImageByte, '*/*'));
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
centerTitle: true,
),
body: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
SizedBox(
height: 50,
),
Text(
'Share PDF Text File',
style: TextStyle(
fontStyle: FontStyle.normal,
fontSize: 15,
fontWeight: FontWeight.bold),
),
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(5), color: Colors.red),
margin: EdgeInsets.all(15.0),
child: TextFormField(
maxLines: 6,
controller: text,
),
),
ElevatedButton(
onPressed: () {
_sharePdfText();
},
child: Text("Share")),
SizedBox(
height: 50,
),
Text(
'Share PDF Image File',
style: TextStyle(
fontStyle: FontStyle.normal,
fontSize: 15,
fontWeight: FontWeight.bold),
),
_image == null
? Container(
margin: EdgeInsets.all(15.0),
child: ElevatedButton(
onPressed: () {
getImage();
},
child: Text("Add Image")))
: Container(
margin: EdgeInsets.all(15.0),
alignment: Alignment.center, // use aligment
child: Image.file(
_image,
height: 300,
width: 150,
fit: BoxFit.fill,
),
),
ElevatedButton(
onPressed: () {
_sharePdfImage();
},
child: Text("Share")),
],
),
),
);
}
}

References

--

--

--

Software Engineer(Flutter,Native Android — Mobile Application Developer)

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Espresso [All official development resources]

Reflecting app state on screen easily in Flutter

Creating a Lock Screen Device App for Android

Dark Mode on Android App with Kotlin

How to Send an Email With Attachments in Android

Android Spinner DatePicker

Unit Testing Custom View in Android

Flutter — BLoC Architecture

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ganesh L G

Ganesh L G

Software Engineer(Flutter,Native Android — Mobile Application Developer)

More from Medium

Why flutter is most used by everyone? — Introduction to the Flutter and Dart

Development of Mobile App using Delphi

Run GUI app in Docker

A comprehensive guide to Flutter state management techniques