I am new to flutter . I am try to design a pdf file that have a long table. but table don't split automatically . if the table longer than an a4 size the pdf file will not generate. is there a simple solution for that I am missing?
if the counter is longer than 25 the pdf will not generate. yes I know for one table it easy to use if/else function and generate two tables but in my main project I have multiple tables that have different columns numbers. so I need a solution that make the tables split automatically.
import 'package:flutter/services.dart';
import 'package:path_provider/path_provider.dart';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart';
import 'package:printing/printing.dart';
import 'dart:math' as math;
Future<void> testPdf() async {
final Document pdf = Document();
var myFont = Font.ttf(await rootBundle.load("assets/fonts/HacenTunisia.ttf"));
List<TableRow> buildTable(
{ Context context, int count = 10, bool repeatHeader = false}) {
final rows = <TableRow>[];
{
final tableRow = <Widget>[];
for (final cell in <String>['Hue', 'Color', 'RGBA']) {
tableRow.add(Container(
alignment: Alignment.center,
margin: const EdgeInsets.all(5),
child: Text(cell, style: Theme.of(context).tableHeader)));
}
rows.add(TableRow(children: tableRow, repeat: repeatHeader));
}
for (var y = 0; y < count; y++) {
final h = math.sin(y / count) * 365;
final PdfColor color = PdfColorHsv(h, 1.0, 1.0);
final tableRow = <Widget>[
Container(
margin: const EdgeInsets.all(5),
child: Text('${h.toInt()}°', style: Theme.of(context).tableCell)),
Container(
margin: const EdgeInsets.all(5),
decoration: BoxDecoration(
color: color,
),
height: Theme.of(context).tableCell.fontSize),
Container(
margin: const EdgeInsets.all(5),
child: Text(color.toHex(), style: Theme.of(context).tableCell)),
];
rows.add(TableRow(children: tableRow));
}
return rows;
}
pdf.addPage(MultiPage(
theme: ThemeData.withFont(
base: myFont,
),
pageFormat: PdfPageFormat.a4,
build: (Context context) {
return [
Column(children: [
Container(
// margin: EdgeInsets.fromLTRB(22, 5, 22, 5),
child: Directionality(
textDirection: TextDirection.rtl,
child: Table(
children: buildTable(context: context, count: 30),
border: TableBorder.all(),
tableWidth: TableWidth.max,
),
),
),
SizedBox(height: 20),
])
];
}));
final String dir = (await getApplicationDocumentsDirectory()).path;
//final String VoucherNo =request['VoucherNo'];
final String path = '$dir/1.pdf';
final File file = File(path);
await file.writeAsBytes(pdf.save());
await Printing.sharePdf(bytes: pdf.save(), filename: 'report.pdf');
}