【问题标题】:How to render QRImage as .png to be used in a pdf document如何将 QRImage 渲染为 .png 以在 pdf 文档中使用
【发布时间】:2020-08-18 15:26:30
【问题描述】:

我有以下 QR 图像,我想将其转换为 .png 。几年前在stackoverflow上有一个类似的帖子,但是它使用一个按钮来单击并获取我在我的应用程序中无法拥有的图像。这是我绘制二维码的代码。任何转换为​​ .png 以便我可以添加到我的 pdf 的帮助表示赞赏。

Widget getQRImage() {
if (this.showQR) {
  return QrImage(
    data: jsonEncode({
      "name": _name,
      "email": _email,
      "image": imageUrl,
     
    }),
    version: QrVersions.auto,
    size: 200,
    gapless: false,
    embeddedImage: new AssetImage('assets/logo.png'),
    embeddedImageStyle: QrEmbeddedImageStyle(
      size: Size(50, 50),
    ),
    backgroundColor: StateContainer.of(context).curTheme.primary,
    errorStateBuilder: (cxt, err) {
      return Container(
        child: Center(
          child: Text(
            "Uh oh! Something went wrong...",
            textAlign: TextAlign.center,
          ),
        ),
      );
    },
  );
} else {
  return Container();
}

}

编辑:

这是我的小部件根据答案的样子,但我无法再加载我的 pdf。是不是因为我的小部件没有返回任何内容而出错?

 pw.Widget _showQR(pw.Context context) {
    pw.Center(
      child: pw.Paragraph(text: "Your QR", style: pw.TextStyle(fontSize: 18)));

    pw.Center(child: pw.BarcodeWidget(
    data: jsonEncode({
    "name": "_name",
    "email": "_email",
   // "image": "imageUrl",

    }),
    width: 150,
    height: 150,
    barcode: pw.Barcode.qrCode(),
    ),);
    pw.Padding(padding: const pw.EdgeInsets.all(10));
  }

【问题讨论】:

  • 你想直接在pdf中使用png还是先显示qr然后在pdf上生成?
  • 是的,我想先显示它,然后在pdf上生成它

标签: flutter dart png qr-code


【解决方案1】:

要在 pdf 上绘制二维码,您可以简单地使用 pw.BarcodeWidget。这是一个示例代码:

Future<Uint8List> generateDocument() async {
  final pw.Document doc = pw.Document();

  doc.addPage(pw.MultiPage(
      pageFormat: PdfPageFormat.a4,
      crossAxisAlignment: pw.CrossAxisAlignment.start,
      header: (pw.Context context) {
        if (context.pageNumber == 1) {
          return null;
        }
        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.BoxBorder(
                    bottom: true, width: 0.5, color: PdfColors.grey)),
            child: pw.Text('Portable Document Format',
                style: pw.Theme.of(context)
                    .defaultTextStyle
                    .copyWith(color: PdfColors.grey)));
      },
      build: (pw.Context context) => <pw.Widget>[
        pw.Center(child: pw.Paragraph(text: "Your QR", style: pw.TextStyle(fontSize: 18)),),
        pw.Center(child: pw.BarcodeWidget(
          data: jsonEncode({
            "name": "_name",
            "email": "_email",
            "image": "imageUrl",

          }),
          width: 150,
          height: 150,
          barcode: pw.Barcode.qrCode(),
        ),),
        pw.Padding(padding: const pw.EdgeInsets.all(10)),
      ]));

  return doc.save();
}



Future<File> getPdf() async {
    Uint8List uint8list = await generateDocument();
    Directory output = await getTemporaryDirectory();
    File file = File(output.path + "/example.pdf");
    await file.writeAsBytes(uint8list);
    return file;
  }

@override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: FutureBuilder<File>(
        future: getPdf(),
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            return Center(
              child: PDFView(
                filePath: snapshot.data.path,
                autoSpacing: false,
                pageFling: false,
              ),
            );
          } else {
            return Center(child: CircularProgressIndicator());
          }
        },
      ),
    );
  }

输出:

为了构建 pdf,我使用了 pdf 包。显示使用flutter_pdfview的pdf并获取使用path_provider包的临时目录。

别忘了导入'package:pdf/widgets.dart' as pw;

【讨论】:

  • 谢谢,尽管出于某种原因,我在执行此操作后无法加载我的 pdf,并且我正在根据同一文件中实现的其他小部件更新我实现它的方式。请告诉我我错过了什么
  • 如果有任何错误,请分享。否则终止应用程序并再次运行它。有时热重载不显示pdf。
  • 我尝试终止并再次运行它。它没有显示错误,但没有出现pdf。是否有可能是因为小部件没有返回任何内容(在我添加的编辑中以显示我对您的代码的改编)?
  • 实际上在你的 _showQR 方法中你没有返回任何东西。同样,_showQR 方法的返回类型是 pw.Widget,因此您不能将该小部件与普通小部件一起使用。
  • 这就是为什么您需要将 pdf 作为文件,以便我们可以使用其他小部件显示 pdf。
猜你喜欢
  • 2018-02-20
  • 1970-01-01
  • 2014-09-29
  • 1970-01-01
  • 1970-01-01
  • 2021-12-08
  • 2021-01-04
  • 2011-09-21
  • 1970-01-01
相关资源
最近更新 更多