Unhandled Exception: Instance of 'TooManyPagesException' #0 MultiPage.generate

262 Views Asked by At

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

1

There are 1 best solutions below

0
On

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.