(Flutter/Dart-PDF) How to handle long text in a Multipage Table without causing InstanceOfTooManyPages error?

26 Views Asked by At

I have a text that is so long that it spans several pages. Since it is in a table that also spans multiple pages, the TooManyPages error is thrown.

This is the code as a Table and Paragraph. Please help me to make the text span multiple pages.

// Erstelle eine Tabelle mit der hierarchischen Struktur
  pdf.addPage(pw.MultiPage(
    maxPages: 999,
    pageFormat: PdfPageFormat.a4,
    build: (context) => [
      pw.Table(
        columnWidths: {
          0: pw.FlexColumnWidth(1),
          1: pw.FlexColumnWidth(4),
          2: pw.FlexColumnWidth(1),
          3: pw.FlexColumnWidth(1),
          4: pw.FlexColumnWidth(2),
          5: pw.FlexColumnWidth(2),
        },
        children: [
          pw.TableRow(
            //decoration: pw.BoxDecoration(color: PdfColors.grey300),
            children: [
              pw.Padding(
                  padding: const pw.EdgeInsets.all(4),
                  child: pw.Text('Pos.Nr.', style: headerStyle)),
              pw.Padding(
                  padding: const pw.EdgeInsets.all(4),
                  child: pw.Text('Leistungsbeschreibung', style: headerStyle)),
              pw.Padding(
                  padding: const pw.EdgeInsets.all(4),
                  child: pw.Text('Menge', style: headerStyle)),
              pw.Padding(
                  padding: const pw.EdgeInsets.all(4),
                  child: pw.Text('Einheit', style: headerStyle)),
              pw.Padding(
                  padding: const pw.EdgeInsets.all(4),
                  child: pw.Text('Einheitspreis', style: headerStyle)),
              pw.Padding(
                  padding: const pw.EdgeInsets.all(4),
                  child: pw.Text('Gesamtpreis', style: headerStyle)),
            ],
          ),
          ...categoriesAndItems.map((item) {
            endTime = DateTime.now();
            duration = endTime.difference(startTime);
            print("PDF item created. (after ${duration.inMilliseconds}ms)");
            if (item['isCategory']) {
              return pw.TableRow(
                children: [
                  pw.Padding(
                    padding: const pw.EdgeInsets.all(4),
                    child: pw.Text(item['posnr'],
                        style: textStyles[item['level'] - 1]),
                  ),
                  pw.Padding(
                    padding: const pw.EdgeInsets.all(4),
                    child: pw.Column(
                        crossAxisAlignment: pw.CrossAxisAlignment.start,
                        children: [
                          pw.Text(item['position'],
                              style: textStyles[item['level'] - 1]),
//here the critical text block
                          pw.Padding(
                              padding: const pw.EdgeInsets.all(4),
                              child: pw.Paragraph(
                                  text: item['description'],
                                  style: textStyles[3])),
                        ]),
                  ),
                  pw.Padding(
                      padding: const pw.EdgeInsets.all(4),
                      child: pw.Text('', style: textStyles[item['level'] - 1])),
                  pw.Padding(
                      padding: const pw.EdgeInsets.all(4),
                      child: pw.Text('', style: textStyles[item['level'] - 1])),
                  pw.Padding(
                      padding: const pw.EdgeInsets.all(4),
                      child: pw.Text('', style: textStyles[item['level'] - 1])),
                  pw.Padding(
                      padding: const pw.EdgeInsets.all(4),
                      child: pw.Text(item['totalPrice'],
                          style: textStyles[item['level'] - 1])),
                ],
              );
            } else {
              return pw.TableRow(
                children: [
                  pw.Padding(
                    padding: const pw.EdgeInsets.all(4),
                    child: pw.Text(item['posnr'], style: textStyles[2]),
                  ),
                  pw.Padding(
                    padding: const pw.EdgeInsets.all(4),
                    child: pw.Column(
                        crossAxisAlignment: pw.CrossAxisAlignment.start,
                        children: [
                          pw.Text(item['position'], style: textStyles[2]),
                          pw.Padding(
                              padding: const pw.EdgeInsets.all(4),
//here another critical text block
                              child: pw.Paragraph(
                                  text: item['description'],
                                  style: textStyles[3])),
                        ]),
                  ),
                  pw.Padding(
                    padding: const pw.EdgeInsets.all(4),
                    child: pw.Align(
                      alignment: pw
                          .Alignment.centerRight, // Setzt den Text rechtsbündig
                      child: pw.Text(item['quantity'], style: textStyles[2]),
                    ),
                  ),
                  pw.Padding(
                      padding: const pw.EdgeInsets.all(4),
                      child: pw.Text(item['unit'], style: textStyles[2])),
                  pw.Padding(
                      padding: const pw.EdgeInsets.all(4),
                      child: pw.Text(item['unitPrice'], style: textStyles[2])),
                  pw.Padding(
                      padding: const pw.EdgeInsets.all(4),
                      child: pw.Text(item['totalPrice'], style: textStyles[2])),
                ],
              );
            }
          }),
        ],
      ),
    ],
  ));

If the text is shorter than a page, my code works.

I tried, paragraph, text, Wraps and so on. I even converted everything to containers and rows and the same error is thrown.

0

There are 0 best solutions below