create icons in flutter pdf? - flutter

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

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,
)

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
)

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

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,

Related

Flutter - PDF: Non english language characters are not rendered properly in PDF even after loading the necessary fonts

I am using PDF package in flutter web. Everything is good but when i try to use non-english language, its not showing the character properly. for example (Tamil + english) , Tamil is a language and when i use the character from this language its not showing the character properly.
I did research multiple issues that are related to similar problem and i did load the necessary font in application but characters are not showing correctly.
here is the code.
Future<Uint8List> createSecondPdf(
List<AttendeeListViewModel> attendeeListViewModelList) async {
var mytheme = ThemeData.withFont(
base: Font.ttf(await rootBundle.load("assets/HindMadurai-Regular.ttf")),
bold: Font.ttf(await rootBundle.load("assets/HindMadurai-Regular.ttf")),
italic: Font.ttf(await rootBundle.load("assets/HindMadurai-Regular.ttf")),
boldItalic:
Font.ttf(await rootBundle.load("assets/HindMadurai-Regular.ttf")),
);
// final pdf = Document(theme: mytheme);
final pdf = Document();
//final f1 = await PdfGoogleFonts.hindGunturRegular();
// final f2 = await rootBundle.load("assets/TAU-Marutham_Bold.ttf");
print(
' ${attendeeListViewModelList[2].attendeeDataModel?.pNameAndProfession ?? ''}');
final data = await rootBundle.load('assets/HindMadurai-Regular.ttf');
final loadedfont = Font.ttf(data);
pdf.addPage(
MultiPage(
// theme: mytheme,
build: ((context) {
return [
Table.fromTextArray(
headers: ['Sl.No', 'Name', 'Data'],
cellAlignments: {0: Alignment.center},
cellStyle: TextStyle(font: loadedfont),
columnWidths: {
0: const FlexColumnWidth(0.6),
1: const FlexColumnWidth(4.0),
2: const FlexColumnWidth(2.0),
},
data: <List<String>>[
<String>[
'Hello',
attendeeListViewModelList[2]
.attendeeDataModel
?.pNameAndProfession ??
'',
'ஜெயப்பாண்டி'
]
],
),
];
}),
),
);
return pdf.save();
}
tried multiple options but no luck so far. Any help is really appreciated. Below screen shot show the characters in different way than what i have given in the code.
The below are packages that i am using.
pdf: ^3.8.1
printing: ^5.9.1
Appreciate the response!.
Thanks.

CREATE INVOICE PDF using FLUTTER WEB

I need to create a path to temporarily store my pdf file before sharing it. but path_provider does not support web. please what other way can i use to share the pdf decuments.
knowing that I want to create an incoice.
here is the context with some code
Future getPdf(Uint8List screenShot) async {
pw.Document pdf = pw.Document();
final img = pw.MemoryImage(screenShot);
pdf.addPage(
pw.Page(
pageFormat: PdfPageFormat.a4,
build: (context) {
return pw.Expanded(
child: pw.Image(img),
);
},
),
);
// File pdfFile = File('Your path + File name');
// pdfFile.writeAsBytesSync(await pdf.save());
print(pdf);
}
in this specific case I would like to use an alternative to path_provider

captureFromWidget returns black image in screenshot library

I'm trying to capture QrImage using screenshot package specifically .captureFromWidget method, I'm not building the widget QrImage, because I don't want to show it just print it out using the sumni_printer package.
I tried to show the QR before printing when it was an image, and it showed flawsly as it is.
here's the code:
Future<Uint8List> getQRBytes(String data) async {
QrImage qr = QrImage(
data: data,
size: 100,
);
ScreenshotController screenshotController = ScreenshotController();
Screenshot(
controller: screenshotController,
child: qr,
);
Uint8List bytes = await screenshotController.captureFromWidget(qr);
return bytes;
}
and the results when printed in sumni printer:
this is how I import the Uint8List when printing:
Uint8List qr = await getQRBytes(data);
await SunmiPrinter.printImage(qr);
here's how it looks in the app
It turns out it's the sunmi_printer package...
there's a new package, sunmi_printer_plus
it solved my issue and it's an amazing package

