flutter web, display image from pdfFile assets - flutter

hey guys I need to convert image from pdf file!
i have an experience image to pdf but
Future<void> addPage(pw.Document pdf, String filename) async {
final imageByteData = await rootBundle.load('assets/$filename');
// Convert ByteData to Uint8List
final imageUint8List = imageByteData.buffer.asUint8List(imageByteData.offsetInBytes, imageByteData.lengthInBytes);
final image = pw.MemoryImage(imageUint8List);
pdf.addPage(
pw.Page(
build: (pw.Context context) {
return pw.Center(
child: pw.Image(image),
); // Center
},
),
);
}
i have tried this kind of solution didn't work as I want
so for examople if i have 3 files in pdf file I need to change each files to image and display it

Related

cannot generate PDF file when i build on windows , it only works in debug not in release

final pdf = pw.Document();
final Uint8List fontData =
File('assets/AbhayaLibre-Regular.ttf').readAsBytesSync();
final ttf = pw.Font.ttf(fontData.buffer.asByteData());
pdf.addPage(pw.Page(
pageFormat: PdfPageFormat.a4,
build: (pw.Context context) {
return pw.Center(
child:
pw.Text(dilekce, style: pw.TextStyle(font: ttf, fontSize: 40)),
); // Center
}));
final Directory directory = await getApplicationSupportDirectory();
var path = directory.path;
print(path);
final file = File("C:\\Users\\Hakan\\Desktop\\example.pdf");
await file.writeAsBytes(await pdf.save()).whenComplete(() => print("done"));
working release and debug mod but building windows app not save pdf file

Is there a way to convert html to image using flutter

I am trying to convert HTML to image,
I tried this via the screenshot, but when the html is height than the display screen, the image is cropped
/// Convert Html to Image
Future<File> htmlToImage(
String html, String targetPath, String fileName) async {
Uint8List img = await screenshotController.captureFromWidget(MediaQuery(
data: const MediaQueryData(),
child: Html(
shrinkWrap: true,
data: html,
),
));
File file = await File('$targetPath/$fileName.jpg').create();
file.writeAsBytesSync(img);
return file;
}
Is there a better way to achieve this, or can this problem be solved

error The argument type 'PdfImage' can't be assigned to the parameter type 'ImageProvider'

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 is
Future 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?
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());
}

Flutter How can we convert Uint8List image to .jpg / .png?

I am using flutter_inappwebview to take screenshot of the webpage. The screenshot in is Uint8List format. I want to change it into File so that I can save it in my server.
Here is an example with path being the full path to your image and img your Uint8List:
File newFile = await File(path).writeAsBytes(img);
getApplicationDocumentsDirectory() is from path_provider package.
onPressed: () async {
final result = await webView.takeScreenshot();
final directory = (await getApplicationDocumentsDirectory()).path; // to get path of the file
String fileName = DateTime.now().toIso8601String(); // the name needs to be unique every time you take a screenshot
var path = '$directory/$fileName.png';
File image = await File(path).writeAsBytes(result); // thanks to the answer above by #Guillaume Roux
showDialog(
context: context,
builder: (context) {
return AlertDialog(
content: Image.file(image),
);
},
);
},

Flutter : How to save widget to png with transparent background?

There is the way to save the widget into transparent png and save it into gallery?
Thanks in advance.
Flutter draws every single pixel, so you can easily convert your widget to an image.
You need to follow these steps:
Edit -- first import path_provider link to pupspec.yaml and then follow this steps
Create a GlobalKey
final _globalKey = GlobalKey();
Create Uint8List
Uint8List pngBytes;
Wrap your widget with RepaintBoundary widget & pass in the key
RepaintBoundary(
key: _globalKey,
child: YourWidget(),
),
Create a method to convert your widget to image:
Future<void> _capturePng() async {
try {
final RenderRepaintBoundary boundary =
_globalKey.currentContext.findRenderObject();
final image = await boundary.toImage(pixelRatio: 2.0); // image quality
final byteData = await image.toByteData(format: ui.ImageByteFormat.png);
pngBytes = byteData.buffer.asUint8List();
} catch (e) {
print(e);
}
}
Convert your image to a file so that you can save it in your app
Future<File> convertImageToFile(Uint8List image) async {
final file = File(
'${(await
getTemporaryDirectory()).path}/${DateTime.now().millisecondsSinceEpoch}.png');
await file.writeAsBytes(image);
return file;
}