Код: Выделить всё
I Use RepaintBoundary | screenshot | Pdf |
Код: Выделить всё
body: Screenshot(
controller: _screenshotController,
child: SingleChildScrollView(
controller: _scrollController,
scrollDirection: Axis.horizontal,
child: SingleChildScrollView(
scrollDirection: Axis.vertical,
child: RepaintBoundary(
key: _screenshotKey,
child: Container(
color: Colors.white,
width: 2100.0, // Set a fixed width or use a sized container
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children:
Код: Выделить всё
final boundary = _screenshotKey.currentContext!.findRenderObject()
as RenderRepaintBoundary;
boundary.toImage(pixelRatio: 1.0).then((image) async {
ByteData? byteData =
await image.toByteData(format: ui.ImageByteFormat.png);
Uint8List uint8List = byteData!.buffer.asUint8List();
switch (choice) {
case 'Pdf':
getPdf(uint8List);
break;
case 'ScreenShoot':
Uint8List imageBytes = uint8List;
final output = await getExternalStorageDirectory();
final file = File('${output?.path}/example.png');
await file.writeAsBytes(imageBytes);
// Open the generated PDF file
OpenFile.open(file.path);
}
});
Код: Выделить всё
Future getPdf(Uint8List screenShot) async {
final pdf = pw.Document();
final img.Image? longScreenshot = img.decodeImage(screenShot);
if (longScreenshot == null) {
throw Exception('Could not decode image');
}
final double a4Height = 3540; // in pixels
final int pageCount = (longScreenshot.height / a4Height).ceil();
//Add screenshot sections to separate pages
for (int page = 0; page < pageCount; page++) {
int start = (page * a4Height).toInt();
int end = ((page + 1) * a4Height).toInt();
if (end > longScreenshot.height) {
end = longScreenshot.height;
}
final img.Image section = img.copyCrop(
longScreenshot,
x: 0,
y: start,
width: longScreenshot.width,
height: end - start,
);
Uint8List pngBytes = img.encodePng(section);
pdf.addPage(pw.Page(
build: (pw.Context context) {
return pw.Expanded(
child: pw.Image(pw.MemoryImage(pngBytes)),
);
},
));
}
final output = await getExternalStorageDirectory();
final file = File('${output?.path}/example.pdf');
await file.writeAsBytes(await pdf.save());
// Open the generated PDF file
OpenFile.open(file.path);
}
Код: Выделить всё
pixelRatio: 3.0
Код: Выделить всё
final int pageCount = (longScreenshot.height / a4Height).ceil();
Помогите мне с этим.
Заранее спасибо.
Подробнее здесь: https://stackoverflow.com/questions/783 ... ze-problem