Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter -PDF -- Error - This widget created more than 20 pages. This may be an issue in the widget or the document

I am creating a PDF document that has a large content. However, I am not able to show the PDF and I get the error

"This widget created more than 20 pages. This may be an issue in the widget or the document."

I know the text is too large but I don't know how to make it work

    pdf.addPage(
    pw.MultiPage(
      pageFormat: PdfPageFormat.a4,
      orientation: pw.PageOrientation.portrait,
      crossAxisAlignment: pw.CrossAxisAlignment.start,
      build: (pw.Context context) {
        return <pw.Widget>[
          pw.Wrap(
            children: <pw.Widget>[
              pw.Container(
                width: PdfPageFormat.a4.width,
                child: pw.Row(
                  mainAxisSize: pw.MainAxisSize.min,
                  crossAxisAlignment: pw.CrossAxisAlignment.start,
                  children: <pw.Widget>[
                    pw.Expanded(
                      child: pw.Column(
                        mainAxisSize: pw.MainAxisSize.min,
                        crossAxisAlignment: pw.CrossAxisAlignment.start,
                        children: <pw.Widget>[
                          pw.SizedBox(height: 8.0),
                          for (int i = 0; i < data['employers'].length; i++)
                            pw.Column(
                              mainAxisSize: pw.MainAxisSize.min,
                              crossAxisAlignment: pw.CrossAxisAlignment.start,
                              children: <pw.Widget>[
                                pw.Text(
                                  "${data['employers'][i]['duties']}",
                                  style: pw.TextStyle(
                                    fontSize: 12.0,
                                  ),
                                  softWrap: true,
                                ),
                              ],
                            ),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ];
      },
    ),
  );

I need some help on how to restructure my code so that I can display the PDF and have it wrap to the next page The text is just a a set of Lorem Ipsum .

Thanks for your help

like image 610
user2570135 Avatar asked May 08 '20 12:05

user2570135


2 Answers

You can simply use this.

import 'package:ferns_restaurant/constants/Constants.dart';
import 'package:pdf/widgets.dart' as pw;
import 'package:pdf/widgets.dart';
import 'package:printing/printing.dart';
import 'package:woocommerce/models/order.dart';

class CustomPrinter {
  final pdf = pw.Document();

  print(WooOrder order) {
    List<LineItems> _product = order.lineItems;

    Printing.layoutPdf(
      onLayout: (format) {
        final doc = pw.Document();
        doc.addPage(
          pw.MultiPage(
              crossAxisAlignment: pw.CrossAxisAlignment.start,
              build: (pw.Context context) => <pw.Widget>[
                    _topHeaderLayout(),
                    for (int i = 0; i < _product.length; i++)
                      _productLayout(_product[i]),
                  ]),
        );
        return doc.save();
      },
      name: 'order_id_#' + order.id.toString(),
    );
  }

  _topHeaderLayout() {
    return new pw.Container(
        // height: 60.0,
        margin: pw.EdgeInsets.only(bottom: 20.0),
        child: pw.Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            pw.Expanded(
              child: pw.Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <pw.Widget>[
                  pw.Container(
                    child: pw.Text(
                      'ITEM',
                      style: pw.TextStyle(
                          fontWeight: pw.FontWeight.bold,
                          fontSize: Constants.PRINTER_BASE_FONT_SIZE),
                    ),
                  ),
                ],
              ),
            ),
            pw.Expanded(
              child: pw.Column(
                crossAxisAlignment: CrossAxisAlignment.end,
                children: <pw.Widget>[
                  pw.Text(
                    'PRICE',
                    style: pw.TextStyle(
                        fontWeight: pw.FontWeight.bold,
                        fontSize: Constants.PRINTER_BASE_FONT_SIZE),
                  ),
                ],
              ),
            )
          ],
        ));
  }

  _productLayout(LineItems item) {
    return new pw.Container(
        // height: 60.0,
        margin: pw.EdgeInsets.only(bottom: 20.0),
        child: pw.Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            pw.Expanded(
              child: pw.Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <pw.Widget>[
                  pw.Container(
                    child: pw.Text(
                      item.name.toString() + ' - ID: ' + item.id.toString(),
                      style: pw.TextStyle(
                          fontWeight: pw.FontWeight.bold,
                          fontSize: Constants.PRINTER_BASE_FONT_SIZE),
                    ),
                  ),
                  pw.Container(
                    child: Text(
                        'Qty: ' +
                            item.quantity.toString() +
                            ' x Cost: ' +
                            '£' +
                            item.price,
                        style: pw.TextStyle(
                            fontWeight: pw.FontWeight.normal,
                            fontSize: Constants.PRINTER_BASE_FONT_SIZE)),
                  ),
                ],
              ),
            ),
            pw.Expanded(
              child: pw.Column(
                crossAxisAlignment: CrossAxisAlignment.end,
                children: <pw.Widget>[
                  pw.Text(
                    '£' + item.total.toString(),
                    style: pw.TextStyle(
                        fontWeight: pw.FontWeight.normal,
                        fontSize: Constants.PRINTER_BASE_FONT_SIZE),
                  ),
                ],
              ),
            )
          ],
        ));
  }
}
like image 183
Arunjith R S Avatar answered Oct 15 '22 00:10

Arunjith R S


I think this is because of the length of the table. Try splitting the table so that it doesn't overcome 20 pages for a single table.

like image 39
Nilotpal Kapri Avatar answered Oct 14 '22 23:10

Nilotpal Kapri