flutter - Flutter -PDF -- 错误 - 这个小部件创建了 20 多个页面。这可能是小部件或文档中的问题
问题描述
我正在创建一个包含大量内容的 PDF 文档。但是,我无法显示 PDF 并且出现错误
“这个小部件创建了 20 多个页面。这可能是小部件或文档中的问题。”
我知道文字太大,但我不知道如何使它工作
pdf.addPage(
pw.MultiPage(
pageFormat: PdfPageFormat.a4,
orientation: pw.PageOrientation.portrait,
crossAxisAlignment: pw.CrossAxisAlignment.start,
build: (pw.Context context) {
return <pw.Widget>[
pw.Wrap(
children: <pw.Widget>[
pw.Container(
width: PdfPageFormat.a4.width,
child: pw.Row(
mainAxisSize: pw.MainAxisSize.min,
crossAxisAlignment: pw.CrossAxisAlignment.start,
children: <pw.Widget>[
pw.Expanded(
child: pw.Column(
mainAxisSize: pw.MainAxisSize.min,
crossAxisAlignment: pw.CrossAxisAlignment.start,
children: <pw.Widget>[
pw.SizedBox(height: 8.0),
for (int i = 0; i < data['employers'].length; i++)
pw.Column(
mainAxisSize: pw.MainAxisSize.min,
crossAxisAlignment: pw.CrossAxisAlignment.start,
children: <pw.Widget>[
pw.Text(
"${data['employers'][i]['duties']}",
style: pw.TextStyle(
fontSize: 12.0,
),
softWrap: true,
),
],
),
],
),
),
],
),
),
],
),
];
},
),
);
我需要一些关于如何重组代码的帮助,以便我可以显示 PDF 并将其换行到下一页文本只是一组 Lorem Ipsum 。
谢谢你的帮助
解决方案
你可以简单地使用它。
import 'package:ferns_restaurant/constants/Constants.dart';
import 'package:pdf/widgets.dart' as pw;
import 'package:pdf/widgets.dart';
import 'package:printing/printing.dart';
import 'package:woocommerce/models/order.dart';
class CustomPrinter {
final pdf = pw.Document();
print(WooOrder order) {
List<LineItems> _product = order.lineItems;
Printing.layoutPdf(
onLayout: (format) {
final doc = pw.Document();
doc.addPage(
pw.MultiPage(
crossAxisAlignment: pw.CrossAxisAlignment.start,
build: (pw.Context context) => <pw.Widget>[
_topHeaderLayout(),
for (int i = 0; i < _product.length; i++)
_productLayout(_product[i]),
]),
);
return doc.save();
},
name: 'order_id_#' + order.id.toString(),
);
}
_topHeaderLayout() {
return new pw.Container(
// height: 60.0,
margin: pw.EdgeInsets.only(bottom: 20.0),
child: pw.Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
pw.Expanded(
child: pw.Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <pw.Widget>[
pw.Container(
child: pw.Text(
'ITEM',
style: pw.TextStyle(
fontWeight: pw.FontWeight.bold,
fontSize: Constants.PRINTER_BASE_FONT_SIZE),
),
),
],
),
),
pw.Expanded(
child: pw.Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: <pw.Widget>[
pw.Text(
'PRICE',
style: pw.TextStyle(
fontWeight: pw.FontWeight.bold,
fontSize: Constants.PRINTER_BASE_FONT_SIZE),
),
],
),
)
],
));
}
_productLayout(LineItems item) {
return new pw.Container(
// height: 60.0,
margin: pw.EdgeInsets.only(bottom: 20.0),
child: pw.Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
pw.Expanded(
child: pw.Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <pw.Widget>[
pw.Container(
child: pw.Text(
item.name.toString() + ' - ID: ' + item.id.toString(),
style: pw.TextStyle(
fontWeight: pw.FontWeight.bold,
fontSize: Constants.PRINTER_BASE_FONT_SIZE),
),
),
pw.Container(
child: Text(
'Qty: ' +
item.quantity.toString() +
' x Cost: ' +
'£' +
item.price,
style: pw.TextStyle(
fontWeight: pw.FontWeight.normal,
fontSize: Constants.PRINTER_BASE_FONT_SIZE)),
),
],
),
),
pw.Expanded(
child: pw.Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: <pw.Widget>[
pw.Text(
'£' + item.total.toString(),
style: pw.TextStyle(
fontWeight: pw.FontWeight.normal,
fontSize: Constants.PRINTER_BASE_FONT_SIZE),
),
],
),
)
],
));
}
}
推荐阅读
- android - 使用约束布局、滚动视图和线性布局的 Android 布局
- xml - 如何验证 XML
- mysql - 在 AWS 中存储与数据库记录相关的大量 HTML 内容的位置
- laravel - Laravel 角色和权限包
- python - sqlite3 python留下一个空白列
- java - Java android ROOM获取带有详细信息的对象
- sql - SQL 表连接并根据查询生成新列
- javascript - 需要 Javascript 正则表达式来防止用户只输入字符并且只允许一个撇号
- php - 使用 ElasticSearch 无痛脚本检查参数数组中是否存在值
- python - BeautifulSoup 不读取 html