简体   繁体   中英

pdf Flutter, how to attach images (JPG/ PNG etc) from assets into a pdf

Having trouble with using the Image widget from the pdf library

Current Version: pdf: ^3.3.0

code:

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


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

Error:

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

I have absolutely no idea on how to parse a Pdfimage into an image, initially i had

UPDATE:

Using

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

results in a catch 22 where

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)

but if i were to make it a return type of memimage it will prompt me to turn it back into a future.

also tried this, but it never updates the value so it remains 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");

Use rootBundle to load image from assets

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM