图片不显示在Flutter中的pdf创建

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

我有一个问题,当生成pdf时,资产中的图像不能在屏幕上呈现。

我安装了pdf和打印包。打印包用于从我的图片文件夹中加载图片。

以下是我的代码

final pdf = pw.Document(deflate: zlib.encode);

writeOnPdf() async {

const imageProvider = const AssetImage('images/sig1.png');

final PdfImage sig1 = await pdfImageFromImageProvider(pdf: pdf.document, image: imageProvider);

pdf.addPage(pw.Page(
  pageFormat: PdfPageFormat.a4,
  build: (pw.Context context) {
    return pw.Column(
      children: [
        pw.Row(
          mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
          children: [
            pw.Text("Dexandra Inventory", style: pw.TextStyle(fontSize: 28.0)),
            pw.Text("RECEIPT#1", style: pw.TextStyle(fontSize: 28.0))
          ]
        ),
        pw.SizedBox(
          height: 30.0
        ),
        pw.Row(
          mainAxisAlignment: pw.MainAxisAlignment.start,
          children: [
            pw.Text("Luqman +60186661360", style: pw.TextStyle(fontSize: 18.0)),
          ]
        ),
        pw.SizedBox(
            height: 30.0
        ),
        pw.Header(
          level: 0,
          child: pw.Text("1 Item (Qty:2)", style: pw.TextStyle(fontSize: 22.0))
        ),
        pw.SizedBox(
            height: 30.0
        ),
        pw.Row(
          mainAxisAlignment: pw.MainAxisAlignment.spaceEvenly,
          children: [
            pw.Text("2x", style: pw.TextStyle(fontSize: 16.0)),
            pw.Text("Air Feshener", style: pw.TextStyle(fontSize: 16.0)),
            pw.Text("RM 10.00", style: pw.TextStyle(fontSize: 16.0, color: PdfColors.grey)),
            pw.SizedBox(width: 40.0),
            pw.Text("RM 20.00", style: pw.TextStyle(fontSize: 16.0, fontBold: pw.Font.courierBold())),
          ]
        ),
        pw.SizedBox(
            height: 40.0
        ),
        pw.Row(
            mainAxisAlignment: pw.MainAxisAlignment.end,
            children: [
              pw.Column(
                children: [
                  pw.Text("Total: RM 20.00", style: pw.TextStyle(fontSize: 20.0, fontBold: pw.Font.courierBold())),
                  pw.SizedBox(
                      height: 15.0
                  ),
                  pw.Text("Cash: RM 20.00", style: pw.TextStyle(fontSize: 16.0)),
                ]
              ),
            ]
        ),
        pw.Header(
            level: 0,
            child: pw.Text("")
        ),
        pw.Row(
          mainAxisAlignment: pw.MainAxisAlignment.center,
          children: [
            pw.Text("March 30, 2020 4:41 PM", style: pw.TextStyle(fontSize: 16.0, color: PdfColors.grey)),
          ]
        ),
        pw.Row(
          mainAxisAlignment: pw.MainAxisAlignment.spaceAround,
          children: [
            pw.Image(sig1),
          ]
        )
      ]
    ); // Center
}));
}

一切都很好,直到最后一个小部件,也就是图像。它不显示在PDF页面中。

pubspec.yml

assets:
- images/

这就是我点击按钮生成pdf时在终端出现的情况,不知道是否与图片没有渲染有关。

I/flutter (13171): Helvetica has no Unicode support see https://github.com/DavBfr/dart_pdf/wiki/Fonts-Management
E/AccessibilityBridge(13171): VirtualView node must not be the root node.
E/AccessibilityBridge(13171): VirtualView node must not be the root node.
I/chatty  (13171): uid=10341(com.example.dexandrainventory) identical 2 lines
E/AccessibilityBridge(13171): VirtualView node must not be the root node.
E/AccessibilityBridge(13171): VirtualView node must not be the root node.
I/chatty  (13171): uid=10341(com.example.dexandrainventory) identical 5 lines
flutter pdf-generation flutter-layout flutter-image
1个回答
0
投票

在函数中声明变量'pdf'。

像这样。

writeOnPdf() async { final pdf = pw.Document(deflate: zlib.encode);

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