DavBfr / dart_pdf

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

Instance of toomanypagesexception #1671

Open mohammednabil95 opened 5 months ago

mohammednabil95 commented 5 months ago

I'm using this wonderful and lot's of feature plugin and i able to show multiple pages pdf. Now the issue the data is coming from api and if the data on Text widget is in bulk information then i'm getting the exception 'toomanypagesexception'. It only works if i reduce the fontsize to 10 but text display is very small. Below is the sample dart code and please help me on this issue.

class PdfPreviewWidget extends StatelessWidget {
  final Drugs drugs;
  const PdfPreviewWidget({super.key, required this.drugs});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: bodyColor,
      appBar: AppBar(
        iconTheme: const IconThemeData(color: Colors.white),
        centerTitle: false,
        elevation: 0,
        backgroundColor: primaryColor,
        title: Text(
          preview,
          style: const TextStyle(color: Colors.white, fontSize: 18.0),
        ),
      ),
      body: PdfPreview(
        previewPageMargin: EdgeInsets.zero,
        padding: EdgeInsets.zero,
        enableScrollToPage: true,
        canChangeOrientation: false,
        canChangePageFormat: false,
        canDebug: false,
        build: (context) => generatePdf(drugs),
      ),
    );
  }
}

Future<Uint8List> generatePdf(Drugs drugs) async {
  final pdf = pw.Document();
  var data = await rootBundle.load('assets/Amiri-Regular.ttf');
  final ttf = pw.Font.ttf(data);
  pdf.addPage(pw.MultiPage(
      textDirection: pw.TextDirection.rtl,
      build: (context) {
        return [
          pw.ListView.builder(
              itemCount: drugs.details!.length,
              itemBuilder: (context, index) {
                return pw.Column(
                  crossAxisAlignment: pw.CrossAxisAlignment.start,
                  children: [
                    pw.Text(drugs.details![index]!.title ?? "",
                        style: pw.TextStyle(
                          font: ttf,
                          fontSize: 25.0,
                          fontWeight: pw.FontWeight.bold,
                          color: const PdfColor.fromInt(0xFF259cd8),
                        ),
                        textDirection: pw.TextDirection.rtl),
                    pw.SizedBox(height: 5.0),
                    pw.Column(
                        children: drugs.details![index]!.details!.map((e) {
                      return pw.Row(
                        crossAxisAlignment: pw.CrossAxisAlignment.start,
                        children: [
                          pw.Padding(
                            padding: const pw.EdgeInsets.only(top: 10.0),
                            child: pw.Container(
                              height: 5.0,
                              width: 5.0,
                              decoration: const pw.BoxDecoration(
                                color: PdfColor.fromInt(0xFF000000),
                                borderRadius: pw.BorderRadius.all(
                                    pw.Radius.circular(2.0)),
                              ),
                            ),
                          ),
                          pw.SizedBox(
                            width: 10.0,
                          ),
                          pw.Expanded(
                              child: pw.Text(e!,
                                  style: pw.TextStyle(font: ttf, fontSize: 16),
                                  textDirection: pw.TextDirection.rtl)),  //Exception on this line
                        ],
                      );
                    }).toList()),
                    pw.SizedBox(height: 25.0),
                  ],
                );
              }),
        ];
      }));
  return pdf.save();
} 
ami-b-simform commented 3 months ago

I’m facing the same issue where my data is dynamically fetched from an API and used to populate a PDF. The text content sometimes lacks line breaks, which is causing display problems in the pdf as well as table cells. I’m stuck trying to figure out how to handle this content to ensure it displays correctly. Any help or suggestions would be appreciated!

johnpsc commented 3 months ago

in my case, i resolved using map instead of using ListView

SizedBox( width: width, child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ if (data.isNotEmpty) ...[ ...data.map((e) { return YOURWIDGET(), }), ],

        ],
      ),
    ),