CREATE INVOICE PDF using FLUTTER WEB - flutter

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

Related

Flutter PDF Rotation

I use this package (https://pub.dev/packages/pdf) to create PDF files with app.. Now I can't find a way to rotate the page of PDF.
Try this. Comments in code.
pdf.addPage(
pw.Page(
pageTheme: pw.PageTheme(
// orientation: pw.PageOrientation.landscape, //this set the orientation of the content of the page, not the page itself. Which confuses most people
pageFormat: PdfPageFormat.a4.landscape, //this is what you want
...
),
build: (ctx) => ChildWidget(),
...
Source: https://github.com/DavBfr/dart_pdf/issues/557

Flutter: save a generated PDF from preview page with a custom function

I'm using the pdf package (link to pub.dev page) to create documents inside my app.
How can I save the document from the preview page?
I know about the built-in print and share functions, but I need something else.
I need a function that does the following:
save the document to the device
upload it to Firebase Storage
delete the local file
I know how to do all of these steps, but I'm unable to access the Uint8List bytes of the pdf document that is being previewed... I've tried to add a PdfPreviewAction, but I still can't access the bytes of the document from it
all you have to do is call the save method in the Document() object
import "package:pdf/widgets.dart" as w;
final pdf = w.Document();
Future<Uint8List> getPDFData() async {
return await pdf.save();
}
you can do all the operations using the getPDFData() method
for example:
return PdfPreview(
build: (format) => getPdfData(),
actions: [
PdfPreviewAction(
icon: Icon(Icons.print),
onPressed: (context, fn, format) {
//if you call fn(format), you'll get Future<UInt8List>
}),
],
);

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

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());

How do i save a generated PDF to Accessible storage?

I did generated a pdf from HTML code , using html_to_pdf pluggin ,
but it seems that it saves it in DocumentAppPath (data/user/0/com.exmpl.myapp/app_flutter/) which is not something easy to access by a user whom does not know much .
Future<void> generateExampleDocument() async {
var htmlContent = widget.htmlCont; // html Cv File's content
Directory appDocDir = await getApplicationDocumentsDirectory(); // appDocDir
var targetPath = appDocDir.path;
var targetFileName = "example-pdf";
var generatedPdfFile = await FlutterHtmlToPdf.convertFromHtmlContent(
htmlContent, targetPath, targetFileName); // generates pdf document at the given path.
generatedPdfFilePath = generatedPdfFile.path; // variable outside function .
}
then displaying the pdf , it works fine However as i said i want it to be saved somewhere where the user access it for example Download folder .. so after i can simply Open it from that path outside my app .
onPressed: () => Navigator.push(
context,
MaterialPageRoute(
builder: (context) => PDFViewerScaffold(
appBar: AppBar(title: Text("SHOW TEST")),
path: generatedPdfFilePath)),
),
),
Thanks.
I've used esys_flutter_share package. I had same task, but I was generating pdf from the widget using pdf package. So then I've add save button, wich calls share function with options to save the file on mobile or send it someone else.
final sharePdf = await generatedPdfFile.readAsBytes();
await Share.file(
'PDF Document',
'project.pdf',
sharePdf.buffer.asUint8List(),
'*/*',
);