Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load an existing pdf and print using printing in flutter

I've been been trying to print an existing pdf file which is in sd card. So i added printing package to my flutter project to print pdf files.

Printing package link

I checked their documentation and i can't find a way to load and print pdf.

Fyi, i can create a pdf from image, text or whaetever it is and pass it to the Printing function. That works as anticipated.

final pdf = Document();

pdf.addPage(Page(
      pageFormat: PdfPageFormat.a4,
      build: (Context context) {
        return Center(
          child: Text("Hello World"),
        );
      })
);

await Printing.layoutPdf(onLayout: (PdfPageFormat format) async => pdf.save());

Any help to load existing pdf and print would be appreciable

Thanks

like image 599
Hussain Badusha Avatar asked Oct 17 '25 13:10

Hussain Badusha


1 Answers

I finally find a way to print pdf using printing plugin.

You can directly print pdf by giving the online pdf url

To do so, just use the following dart snippet

http.Response response = await http.get('http://www.africau.edu/images/default/sample.pdf');
var pdfData = response.bodyBytes;
await Printing.layoutPdf(onLayout: (PdfPageFormat format) async => pdfData);

Don't forget to import http plugin

import 'package:http/http.dart' as http;

Thanks everyone for the help

like image 195
Hussain Badusha Avatar answered Oct 19 '25 03:10

Hussain Badusha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!