DavBfr / dart_pdf

Pdf creation module for dart/flutter
https://pub.dev/packages/pdf
Apache License 2.0
1.38k stars 611 forks source link

Problem with multipage with a bunch of data #1530

Open Interscope19 opened 9 months ago

Interscope19 commented 9 months ago

Hello guys, i have a problem with a bunch of data that comes from a request, in the past it just works but now doesnt, it throws Error: Instance of 'TooManyPagesException' the length of the list its about 154 but even if i try with a.take() method over that list to take only 10 rows and it throws thisError: RangeError (index): Index out of range: index should be less than 501832: 501835 this is the full message Error: RangeError (index): Index out of range: index should be less than 501832: 501835 dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 294:3 throw_ dart-sdk/lib/_internal/js_dev_runtime/private/native_typed_data.dart 1802:5 _checkValidIndex dart-sdk/lib/_internal/js_dev_runtime/private/native_typed_data.dart 1059:5 _get] packages/pdf/src/pdf/obj/image.dart 228:21 _alpha packages/pdf/src/pdf/obj/image.dart 86:31 new packages/pdf/src/svg/image.dart 76:19 fromXml packages/pdf/src/svg/operation.dart 53:23 fromXml packages/pdf/src/svg/group.dart 45:37 <fn> dart-sdk/lib/internal/iterable.dart 403:20 moveNext dart-sdk/lib/internal/iterable.dart 882:20 moveNext dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 1277:20 next packages/pdf/src/svg/group.dart 61:16 paintShape packages/pdf/src/svg/operation.dart 98:5 paint packages/pdf/src/svg/painter.dart 46:46 paint packages/pdf/src/widgets/svg.dart 130:12 paint packages/pdf/src/widgets/widget.dart 320:7 paintChild packages/pdf/src/widgets/basic.dart 420:5 paint packages/pdf/src/widgets/basic.dart 127:7 paint packages/pdf/src/widgets/widget.dart 320:7 paintChild packages/pdf/src/widgets/basic.dart 449:5 paint packages/pdf/src/widgets/widget.dart 260:7 paint packages/pdf/src/widgets/flex.dart 521:12 paint packages/pdf/src/widgets/widget.dart 320:7 paintChild packages/pdf/src/widgets/flex.dart 594:5 paint packages/pdf/src/widgets/flex.dart 521:12 paint packages/pdf/src/widgets/flex.dart 521:12 paint packages/pdf/src/widgets/multi_page.dart 202:12 [_paintChild] packages/pdf/src/widgets/multi_page.dart 438:9 postProcess packages/pdf/src/widgets/document.dart 130:13 save dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 84:54 runBody dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 127:5 _async packages/pdf/src/widgets/document.dart 127:25 save packages/chekin/app/modules/admin/controllers/admin_controller.dart 1262:30 crearPdf dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 84:54 runBody dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 127:5 _async packages/chekin/app/modules/admin/controllers/admin_controller.dart 1260:24 crearPdf packages/chekin/app/modules/admin/controllers/admin_controller.dart 866:11 abrirPdfTotal dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 45:50 <fn> dart-sdk/lib/async/zone.dart 1661:54 runUnary dart-sdk/lib/async/future_impl.dart 162:18 handleValue dart-sdk/lib/async/future_impl.dart 846:44 handleValueCallback dart-sdk/lib/async/future_impl.dart 875:13 _propagateToListeners dart-sdk/lib/async/future_impl.dart 647:5 [_completeWithValue] dart-sdk/lib/async/future_impl.dart 721:7 callback dart-sdk/lib/async/schedule_microtask.dart 40:11 _microtaskLoop dart-sdk/lib/async/schedule_microtask.dart 49:5 _startMicrotaskLoop dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 181:7 <fn>

I tried to reproduce the behaviour with the invoice example extending the quantity of data in the doc example ( invoice) and happens the same and i already tried to wrap it in flexible, column, wrap widgets with same behaviour i tried this tho #1354 .

