Flutter -PDF --错误-此小部件创建了超过20个页面,这可能是小部件或文档中的问题

zour9fqk  于 2022-12-27  发布在  Flutter
关注(0)|答案(5)|浏览(128)

我正在创建一个内容很大的PDF文档。但是,我无法显示PDF,并收到错误消息
"此小部件创建了20多个页面。这可能是小部件或文档中的问题。"
我知道文本太大,但我不知道如何使它工作

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,
                                ),
                              ],
                            ),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ];
      },
    ),
  );

我需要一些关于如何重组我的代码,这样我就可以显示PDF格式,并有它 Package 到下一页的文字只是一套洛伦Ipsum的帮助。
谢谢你的帮忙

pqwbnv8z

pqwbnv8z1#

我想这是因为表格的长度。请尝试拆分表格,这样单个表格就不会超过20页。

kse8i1jr

kse8i1jr2#

你可以简单地使用这个。

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),
                  ),
                ],
              ),
            )
          ],
        ));
  }
}
scyqe7ek

scyqe7ek3#

这可能是您问题的解决方案
我粘贴了链接的代码以防你无法输入。
假设我们有一个列表issues

pdf.addPage(
  MultiPage(
    build: (Context context) => <Widget>[
      Wrap(
        children: List<Widget>.generate(issues.length, (int index) {
          final issue = issues[index];
          return Container(
            child: Column(
              children: <Widget>[
                Header(
                    text: "Issue n°${issue.id}",
                    textStyle: TextStyle(fontSize: 20)),
                Text("Description :",
                    textAlign: TextAlign.left,
                    style: TextStyle(fontSize: 15)),
              ],
            ),
          );
        }),
      ),
    ],
  ),
);
np8igboo

np8igboo4#

这是类的构造函数

package:pdf/src/widgets/multi_page.dart 
(new) MultiPage MultiPage({   
  PageTheme? pageTheme,   
  PdfPageFormat? pageFormat,   
  required List<Widget> Function(Context) build,   
  MainAxisAlignment mainAxisAlignment = MainAxisAlignment.start,   
  CrossAxisAlignment crossAxisAlignment = CrossAxisAlignment.start,   
  Widget Function(Context)? header,   
  Widget Function(Context)? footer,   
  ThemeData? theme,   
  int maxPages = 20, //******* here the default maxPages = 20  
  PageOrientation? orientation,   
  EdgeInsets? margin,   
  TextDirection? textDirection, 
})  

Containing class: MultiPage

所以我认为你可以计算你的页数,然后把结果赋给这个属性,或者简单地把数字20改成你想要的。

8oomwypt

8oomwypt5#

您必须修复maxPage:

doc.addPage(
      pw.MultiPage(
        maxPages: 100,
        .
        .
        )
      )

相关问题