如何将资源中的图像(JPG/PNG 等)附加到 pdf 中

lol*_*nin 4 pdf image flutter

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

当前版本:pdf:^3.3.0

代码:

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

child: pw.Image(pw.ImageImage(pdfimageA)),
Run Code Online (Sandbox Code Playgroud)

错误:

未处理的异常:“PdfImage”类型不是“Image”类型的子类型

我完全不知道如何将 Pdfimage 解析为图像。

使用

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

结果是第 22 条陷阱,其中

错误:无法从函数“convtoImage”返回“MemoryImage”类型的值,因为它的返回类型为“Future<MemoryImage>”。(return_of_invalid_type 位于 [scanrecopdfmaker] lib\main.dart:490)

如果我要把它变成记忆图像的返回类型,它会促使我把它变回未来。

我也尝试过这个,但它永远不会更新该值,因此它仍然为空

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");
Run Code Online (Sandbox Code Playgroud)

小智 21

使用 rootBundle 从资源中加载图像

pw.MemoryImage(
(await rootBundle.load('assets/img/your_image.jpg')).buffer.asUint8List(),
);
Run Code Online (Sandbox Code Playgroud)