【问题标题】:create icons in flutter pdf?在flutter pdf中创建图标?
【发布时间】:2021-06-10 17:22:43
【问题描述】:

我需要在 Flutter pdf 中添加图标。与在颤振中添加图标相比,这完全不同。我正在使用https://pub.dev/packages/pdf这个包。

代码如下:

pw.Icon(pw.IconData(0xe047));

错误是:

ArgumentError (Invalid argument (string): Contains invalid characters.: "")

【问题讨论】:

    标签: flutter dart pdf icons


    【解决方案1】:

    检查页面主题下是否添加了 PdfGoogleFonts.materialIcons():

    theme: pw.ThemeData.withFont(
      base: await PdfGoogleFonts.openSansRegular(),
      bold: await PdfGoogleFonts.openSansBold(),
      icons: await PdfGoogleFonts.materialIcons(), // this line
    )
    

    【讨论】:

      【解决方案2】:

      使用www.fluttericon.com设置您的自定义图标

      像这样使用 pw.ThemeData.withFont

      var pathToFile = await rootBundle.load('- your icon font file (.ttf) -');
      final ttf = pw.Font.ttf(pathToFile);
      
      // load ttf to pdf theme
      final theme = pw.ThemeData.withFont(
                        base: await PdfGoogleFonts.robotoCondensedRegular(),
                        bold: await PdfGoogleFonts.robotoCondensedBold(),
                        icons: ttf,
                     );
      
      final pw.Icon(pw.IconData(customIcon.icon.codePoint), size: 10)
      

      渲染pdf文件后的输出是,

      【讨论】:

        【解决方案3】:

        你必须添加打印模块

        https://pub.dev/packages/printing

        dependencies:
          printing: ^5.6.0
        

        在你的 dart 文件中导入包

        import 'package:printing/printing.dart'; 
        

        并设置您的主题:

        final pdf = pw.Document();
        
        pdf.addPage(
          pw.Page(
            theme: pw.ThemeData.withFont(
              base: await PdfGoogleFonts.varelaRoundRegular(),
              bold: await PdfGoogleFonts.varelaRoundRegular(),
              icons: await PdfGoogleFonts.materialIcons(),
            ),
            pageFormat: PdfPageFormat.a4,
            build: (pw.Context context) {
              return pw.Center(
                child: pw.Text("Hello World"),
              );
            },
          ),
        );
        

        这个答案的来源在这里

        https://github.com/DavBfr/dart_pdf/blob/master/demo/lib/examples/resume.dart

        【讨论】:

          【解决方案4】:

          要在 Pdf 包中使用 Material Icons,您只需按照此处的代码 sn-ps 操作即可。

          ma​​terial导入为mt,将pdf导入为dynamic

          import 'package:flutter/material.dart' as mt;
          import 'package:pdf/pdf.dart';
          import 'package:pdf/widgets.dart';
          

          现在试试下面的代码,它会完美运行。

          Icon(IconData(mt.Icons.check.codePoint),
                            color:
                            value PdfColors.grey,
                            size: 18,
                          )
          

          【讨论】:

            猜你喜欢
            • 2020-07-11
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-09-23
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多