Home > Net >  I am using pdf lib to generate pdf and I want to add a simple text to pdf coming from the API, the t
I am using pdf lib to generate pdf and I want to add a simple text to pdf coming from the API, the t

Time:10-27

I am using pdf lib to generate pdf and I want to add a text in pdf coming from the API, the text can be very long like 3,4 pages or more. Below is my code.

List<pw.Widget> widgets = [];
 widgets.add(pw.Text(provider.getExtractedText));
  final pdf = pw.Document();

      pdf.addPage(

        pw.MultiPage(

          build: (pw.Context context) {
            return [
              pw.Wrap(
                children: widgets
              )
            ];
          },
        ),
      );

but i am getting the following exception.

E/flutter (19405): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Exception: This widget created more than 20 pages. This may be an issue in the widget or the document. See https://pub.dev/documentation/pdf/latest/widgets/MultiPage-class.html
E/flutter (19405): #0      MultiPage.generate.<anonymous closure> (package:pdf/src/widgets/multi_page.dart:251:11)
E/flutter (19405): #1      MultiPage.generate (package:pdf/src/widgets/multi_page.dart:255:8)
E/flutter (19405): #2      Document.addPage (package:pdf/src/widgets/document.dart:118:10)
E/flutter (19405): #3      _DocumentConversionScreenState.saveFile (package:speak_and_translate/screens/document_conversion_screen.dart:215:11)
E/flutter (19405): <asynchronous suspension>
E/flutter (19405): 

CodePudding user response:

Your stacktrace gives you file and line number - so go and look there:

    // Detect too big widgets
    if (sameCount   > maxPages) {
      throw Exception(
          'This widget created more than $maxPages pages. This may be an issue in the widget or the document. See https://pub.dev/documentation/pdf/latest/widgets/MultiPage-class.html');
    }

As you see, it compares to maxPages. Where is that set? Check line 152:

  this.maxPages = 20,

So, change that default to a sensible number for your application:

    pw.MultiPage(
      maxPages: 40, // <- add max pages here
      build: (pw.Context context) {
  • Related