flutter pdf 插件使用

我当前flutter版本是1.22.6版本 使用的pub.dev的 pdf 插件和 print插件分别如下,请在pubspec.yaml导如下插件

printing: 4.1.0
pdf: 2.1.0

导入以后为实现打印预览pdf和打印功能,我根据Flutter PDF Demo 里右下角查看了示例代码,发现需要主函数才能运行,于是我更改如下示例代码

import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as pw;
import 'package:printing/printing.dart';
import 'package:warehouse_management/ui/GradientAppBar.dart';

void main() => runApp(const MyApp('打印详情'));

class MyApp extends StatelessWidget {
  const MyApp(this.title);

  final String title;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text(title),
          centerTitle: true,
        ),
        body: PdfPreview(
          build: (format) => _generatePdf(format, title),
        ),
      ),
    );
  }

  Future<Uint8List> _generatePdf(PdfPageFormat format, String title) async {
    final pdf = pw.Document(
        pageMode: PdfPageMode.fullscreen,
        theme: pw.ThemeData.withFont(
            base: pw.Font.ttf(
                await rootBundle.load("images/fonts/alibabapuhui.ttf"))));

    pdf.addPage(pw.MultiPage(
        pageFormat:
            PdfPageFormat.letter.copyWith(marginBottom: 1.5 * PdfPageFormat.cm),
        crossAxisAlignment: pw.CrossAxisAlignment.start,
        header: (pw.Context context) {
          if (context.pageNumber == 1) {
            return pw.SizedBox();
          }
          return pw.Container(
              alignment: pw.Alignment.centerRight,
              margin: const pw.EdgeInsets.only(bottom: 3.0 * PdfPageFormat.mm),
              padding: const pw.EdgeInsets.only(bottom: 3.0 * PdfPageFormat.mm),
              decoration: const pw.BoxDecoration(
                  border: pw.Border(
                      bottom:
                          pw.BorderSide(width: 0.5, color: PdfColors.grey))),
              child: pw.Text('测试',
                  style: pw.Theme.of(context)
                      .defaultTextStyle
                      .copyWith(color: PdfColors.grey)));
        },
        footer: (pw.Context context) {
          return pw.Container(
              alignment: pw.Alignment.centerRight,
              margin: const pw.EdgeInsets.only(top: 1.0 * PdfPageFormat.cm),
              child: pw.Text(
                  'Page ${context.pageNumber} of ${context.pagesCount}',
                  style: pw.Theme.of(context)
                      .defaultTextStyle
                      .copyWith(color: PdfColors.grey)));
        },
        build: (pw.Context context) => <pw.Widget>[
              pw.Header(
                  level: 0,
                  title: '测试',
                  child: pw.Row(
                      mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
                      children: <pw.Widget>[
                        pw.Text('绣花裁片物料单', textScaleFactor: 2),
                        pw.PdfLogo()
                      ])),
              pw.Text('传票号', softWrap: true),
              pw.Container(
                alignment: pw.Alignment.centerRight,
                child: pw.Column(children: [
                  pw.BarcodeWidget(
                    data: '我是张坤',
                    width: 60,
                    height: 60,
                    barcode: pw.Barcode.qrCode(),
                  ),
                  pw.SizedBox(height: 20),
                  pw.Text("扫码查看记录快照")
                ]),
              ),
              pw.SizedBox(height: 10),
              pw.Text('二维码名称:绣花裁片物料单       填表人:张三     填表时间:2021-03-10 14:33',
                  softWrap: true),
              pw.SizedBox(height: 10),
              pw.Table.fromTextArray(
                  headerCount: 0,
                  context: context,
                  data: const <List<String>>[
                    <String>[
                      '厂家',
                      '泳装分厂',
                    ],
                    <String>[
                      '车间',
                      '泳装分厂',
                    ],
                    <String>[
                      '合同号',
                      '025k',
                    ],
                    <String>[
                      '货号',
                      '0254689-25',
                    ],
                    <String>[
                      '部位',
                      '后片',
                    ],
                    <String>[
                      '颜色',
                      '001',
                    ],
                    <String>[
                      '规格',
                      'xl',
                    ],
                    <String>[
                      '数量',
                      '280',
                    ],
                    <String>[
                      '日期',
                      '2021-03-10',
                    ],
                    <String>[
                      '时间',
                      '14:33',
                    ],
                  ]),
              pw.Padding(padding: const pw.EdgeInsets.all(10)),
            ]));

    return pdf.save();
  }
}

可以预览以后发现不能使用中文预览,需要配合导入文字ttf,此代码已经在上方了,我下的文字ttf是阿里的普惠字体应用授权 ,阿里需要注册以后才能使用下载,你也可以使用其他ttf免费字体,字体导入如下文件夹,比如我的

重点:需要在 pubspec里配置font

  fonts:
    - family: AlibabaFont
      fonts:
        - asset: res/fonts/alibabapuhui.ttf

然后就可以run 起来了,以下是运行后的图片

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值