Trying to create a pdf in different languages in flutter

I want to print Hindi language in my PDF, I've also tried using Raleway-Regular.ttf font by Google but it also didn't worked.
final font = await rootBundle.load("fonts/ARIAL.TTF");
final ttf = pw.Font.ttf(font);
pdf.addPage(
pw.MultiPage(
pageFormat: PdfPageFormat.a4,
margin: pw.EdgeInsets.all(32),
build: (pw.Context context){
return <pw.Widget>[
pw.Center(
child: pw.Text("मेन",style: pw.TextStyle(fontSize: 16,font: ttf))
),
pw.SizedBox(height: 20),
pw.Center(
child: pw.Text("ABC",style: pw.TextStyle(fontSize: 16))
),
),
),
I'm getting following Error:
***I/flutter (24862): Cannot decode the string to Latin1.
I/flutter (24862): This font does not support Unicode characters.
I/flutter (24862): If you want to use strings other than Latin strings, use a TrueType (TTF) font instead.
I/flutter (24862): See https://github.com/DavBfr/dart_pdf/wiki/Fonts-Management
I/flutter (24862): ---------------------------------------------
E/flutter (24862): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: Invalid argument (string): Contains invalid characters.: "मेन"
I am also facing the same error its because the fonts you are using does not support that language , if you want to use hindi language then you can use hind-regular.ttl font it supports both the fonts hindi and english.
here is the example what i got.
// step 1- add font locatin in pubspec
fonts:
- family: hind
fonts:
- asset: assets/hind.ttf
//step 2-
final pdf = pw.Document();
final font = await rootBundle.load("assets/hind.ttf");
final ttf = pw.Font.ttf(font);
then just apply this ttf in the fontStyle
packages i m using -
import 'package:universal_html/html.dart' as html;
import 'package:pdf/widgets.dart' as pw;
universal_html: ^1.2.3
enter image description here
pdf: ^1.11.1
Some issues are there with Unicode translations . Please take care of it https://github.com/DavBfr/dart_pdf/issues/198
You can use syncfusion pdf library (You need to buy subscription). You will get all kind of support including search,rtl, unicode support .
final font = await rootBundle.load("assets/fonts/hind.ttf");
final ttf = Font.ttf(font);
final pdf = pw.Document();
pw.Text(
'Your text',
textDirection:TextDirection.rtl, // If you need in opp direction like urdu
style: pw.TextStyle(
font: ttf,))
If someone still has this problem
final data = await rootBundle.load('assets/fonts/opensans_regular.ttf');
final dataint = data.buffer.asUint8List(data.offsetInBytes,data.lengthInBytes);
final PdfFont font = PdfTrueTypeFont (date,12);
Most of the language not works well with the flutter pdf plugin
In Tamil language, I face problems when two characters merge.
so, i modified the tff with available tools and replaced that character with that character.
as the result, i can able to get the output
String path = 'assets/custom_Anand_MuktaMalar.ttf';
pw.Font? tamilFont = pw.Font.ttf(await rootBundle.load(path));
pw.Text('உவவுமதி உருவின் ஓங்கல் வெண்குடை'.toPrintPdf)
pdf.addPage(
pw.Page(
theme: pw.ThemeData(defaultTextStyle: pw.TextStyle(font: tamilFont)),
));
for further ref, you can see my project:
https://github.com/anandsubbu007/anand-work/tree/BLOC/tamil_pdf

How to create PDF file from API Response on Flutter

I'm learning to make an application that captures the response api from the server and then I want to make the response from the api into a pdf file in flutter, are there any instructions or examples of how I should make it? thanks for help...
You can use the response that you get from the API into a PDF file using this package: pdf
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as pw;
final pdf = pw.Document();
pdf.addPage(pw.Page(
pageFormat: PdfPageFormat.a4,
build: (pw.Context context) {
return pw.Center(
child: pw.Text(<API response>),
);
}));
final file = File("example.pdf");
await file.writeAsBytes(pdf.save());