【问题标题】:pdf Flutter, how to attach images (JPG/ PNG etc) from assets into a pdfpdf Flutter,如何将资产中的图像(JPG/PNG 等)附加到 pdf 中
【发布时间】:2021-07-31 14:19:57
【问题描述】:

使用 pdf 库中的图像小部件时遇到问题

当前版本: pdf: ^3.3.0

代码:

   final imageA = PdfImage.file(
      pdf.document,
      bytes: File('lib/Images/rocket14.jpg').readAsBytesSync(),
    );


child: pw.Image(pw.ImageImage(pdfimageA)),

错误:

 Unhandled Exception: type 'PdfImage' is not a subtype of type 'Image'

我完全不知道如何将 Pdfimage 解析为图像,最初我有

更新:

使用

 Future<MemoryImage> convtoImage(String name) async => pw.MemoryImage((await rootBundle.load('assets/img/$name.jpg')).buffer.asUint8List(),);

导致捕获 22 的位置

error: A value of type 'MemoryImage' can't be returned from the function 'convtoImage' because it has a return type of 'Future<MemoryImage>'. (return_of_invalid_type at [scanrecopdfmaker] lib\main.dart:490)

但如果我将其设为 memimage 的返回类型,它会提示我将其变回未来。

也试过了,但它从不更新值,所以它仍然为空

Future<Uint8List> convtoImage(String name) async {
   ///1
   var a = await rootBundle.load('lib/Images/$name.jpg');
   Uint8List data = (await rootBundle.load('lib/Images/rocket14.jpg'))
       .buffer
       .asUint8List();
  // print("data IS $data");

   // var b = pw.MemoryImage(a.buffer.asUint8List());

  return  data;
 }
 var done;
  var tempa =  convtoImage('rocket14').whenComplete(() {
    print("done");
  }).then((value) => done=value);

print("AA IS $tempa");
  print("BIS $done");

  while(done == null){
    print(done);
   }

  print("Finito");

【问题讨论】:

    标签: image flutter pdf


    【解决方案1】:

    使用 rootBundle 从资源中加载图片

    pw.MemoryImage(
    (await rootBundle.load('assets/img/your_image.jpg')).buffer.asUint8List(),
    );
    

    【讨论】:

    • 不要忘记在pubspec.yaml中添加资产
    • 试过了,但有一些错误。更新了我的问题。我一定是错过了什么
    • 您必须在函数 convToImage 中遇到类型错误,因为它的返回类型是 Future 而不是 Future
    猜你喜欢
    • 2021-11-24
    • 2020-06-24
    • 1970-01-01
    • 2017-03-01
    • 2018-07-17
    • 1970-01-01
    • 2020-08-07
    • 2020-08-18
    • 1970-01-01
    相关资源
    最近更新 更多