【问题标题】:flutter png and then create PDF file to printer扑动 png 然后创建 PDF 文件到打印机
【发布时间】:2023-03-27 09:49:01
【问题描述】:

我正在从事新的颤振项目,该项目使用条形码和一些文本(如this one)将小部件捕获为 PNG 图像,并通过本机代码将数据发送到打印机。但是我有很多问题。例如使用 ios 设备打印速度太慢(android 打印速度还可以)。所以我决定从 ios 的 PDF 打印,我有几个问题:

  1. 我必须使用哪个包来打印 PDF。
  2. 你也可以给我提供转换为 PDF 的正确代码

谢谢

【问题讨论】:

  • 欢迎来到stackoverflow!你能分享一些你试过的代码吗?还是你已经使用的包?

标签: pdf flutter printing png barcode


【解决方案1】:

你可以看看这个包:

https://pub.dev/packages/printing

包中的示例:

final doc = pw.Document();

doc.addPage(pw.Page(
  pageFormat: PdfPageFormat.a4,
  build: (pw.Context context) {
    return pw.Center(
      child: pw.Text('Hello World'),
    ); // Center
  })); 

然后

await Printing.layoutPdf(
  onLayout: (PdfPageFormat format) async => doc.save());

希望它对你有用。

【讨论】:

    【解决方案2】:

    稍后谢谢我

     Future printPng() async {
        try {
          RenderRepaintBoundary boundary = _globalKey.currentContext!
              .findRenderObject() as RenderRepaintBoundary;
    
          // if it needs repaint, we paint it.
          if (boundary.debugNeedsPaint) {
            Timer(const Duration(seconds: 1), () => printPng());
            return null;
          }
    
          ui.Image image = await boundary.toImage(pixelRatio: 3.0);
          ByteData? byteData =
              await image.toByteData(format: ui.ImageByteFormat.png);
          var pngBytes = byteData!.buffer.asUint8List();
    
          final imageToPrint = await flutterImageProvider(MemoryImage(pngBytes));
          final doc = pw.Document();
          doc.addPage(pw.Page(build: (pw.Context context) {
            return pw.Center(child: pw.Image(imageToPrint)); // Center
          }));
          await Printing.layoutPdf(
              onLayout: (PdfPageFormat format) async => doc.save());
          return pngBytes;
        } catch (e) {
          print(e);
          return null;
        }
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-01
      • 2018-08-15
      • 1970-01-01
      • 2012-04-30
      • 2013-07-01
      • 2015-10-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多