Reputation: 519
I'm trying to implement some new functionalities to my Flutter app such as sharing PDF files with people. So I got 10 different pages and 10 different PDF files. On each page I want to implement a button, which then allows the user to share the specific PDF file (for example via WhatsApp, Mail, etc.). But I didn't find a proper solution. Where do I save those 10 PDF files inside of my Flutter project? And how can I implement this functionality in general? Thank you so much!
Upvotes: 1
Views: 9862
Reputation: 66
You can use this package.
https://pub.dev/packages/share_pdf
which will help you to share a pdf file
sharePDF() async {
SharePDF sharePDF = SharePDF(
url: "https://pdfobject.com/pdf/sample.pdf",
subject: "Subject Line goes here",
);
await sharePDF.downloadAndShare();
}
Upvotes: 0
Reputation: 544
First, you need to obtain the PDF file in a File so you can get the path for sharing the PDF.
To share the PDF, you can use the share_plus package.
After successfully obtaining the path of the PDF file in your local storage, follow this process:
File? x = await sharePdf(
url: widget.magazineModel.fileUrl,
fileName: widget.magazineModel.name,
);
The code above fetches the PDF from the URL and receives the response in the form of bytes. (The method for fetching from the URL is not shown here.)
Next, you can proceed with this code:
if (x != null) {
await Share.shareXFiles(
[XFile(x.path)],
sharePositionOrigin: Rect.fromCircle(
radius: size.width * 0.25,
center: const Offset(0, 0),
),
);
} else {
Fluttertoast.showToast(msg: "Failed to share");
}
This way, you can share the PDF file.
Hope this helps you. Thank you!"
Upvotes: 3
Reputation: 101
Check out this package. It allows you to share all types of data with other apps https://pub.dev/packages/share_plus
Upvotes: 0