Updated: This error is on IOS only. I dont know why but its working fine on android. I was facing an issue in Flutter using the pdf package and the issue was: Widget won't fit into the page as its height (823.408) exceed a page height (728.5039370078739). You probably need a SpanningWidget or use a single page layout.
Then I try to warp my pdf content to a SpanningWidget i.e. Wrap widget. But after I get another which seems to be a new error.
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Instance of 'TooManyPagesException' #0 MultiPage.generate (package:pdf/src/widgets/multi_page.dart:251:9) #1 Document.addPage (package:pdf/src/widgets/document.dart:117:10) #2 _QuillPageState.generatePdf (package:daiaryapp/pages/addDairyPage/quill_page.dart:76:9) <asynchronous suspension> #3 _QuillPageState.build.<anonymous closure>.<anonymous closure> (package:daiaryapp/pages/addDairyPage/quill_page.dart:320:48) <asynchronous suspension>
This is my Code :
pdf.addPage(pw.MultiPage(
build: (pw.Context context) {
final content = <pw.Widget>[];
content.add(
pw.Center(
child: pw.Text(
post.dairyName.toString(), // Replace with your desired title
style: customTitle,
),
),
);
int itemsPerPage = 8;
for (var i = 0; i < deltaData['ops'].length; i++) {
final op = deltaData['ops'][i];
if (op.containsKey('insert')) {
if (op['insert'] is String) {
final lines = (op['insert'] as String).split('-');
for (var line in lines) {
final textWidget = pw.Padding(
padding: pw.EdgeInsets.all(1.0),
child: pw.Text(
line.trim(), // Trim to remove extra spaces
style: customTextStyle, // Apply custom text style here
),
);
content.add(textWidget);
}
} else if (op['insert'] is Map &&
op['insert'].containsKey('image')) {
final imageData = op['insert']['image'].split(',')[1];
final Uint8List decodedImage = base64Decode(imageData);
final imageWidget = pw.Padding(
padding: pw.EdgeInsets.all(1.0),
child: pw.Image(
pw.MemoryImage(
decodedImage,
),
fit: pw.BoxFit.cover,
height: 245.0,
),
);
content.add(imageWidget);
}
}
if (i > 0 && (i + 1) % itemsPerPage == 0) {
content.add(pw.Container(width: 1.0, height: 1.0));
}
}
// return content;
return [
pw.Wrap(
children: content,
),
];
},
));
I have tried multiple things, including Stackoverflow questions, GPT, and GitHub issue but didn't get anything helpful
Basically column or wrap widget must be parent widget otherwise it takes a full page to expand it self. And when the content is lengthy then a page inside a column or wrap it gives this error. Don't use Wrap as your child widget.
If you have a infinity list, you must split your list into some chunks and add a page for each:
Future<pw.Document> createAccountPDF(List<User> accounts) async {
final pdf = pw.Document();
final Uint8List fontData = await getBase64Resource('libraries/design_system/assets/fonts/font.ttf');
final ttf = pw.Font.ttf(fontData.buffer.asByteData());
const int itemsPerPage = 12;
// Split the invoice items into chunks of itemsPerPage
List<List<User>> chunks = [];
for (var i = 0; i < accounts.length; i += itemsPerPage) {
chunks.add(accounts.sublist(
i,
i + itemsPerPage > accounts.length ? accounts.length : i + itemsPerPage,
));
}
for (var i = 0; i < chunks.length; i++) {
final chunk = chunks[i];
pdf.addPage(
pw.Page(
pageFormat: PdfPageFormat.a5,
margin: const pw.EdgeInsets.all(24),
build: (pw.Context context) {
return pw.Container(
margin: pw.EdgeInsets.all(6),
decoration: pw.BoxDecoration(border: pw.Border.all()),
child: pw.Column(
children: [
...chunk.map(
(acc) {
return pw.Column(
children: [
pw.Row(
children: [pw.Text("Some text")],
),
],
);
},
),
],
),
);
},
),
);
}
return pdf;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With