【问题标题】:How to generate QR Code on PDF file in flutter如何在 Flutter 中生成 PDF 文件的二维码
【发布时间】:2023-03-06 09:38:01
【问题描述】:

我已使用此解决方案How Can I generate QR code and Show it on a PDF page in Flutter,但仍然无法打印二维码。

我的代码:

  pdf.BarcodeWidget(
              color: PdfColor.fromHex("#000000"),
              barcode:pdf.Barcode.qrCode(), data: "My name is Bilal"
          ),

这是我的功能:

Future<Uint8List> func_generate_sale_pdf(PdfPageFormat format) {
    final doc = pdf.Document();

    doc.addPage(pdf.Page(build: (pdf.Context context) {
      return pdf.Center(
        child: pdf.Column(children: <pdf.Widget>[

          pdf.Text(config.app_name.toUpperCase(),
              style:
                  pdf.TextStyle(fontSize: 32, fontWeight: pdf.FontWeight.bold)),
          pdf.SizedBox(height: 25),
          pdf.Text(
              " ${languages.skeleton_language_objects[config.app_language]['for_enquiries_call']} : ${config.app_contacts}",
              style: pdf.TextStyle(fontSize: 22)),
          pdf.SizedBox(height: 5),
          pdf.Text("${config.app_po_box}", style: pdf.TextStyle(fontSize: 22)),
          pdf.SizedBox(height: 5),
          pdf.Text(
              "${languages.skeleton_language_objects[config.app_language]['receipt']} # ${receipt_data['receipt_no']}",
              style: pdf.TextStyle(fontSize: 20)),
          pdf.SizedBox(height: 5),
          pdf.Text("${receipt_data['date_time']}",
              style: pdf.TextStyle(fontSize: 20)),
          pdf.SizedBox(height: 30),
          pdf.Container(
            alignment: pdf.Alignment.topLeft,
            child: pdf.Text("${receipt_data['items_list'].toString()}",
                style: pdf.TextStyle(
                    fontSize: 22, fontWeight: pdf.FontWeight.normal),
                textAlign: pdf.TextAlign.left),
          ),
          pdf.SizedBox(height: 30),
          pdf.Container(
              child: pdf.Row(children: <pdf.Widget>[
            pdf.Expanded(
              flex: 1,
              child: pdf.Text(
                  languages.skeleton_language_objects[config.app_language]
                      ['total_amount'],
                  style: pdf.TextStyle(
                      fontSize: 20, fontWeight: pdf.FontWeight.bold),
                  textAlign: pdf.TextAlign.left),
            ),
            pdf.Expanded(
              flex: 1,
              child: pdf.Text("${receipt_data['total_amount']}",
                  style: pdf.TextStyle(fontSize: 20),
                  textAlign: pdf.TextAlign.right),
            ),
          ])),
          pdf.SizedBox(height: 10),
          pdf.Container(
              child: pdf.Row(children: <pdf.Widget>[
            pdf.Expanded(
              flex: 1,
              child: pdf.Text(
                  languages.skeleton_language_objects[config.app_language]
                      ['total_items'],
                  style: pdf.TextStyle(
                      fontSize: 20, fontWeight: pdf.FontWeight.bold),
                  textAlign: pdf.TextAlign.left),
            ),
            pdf.Expanded(
              flex: 1,
              child: pdf.Text("${receipt_data['total_items']}",
                  style: pdf.TextStyle(fontSize: 20),
                  textAlign: pdf.TextAlign.right),
            ),
          ])),
          pdf.SizedBox(height: 10),
          pdf.Container(
              height: 35,
              child: pdf.Row(children: <pdf.Widget>[
                pdf.Expanded(
                  flex: 1,
                  child: pdf.Text(
                      languages.skeleton_language_objects[config.app_language]
                          ['total_tax'],
                      style: pdf.TextStyle(
                          fontSize: 20, fontWeight: pdf.FontWeight.bold),
                      textAlign: pdf.TextAlign.left),
                ),
                pdf.Expanded(
                  flex: 1,
                  child: pdf.Text("${receipt_data['total_tax_amount']}",
                      style: pdf.TextStyle(fontSize: 20),
                      textAlign: pdf.TextAlign.right),
                ),
              ])),
          pdf.SizedBox(height: 35),
          pdf.Text(
              languages.skeleton_language_objects[config.app_language]
                  ['thant_you_and_come_again'],
              style: pdf.TextStyle(fontSize: 24)),


         //From here on, the compiler doesn't print anything.
          pdf.BarcodeWidget(
              color: PdfColor.fromHex("#000000"),
              barcode:pdf.Barcode.qrCode(), data: "My name is Bilal"
          ),
          pdf.Text("SkillzUPP Technologies: +923058431046",
              style: pdf.TextStyle(fontSize: 24),
              textAlign: pdf.TextAlign.center),
          pdf.SizedBox(height: 10),

        ]),
      ); // Center
    }));

    return doc.save();
  }

除 QR 码外,所有内容都可以正常打印,并且在该特定行,此错误会显示在终端:

Helvetica-Bold has no Unicode support see https://github.com/DavBfr/dart_pdf/wiki/Fonts-Management

【问题讨论】:

    标签: flutter dart pdf qr-code barcode


    【解决方案1】:

    我在显示 "has no Unicode support..." 行时遇到问题,以修复我在项目中创建了一个文件夹字体并添加了一些真实类型的字体,然后我为文档创建了一个 pdf 主题,如下所示:

    var myTheme = ThemeData.withFont(
          base: Font.ttf(await rootBundle.load("fonts/OpenSans-Regular.ttf")),
          bold: Font.ttf(await rootBundle.load("fonts/OpenSans-Bold.ttf")),
          italic: Font.ttf(await rootBundle.load("fonts/OpenSans-Italic.ttf")),
          boldItalic:
              Font.ttf(await rootBundle.load("fonts/OpenSans-BoldItalic.ttf")),
        );
    

    然后我在文档页面中使用了主题,错误消失了,如下所示:

    Page(theme: myTheme,...)
    

    我不确定是不是同样的情况,但我认为你应该试试。

    【讨论】:

    • 需要详细说明。我已经编辑了我的问题。
    • 我编辑了我的答案,我认为可以帮助,我希望。
    • 我收到此错误:The method 'withFont' isn't defined for the type 'Theme'.。并且编译器不建议导入任何库。此外字体也无法识别Undefined name 'Font'。可能是因为 Flutter 2.0.4 有一些不同的执行方式...
    • 来自这个包,同一个pdf包,只是文件不同,import 'package:pdf/widgets.dart';
    • 我终于成功了。谢谢你的帮助。在所有设置完成后,我没有得到以 pdf 格式显示的 QR 码。然后我在 BarcodeWidget 的构造函数中给了height: 70 ,width :70。它奏效了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-03
    • 1970-01-01
    相关资源
    最近更新 更多