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

804

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());
  }
Share:
804
Ananthakrishna
Author by

Ananthakrishna

Intend to build a career with leading with leading co-operate of high-tech environment with committed and dedicated people. And also i like to learn new technologies and contributing my part in it.

Updated on January 01, 2023

Comments

  • Ananthakrishna
    Ananthakrishna over 1 year

    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?

    • Peter Koltai
      Peter Koltai over 2 years
      What about MemoryImage?
    • Ananthakrishna
      Ananthakrishna over 2 years
      @PeterKoltai sorry i don't get you!
    • Peter Koltai
      Peter Koltai over 2 years
      Do you use this package?
    • Ananthakrishna
      Ananthakrishna over 2 years
      @PeterKoltai yes that package is used for converting that screenshot to pdf that is part where i got the error, you can see it in the above code.
    • Peter Koltai
      Peter Koltai over 2 years
      This package has a method called MemoryImage, I linked it, unlike pw.Image it accepts Uint8List as input, give it a try.
    • Ananthakrishna
      Ananthakrishna over 2 years
      @PeterKoltai Can you add this in my code above?
    • Peter Koltai
      Peter Koltai over 2 years
      Try: child: pw.MemoryImage(screenShot)
    • Ananthakrishna
      Ananthakrishna over 2 years
      @PeterKoltai The argument type 'MemoryImage' can't be assigned to the parameter type 'Widget error