如何在pdf flutter中为动态数据添加多页

6l7fqoea  于 2022-12-05  发布在  Flutter
关注(0)|答案(1)|浏览(133)

我正在尝试以pdf格式打印配置文件数据。为此使用此pdf包。生成pdf的代码为

doc.addPage(
pw.MultiPage(
  // pageTheme: pageTheme,
  pageFormat: PdfPageFormat.a4,
  build: (pw.Context context) => [
    pw.Partitions(
      children: [
        pw.Partition(
          width: screenWidth * 1.3,
          child: pw.Container(
            height: pageTheme.pageFormat.availableHeight,
            child: pw.Padding(
                 child:pw.Column(children:[//contains multiple items that fits in more than one page]),
                   ),
                ),
             ),

       pw.Partition(
            width: screenWidth * 1.3,
            child: pw.Container(
              height: pageTheme.pageFormat.availableHeight,
                     child: pw.Padding(
                   child:pw.Column(children:[//contains multiple items that fits in more than one page]),
                    ),
                 ),
                ),
],),),

**问题:**在上面的脚本中,只生成了一个a4大小的页面,其余内容被忽略。谢谢

yqhsw0fo

yqhsw0fo1#

你需要先用你的数据创建一个pdf小部件列表。2然后把这个列表传递给MultiPage Builder,你就可以开始了。3下面是示例代码:

import 'package:flutter/material.dart';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as p;
import 'package:printing/printing.dart';
class MyProfile extends StatelessWidget {
const MyProfile({super.key});

@override
Widget build(BuildContext context) {
return Scaffold(
  appBar: AppBar(
    title: const Text('My Profile'),
  ),
  body: Center(
    child: ElevatedButton(
      onPressed: () async {
        //List of pdf widgets
        List<p.Widget> widgets = [];

        //Profile image
        final image = p.ClipOval(
          child: p.Image(
            await imageFromAssetBundle('assets/Image.jpg'),
            fit: p.BoxFit.cover,
            width: 300,
            height: 300,
          ),
        );

        //container for profile image decoration
        final container = p.Center(
          child: p.Container(
            child: image,
            padding: const p.EdgeInsets.all(5),
            decoration: p.BoxDecoration(
              shape: p.BoxShape.circle,
              border: p.Border.all(
                color: PdfColors.blue,
                width: 10,
              ),
            ),
          ),
        );
        
        //add decorated image container to widgets list
        widgets.add(container);
        widgets.add(p.SizedBox(height: 20));//some space beneath image

        //add all other data which may be in the form of list
        //use a loop to create pdf widget and add it to list
        //one by one
        for (int i = 0; i < 4; i++) {
          widgets.add(
            p.Text(
              'Heading',
              style: p.TextStyle(
                fontSize: 25,
                fontWeight: p.FontWeight.bold,
              ),
            ),
          );
          widgets.add(p.SizedBox(height: 5));
          widgets.add(
            p.Text(
              'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed sed accumsan augue, ut tincidunt lectus. Vestibulum venenatis euismod eros suscipit rhoncus. Sed vulputate congue turpis ut cursus. Proin sollicitudin nulla vel nisi vulputate sagittis. Morbi neque mauris, auctor id posuere eu, egestas porttitor justo. Donec tempus egestas lorem in convallis. Quisque fermentum, augue ut facilisis pretium, risus dolor viverra est, ac consequat tellus risus vitae sapien. ',
              style: const p.TextStyle(color: PdfColors.grey),
            ),
          );
          widgets.add(p.SizedBox(height: 10));
        }

        //pdf document
        final pdf = p.Document();
        pdf.addPage(
          p.MultiPage(
            pageFormat: PdfPageFormat.a4,
            build: (context) => widgets,//here goes the widgets list
          ),
        );
        Printing.layoutPdf(
          onLayout: (PdfPageFormat format) async => pdf.save(),
        );
      },
      child: const Text('Print'),
    ),
  ),
);
}
}

下面是生成的pdf screenshot
干杯:)

相关问题