【问题标题】:error The argument type 'PdfImage' can't be assigned to the parameter type 'ImageProvider'错误无法将参数类型“PdfImage”分配给参数类型“ImageProvider”
【发布时间】:2021-11-03 05:20:33
【问题描述】:

我正在尝试使用 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);
                                          },

谁能帮我解决这个问题?

【问题讨论】:

  • MemoryImage 呢?
  • @PeterKoltai 对不起,我没听明白!
  • 你使用this包吗?
  • @PeterKoltai 是的,该软件包用于将该屏幕截图转换为 pdf,这是我收到错误的部分,您可以在上面的代码中看到它。
  • 这个包有一个叫MemoryImage的方法,我链接了它,不像pw.Image它接受Uint8List作为输入,试试看。

标签: flutter dart pdf-generation flutter-packages


【解决方案1】:

试试这个方法:

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

【讨论】:

    猜你喜欢
    • 2023-02-10
    • 2021-11-20
    • 2022-08-19
    • 2021-06-08
    • 2022-08-18
    • 2021-07-09
    • 2021-08-22
    • 2022-08-14
    • 2019-11-28
    相关资源
    最近更新 更多