Flutter — Sharing PDF File(Text and Image)

The following article describes how to share text and images in PDF format with flutter.
The following steps will guide you, how you can share PDF in flutter.
Step 1:
Add following permission AndroidManifest.xml file
<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"/>
Step 2:
Add following line of code inside under activity in AndroidManifest.xml file to support android api 29+(image_picker package).
<activity
....
android:requestLegacyExternalStorage="true"
....
>
Step 3:
Add following plugins in pubspec.yaml file.
image_picker: ^0.7.3
esys_flutter_share: ^1.0.2
pdf: ^3.1.0
Step 4:
Refer the below code to share text or image in PDF Format
main.dart
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")),
],
),
),
);
}
}
Please watch below video for your reference,

References
Thank you…!!!