error The argument type 'PdfImage' can't be assigned to the parameter type 'ImageProvider'
804
Try this way:
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());
}
Author by
Ananthakrishna
Intend to build a career with leading with leading co-operate of high-tech environment with committed and dedicated people. And also i like to learn new technologies and contributing my part in it.
Updated on January 01, 2023Comments
-
Ananthakrishna over 1 year
I am trying to make pdf from screenshot with screenshot and pdf plugins in flutter.
When I pass Uint8List to pdf creation function I am getting error at
PdfImage.file(pdf.document, bytes: screenShot
The argument type 'PdfImage' can't be assigned to the parameter type 'ImageProvider' The code to convert to pdf isFuture 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()); }
and passing the screenshot to the pdf function is below
Uint8List _imageFile; screenshotController.capture().then((Uint8List image) { //Capture Done setState(() { _imageFile = image; }); }).catchError((onError) { print(onError); }); getPdf(_imageFile); },
Can anyone help me with this?
-
Peter Koltai over 2 yearsWhat about MemoryImage?
-
Ananthakrishna over 2 years@PeterKoltai sorry i don't get you!
-
Peter Koltai over 2 yearsDo you use this package?
-
Ananthakrishna over 2 years@PeterKoltai yes that package is used for converting that screenshot to pdf that is part where i got the error, you can see it in the above code.
-
Peter Koltai over 2 yearsThis package has a method called MemoryImage, I linked it, unlike
pw.Image
it acceptsUint8List
as input, give it a try. -
Ananthakrishna over 2 years@PeterKoltai Can you add this in my code above?
-
Peter Koltai over 2 yearsTry:
child: pw.MemoryImage(screenShot)
-
Ananthakrishna over 2 years@PeterKoltai The argument type 'MemoryImage' can't be assigned to the parameter type 'Widget error
-