Flutter -PDF -- 错误 - 该小部件创建了 20 多个页面。这可能是小部件或文档中的问题

问题描述 投票:0回答:6

我正在创建一个内容较大的 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 并将其换行到下一页 文本只是一组 Lorem Ipsum 。

感谢您的帮助

flutter flutter-layout flutter-plugin
6个回答
2
投票

您可以简单地使用这个。

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

2
投票

可能可以解决您的问题

我粘贴了链接的代码,以防您无法输入。

假设我们有一个列表

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

1
投票

我认为这是因为桌子的长度。尝试拆分表格,以免单个表格超过 20 页。


1
投票

这是类的构造函数

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 更改为你想要的。


0
投票

您必须在 Multipage 包内进行更改。

maxPages默认值为20,您可以增加它。

类 MultiPage 扩展了 Page {

MultiPage({ PageTheme? pageTheme, PdfPageFormat? pageFormat, required BuildListCallback build, this.mainAxisAlignment = MainAxisAlignment.start, this.crossAxisAlignment = CrossAxisAlignment.start, this.header, this.footer, ThemeData? theme, **this.maxPages = 1000,** PageOrientation? orientation, EdgeInsetsGeometry? margin, TextDirection? textDirection, })
    

-3
投票
您必须修复 maxPage:

doc.addPage( pw.MultiPage( maxPages: 100, . . ) )
    
© www.soinside.com 2019 - 2024. All rights reserved.