To Reproduce Code snippet to reproduce the behavior:

    final String logo = await rootBundle.loadString('assets/Logo.svg');
  pw.Widget buildHeaderPdfTotal(pw.Context context,String? logo) {
      return pw.Column(
        children: [
          pw.Row(
            crossAxisAlignment: pw.CrossAxisAlignment.start,
            children: [
              pw.Expanded(
                child: pw.Column(
                  children: [
                    pw.Container(
                      height: 50,
                      padding: const pw.EdgeInsets.only(left: 20),
                      alignment: pw.Alignment.centerLeft,
                      child: pw.Text(
                        'Reporte general horario',
                        style: pw.TextStyle(
                          color: PdfColors.black,
                          fontWeight: pw.FontWeight.bold,
                          fontSize: 20,
                        ),
                      ),
                    ),
                    pw.Container(
                      decoration: const pw.BoxDecoration(
                        borderRadius:
                            pw.BorderRadius.all(pw.Radius.circular(2)),
                        color: PdfColors.white,
                      ),
                      padding: const pw.EdgeInsets.only(
                          left: 40, top: 10, bottom: 10, right: 20),
                      alignment: pw.Alignment.centerLeft,
                      height: 50,
                      child: pw.DefaultTextStyle(
                          style: const pw.TextStyle(
                            color: PdfColors.blue800,
                            fontSize: 12,
                          ),
                          child: pw.Column(children: [
                            pw.Row(children: [
                              pw.Text('Fecha de este informe:'),
                              pw.Text(DateFormat('dd/MM/yyyy')
                                  .format(DateTime.now())),
                            ])
                          ])),
                    ),
                  ],
                ),
              ),
              pw.Expanded(
                child: pw.Column(
                  mainAxisSize: pw.MainAxisSize.min,
                  children: [
                    pw.Container(
                      alignment: pw.Alignment.topRight,
                      padding: const pw.EdgeInsets.only(bottom: 8, left: 30),
                      height: 100,
                      child: logo != null
                          ? pw.SvgImage(svg: logo)
                          : pw.PdfLogo(),
                    ),
                  ],
                ),
              ),
            ],
          ),
          if (context.pageNumber > 1) pw.SizedBox(height: 20)
        ],
      );
    }

 pw.Widget contentTable(pw.Context context, List<CheckinTotal> list) {
      const tableHeaders = [
        'Nombre',
        'Rol',
        'Cantidad de dias',
        'Sobrepaso entrada',
        'Sobrepaso almuerzo',
        'Sobrepaso salida',
        'Total sobrepaso'
      ];

      return pw.TableHelper.fromTextArray(
        border: null,
        cellAlignment: pw.Alignment.centerLeft,
        headerDecoration: const pw.BoxDecoration(
          borderRadius: pw.BorderRadius.all(pw.Radius.circular(2)),
          color: PdfColors.amber,
        ),
        headerHeight: 25,
        cellHeight: 40,
        cellAlignments: {
          0: pw.Alignment.center,
          1: pw.Alignment.center,
          2: pw.Alignment.center,
          3: pw.Alignment.center,
          4: pw.Alignment.center,
        },
        headerStyle: pw.TextStyle(
          color: PdfColors.black,
          fontSize: 10,
          fontWeight: pw.FontWeight.bold,
        ),
        cellStyle: const pw.TextStyle(
          color: PdfColors.black,
          fontSize: 10,
        ),
        rowDecoration: const pw.BoxDecoration(
          border: pw.Border(
            bottom: pw.BorderSide(
              color: PdfColors.amber,
              width: .5,
            ),
          ),
        ),
        headers: List<String>.generate(
          tableHeaders.length,
          (col) => tableHeaders[col],
        ),
        data: List<List<String>>.generate(
          list.length,
          (row) {
            // print(row);
           return  List<String>.generate(
            tableHeaders.length,
            (col) {

              print(col);
              print(list[row].nombre);
              return list[row].getIndex(col);
            },
          );
          }
        ),
      );
    }

    doc.addPage(

      pw.MultiPage(

        orientation: PageOrientation.landscape,
        header: (context) => buildHeaderPdfTotal(context, logo),
        footer: (context) =>
            buildFooterPdf(context, homeController.cliente.nombre),
        build: (context) => <pw.Widget>[
                pw.Text('Rango de fecha: ' +
                DateFormat('dd/MM/yyyy').format(startdate) +
                "--" +
                DateFormat('dd/MM/yyyy').format(enddate)),
                pw.SizedBox(height: 10),
                contentTable(context,listCheckinTotal.take(1).toList()),
          // contentFooter(context)
        ],
      ),
    );

Expected behavior

Screenshots

Flutter Doctor

[✓] Flutter (Channel stable, 3.16.0, on macOS 14.0 23A344 darwin-x64, locale en-US)
    • Flutter version 3.16.0 on channel stable at /Users/kendrickgarcia/Development/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision db7ef5bf9f (5 days ago), 2023-11-15 11:25:44 -0800
    • Engine revision 74d16627b9
    • Dart version 3.2.0
    • DevTools version 2.28.2

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
    • Android SDK at /Users/kendrickgarcia/Library/Android/sdk
    • Platform android-33, build-tools 33.0.0
    • ANDROID_SDK_ROOT = /Users/kendrickgarcia/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b829.9-10027231)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.0)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15A240d
    • CocoaPods version 1.14.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2022.3)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b829.9-10027231)

[✓] VS Code (version 1.84.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.76.0

[✓] Connected device (2 available)
    • macOS (desktop) • macos  • darwin-x64     • macOS 14.0 23A344 darwin-x64
    • Chrome (web)    • chrome • web-javascript • Google Chrome 119.0.6045.159

[✓] Network resources
    • All expected network resources are available.

Desktop (please complete the following information):

Smartphone (please complete the following information):

Additional context Also this behaviour happens lastly i have a past build running ( flutter web) and it works fine is just with a new release :( help :(

bksbora commented 9 months ago

Neh i don't want to read whole your code :D Partly comment and find the problem please :,(

Interscope19 commented 9 months ago

Neh i don't want to read whole your code :D Partly comment and find the problem please :,(

just add a lot of rows to the invoice example of the docs and you will find the problem

bambinoua commented 9 months ago

@Interscope19 I also met this exception TooManyPagesException and I don't know for what it is raised. But you can fix this. The MultiPage contains maxPages parameter which is equal 20 by default. Increase it to required value.

renansteles commented 7 months ago

My app is freezing when I use an array that lenght is over them 7000 on the "TableHelper.fromTextArray()".

If the array size is less then 500, i dont have problem. I use the maxPages: 9999, because I dont know de max number of pages.