5

I am trying to make pdf from screenshot with screenshot and pdf plugins in flutter.

When I pass Uint8List to pdf creation function I am getting error at PdfImage.file(pdf.document, bytes: screenShot The argument type 'PdfImage' can't be assigned to the parameter type 'ImageProvider' The code to convert to pdf is

Future getPdf(Uint8List screenShot) async {
    pw.Document pdf = pw.Document();
    pdf.addPage(
      pw.Page(
        pageFormat: PdfPageFormat.a4,
        build: (context) {
          return pw.Expanded(
              child: pw.Image(PdfImage.file(pdf.document, bytes: screenShot), fit: pw.BoxFit.contain)
          );
        },
      ),
    );
    File pdfFile = File('Your path + File name');
    pdfFile.writeAsBytesSync(await pdf.save());
  }

and passing the screenshot to the pdf function is below

 Uint8List _imageFile;
screenshotController.capture().then((Uint8List image) {
                                            //Capture Done
                                            setState(() {
                                              _imageFile = image;
                                            });
                                          }).catchError((onError) {
                                            print(onError);
                                          });
                                          getPdf(_imageFile);
                                          },

Can anyone help me with this?

Ananthakrishna
  • 517
  • 5
  • 24

1 Answers1

7

Try this way:

Future getPdf(Uint8List screenShot) async {
    pw.Document pdf = pw.Document();
    pdf.addPage(
      pw.Page(
        pageFormat: PdfPageFormat.a4,
        build: (context) {
          return pw.Expanded(
              // change this line to this:
              child: pw.Image(pw.MemoryImage(screenshot), fit: pw.BoxFit.contain),
          );
        },
      ),
    );
    File pdfFile = File('Your path + File name');
    pdfFile.writeAsBytesSync(await pdf.save());
  }
Peter Koltai
  • 8,296
  • 2
  • 10
  • 20