Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Instance of toomanypagesexception #1671

Open
mohammednabil95 opened this issue May 26, 2024 · 2 comments
Open

Instance of toomanypagesexception #1671

mohammednabil95 opened this issue May 26, 2024 · 2 comments
Labels
needs triage support Asking for help writing an application

Comments

@mohammednabil95
Copy link

mohammednabil95 commented May 26, 2024

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();
} 
@mohammednabil95 mohammednabil95 added needs triage support Asking for help writing an application labels May 26, 2024
@ami-b-simform
Copy link

ami-b-simform commented Jul 12, 2024

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
Copy link

johnpsc commented Aug 6, 2024

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

        ],
      ),
    ),  

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs triage support Asking for help writing an application
Projects
None yet
Development

No branches or pull requests

3 participants