error The argument type 'PdfImage' can't be assigned to the parameter type 'ImageProvider'

Issue

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?

Solution

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());
  }

Answered By – Peter Koltai

Answer Checked By – Dawn Plyler (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *