错误参数类型“PdfImage”无法分配给参数类型“ImageProvider”

2023-12-31

我正在尝试使用 flutter 中的屏幕截图和 pdf 插件从屏幕截图制作 pdf。

当我将 Uint8List 传递给 pdf 创建函数时,出现错误PdfImage.file(pdf.document, bytes: screenShot 参数类型“PdfImage”无法分配给参数类型“ImageProvider”转换为pdf的代码是

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

并将屏幕截图传递给 pdf 函数如下

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

谁能帮我这个?


试试这个方法:

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());
  }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

错误参数类型“PdfImage”无法分配给参数类型“ImageProvider” 的相关文章

随机推荐