PDF创建时图像未显示在Flutter中

时间:2020-03-31 08:53:28

标签: flutter pdf-generation flutter-layout flutter-image

我遇到一个问题,当生成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

3 个答案:

答案 0 :(得分:1)

如果要显示base64图像,则需要将MemoryImage与base64Decode一起使用,以帮助您解码图像并以pdf显示。

FILE-SECTION.
*> In File:
01 someInt PIC 9. *> eg. 3

*> Out File:
01 PrintLine PIC X(75). *> for writing data to a line

WORKING-STORAGE SECTION.
01 someFloat PIC 9V99. 

PROCEDURE DIVISION. 
COMPUTE someFloat = someInt / 1
DISPLAY someFloat *> displays 3.00 (good) 
WRITE PrintLine FROM someFloat *> stored as 300 (not good)

答案 1 :(得分:1)

final profileImage = pw.MemoryImage(
(await rootBundle.load('assetsimage)).buffer.asUint8List(),);

最后,使用它

pw.Image(profileImage),

答案 2 :(得分:0)

在函数内部声明变量“ pdf”。

赞:

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