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

How to split large data table into multiple pages #1799

Open
1 task
DktPhl2019 opened this issue Dec 24, 2024 · 4 comments
Open
1 task

How to split large data table into multiple pages #1799

DktPhl2019 opened this issue Dec 24, 2024 · 4 comments
Labels
bug Something isn't working needs triage

Comments

@DktPhl2019
Copy link

Hello

I am using flutter pdf on Windows 11.

Describe the bug
Table data with more than 10 records shows blank pdf.
I need to split large table data into multiple pages.

To Reproduce - Please see code below:

mappedList = (list1.isNotEmpty && list2.isNotEmpty && list2.isNotEmpty && list4.isNotEmpty) ? [
        for (int i = 0; i < list1.length; i++)
          myData(list1[i],list2[i],list3[i],list4[i])
      ] : [];
	  
	final pdf = pw.Document();
      pdf.addPage(pw.Page(pageFormat: PdfPageFormat.standard,build: (pw.Context context) {
            return pw.Column(children: [
                pw.Table(border: pw.TableBorder.all(),children: [
                    pw.TableRow(children: [
                        pw.Padding(padding: const pw.EdgeInsets.all(8),child: pw.Text('Name',style: pw.TextStyle(fontSize: 9, fontWeight: pw.FontWeight.bold))),
                        pw.Padding(padding: const pw.EdgeInsets.all(8),child: pw.Text('Salary',style: pw.TextStyle(fontSize: 9, fontWeight: pw.FontWeight.bold))),
                        pw.Padding(padding: const pw.EdgeInsets.all(8),child: pw.Text('Department',style: pw.TextStyle(fontSize: 9, fontWeight: pw.FontWeight.bold))),
                        pw.Padding(padding: const pw.EdgeInsets.all(8),child: pw.Text('Location',style: pw.TextStyle(fontSize: 9, fontWeight: pw.FontWeight.bold))),
                      ],
                    ),
                  ...mappedList.map((item) => pw.TableRow(children: [
                    pw.Padding(padding: const pw.EdgeInsets.all(8),child: pw.Text(item.Name)),
                    pw.Padding(padding: const pw.EdgeInsets.all(8),child: pw.Text(item.Salary)),
                    pw.Padding(padding: const pw.EdgeInsets.all(8),child: pw.Text(item.Department)),
                    pw.Padding(padding: const pw.EdgeInsets.all(8),child: pw.Text(item.Location))),
                  ])),
                  ],
                ),
                pw.Spacer(), // Dynamic spacing to move content up
                pw.Align(alignment: pw.Alignment.bottomCenter,child: pw.Text('Page 1',style: pw.TextStyle(fontSize: 9, color: PdfColors.grey))),
              ],
            );
          },
        ),
      );
      final file = File('${myPath}/my_file.pdf');
      file.writeAsBytes(await pdf.save());//  
	  

Expected behavior
I should see table split into multiple pages.

Screenshots
None

Flutter Doctor
N/A

Desktop (please complete the following information):

  • Windows
@DktPhl2019 DktPhl2019 added bug Something isn't working needs triage labels Dec 24, 2024
@dharmisthadev
Copy link

I am also getting same issue..You find any solution of this issue..?

@MichelMelhem
Copy link

I'm also the facing the same issue , i would love to know if you found a solution ?

@albusaidyy
Copy link

albusaidyy commented Jan 10, 2025

@DktPhl2019
Try and use pw.MultiPage instead of pw.Page

@leocirto
Copy link

Table can span across multiple pages.
Just remove the Column and change Page to MultiPage.

For example:

  Future<Uint8List> _builPdfPreview(final PdfPageFormat format) async {
     final pdf = pw.Document();
      pdf.addPage(
        pw.MultiPage(  // pw.Page(
            pageFormat: format,
            build: (final pw.Context context) {
            return [ // pw.Column( children: [
                pw.Table( border: pw.TableBorder.all(),
                 children: [
                  pw.TableRow(children: [
                        pw.Padding(padding: const pw.EdgeInsets.all(8),child: pw.Text('Name',style: pw.TextStyle(fontSize: 9, fontWeight: pw.FontWeight.bold))),
                        pw.Padding(padding: const pw.EdgeInsets.all(8),child: pw.Text('Salary',style: pw.TextStyle(fontSize: 9, fontWeight: pw.FontWeight.bold))),
                        pw.Padding(padding: const pw.EdgeInsets.all(8),child: pw.Text('Department',style: pw.TextStyle(fontSize: 9, fontWeight: pw.FontWeight.bold))),
                        pw.Padding(padding: const pw.EdgeInsets.all(8),child: pw.Text('Location',style: pw.TextStyle(fontSize: 9, fontWeight: pw.FontWeight.bold))),
                  ] ),

                 for( int k = 0; k < 20; k++ )
                  pw.TableRow( children: [
                    pw.Padding(padding: const pw.EdgeInsets.all(8),child: pw.Text('row $k')),
                    pw.Padding(padding: const pw.EdgeInsets.all(8),child: pw.Text('row $k')),
                    pw.Padding(padding: const pw.EdgeInsets.all(8),child: pw.Text('row $k')),
                    pw.Padding(padding: const pw.EdgeInsets.all(8),child: pw.Text('row $k')) 
                  ] ),
                 ] ),
                pw.Spacer(), // Dynamic spacing to move content up
                pw.Align(alignment: pw.Alignment.bottomCenter,child: pw.Text('Page 1',style: const pw.TextStyle(fontSize: 9, color: PdfColors.grey))),
              // ] )
            ];
          },
        ),
      );
     return pdf.save();
  }

Output:

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs triage
Projects
None yet
Development

No branches or pull requests

5 participants