Phanindra
Phanindra

Reputation: 1378

create icons in flutter pdf?

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.: "")

Upvotes: 4

Views: 5700

Answers (4)

Karthikeyan M
Karthikeyan M

Reputation: 221

Set your custom icons using www.fluttericon.com and download your icon fonts.

or

you can use directly from PdfGoogleFonts materialIcons instead using ttf

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

Upvotes: 3

Mehran Ullah
Mehran Ullah

Reputation: 792

To use Material Icons in Pdf package you just 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 take the Icon from the Material library and pass its codepoints to IconData Constructor in PDF Library now you can easily use the IconData instance in PDF Icon() class.

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

Upvotes: 4

Javier Garcia
Javier Garcia

Reputation: 328

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

Upvotes: 1

kiwiiiii
kiwiiiii

Reputation: 51

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
)

Upvotes: 3

Related Questions