create icons in flutter pdf?

Viewed 1511

I need a add icons in flutter pdf. This was completely different when compared with add icons in flutter. I am using https://pub.dev/packages/pdf this package.

Here is the code :

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

Error was :

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

check whether PdfGoogleFonts.materialIcons() is added under page theme:

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

Set your custom icons using www.fluttericon.com

using pw.ThemeData.withFont like this

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)

output after rendering pdf file is,

enter image description here

To use Material Icons in Pdf package you just simply follow the code snippets down here.

Import material as mt and pdf as dynamic

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

Now Try the following code it will work perfectly.

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

You have to add the printing module

https://pub.dev/packages/printing

dependencies:
  printing: ^5.6.0

import the package in your dart file

import 'package:printing/printing.dart'; 

And set your theme with:

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"),
      );
    },
  ),
);

The source of this answer is here

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

Related