pdf Flutter,如何将图片(JPG/ PNG等)从资产附加到PDF中

cig3rfwq  于 2023-04-22  发布在  Flutter
关注(0)|答案(2)|浏览(147)

使用pdf库中的Image小部件时遇到问题
当前版本: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的返回类型,它会提示我把它变成一个未来。
我也尝试过这个方法,但它从不更新该值,因此它仍然为null

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");
eyh26e7m

eyh26e7m1#

使用rootBundle从资源加载图像

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

ar7v8xwq2#

final ByteData image = await rootBundle.load('assets/image/your_image.jpg');

Uint8List imageData = (image).buffer.asUint8List();

使用它的一个例子:

pw.Container(
    width: 50.0,
    height: 50.0,
    child: pw.Image(pw.MemoryImage(logoData))
)

相关问题