flutter pdf长表

问题描述 投票:0回答:2

我是颤振新手。我正在尝试设计一个有长桌子的pdf文件。但表不会自动拆分。如果表格长度超过 a4 尺寸,则不会生成 pdf 文件。有没有一个简单的解决方案可以解决我所缺少的问题?

如果计数器超过 25,则不会生成 pdf。 是的,我知道对于一个表,很容易使用 if/else 函数并生成两个表,但在我的主项目中,我有多个具有不同列号的表。所以我需要一个使表格自动拆分的解决方案。


import 'package:flutter/services.dart';
import 'package:path_provider/path_provider.dart';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart';
import 'package:printing/printing.dart';
import 'dart:math' as math;
Future<void> testPdf() async {
  final Document pdf = Document();
  var myFont = Font.ttf(await rootBundle.load("assets/fonts/HacenTunisia.ttf"));
  List<TableRow> buildTable(
      { Context context, int count = 10, bool repeatHeader = false}) {
    final rows = <TableRow>[];
    {
      final tableRow = <Widget>[];
      for (final cell in <String>['Hue', 'Color', 'RGBA']) {
        tableRow.add(Container(
            alignment: Alignment.center,
            margin: const EdgeInsets.all(5),
            child: Text(cell, style: Theme.of(context).tableHeader)));
      }
      rows.add(TableRow(children: tableRow, repeat: repeatHeader));
    }
    for (var y = 0; y < count; y++) {
      final h = math.sin(y / count) * 365;
      final PdfColor color = PdfColorHsv(h, 1.0, 1.0);
      final tableRow = <Widget>[
        Container(
            margin: const EdgeInsets.all(5),
            child: Text('${h.toInt()}°', style: Theme.of(context).tableCell)),
        Container(
            margin: const EdgeInsets.all(5),
            decoration: BoxDecoration(
              color: color,

            ),
            height: Theme.of(context).tableCell.fontSize),
        Container(
            margin: const EdgeInsets.all(5),
            child: Text(color.toHex(), style: Theme.of(context).tableCell)),
      ];
      rows.add(TableRow(children: tableRow));
    }
    return rows;
  }
  pdf.addPage(MultiPage(
      theme: ThemeData.withFont(
        base: myFont,
      ),
      pageFormat: PdfPageFormat.a4,
      build: (Context context) {
        return [
          Column(children: [

            Container(
              // margin: EdgeInsets.fromLTRB(22, 5, 22, 5),
              child: Directionality(
                textDirection: TextDirection.rtl,
                child: Table(
                  children: buildTable(context: context, count: 30),
                  border: TableBorder.all(),
                  tableWidth: TableWidth.max,
                ),
              ),
            ),
            SizedBox(height: 20),
          ])
        ];
      }));
  final String dir = (await getApplicationDocumentsDirectory()).path;
  //final String VoucherNo =request['VoucherNo'];
  final String path = '$dir/1.pdf';
  final File file = File(path);
  await file.writeAsBytes(pdf.save());
  await Printing.sharePdf(bytes: pdf.save(), filename: 'report.pdf');
}
dart pdf flutter-layout
2个回答
0
投票

您使用任何套餐吗? 您可以尝试SPDF包


0
投票

您可以尝试使用 Table.textArray ,当当前页面已满时,它将拆分表格

© www.soinside.com 2019 - 2024. All rights reserved.