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());
Related
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
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
Is there any package or method available to save/download image from URL in flutter for web as well app.
You can use Firebase storage or s3-Cognito to achieve this. First, you need to upload the image to the Database using file picker or image picker and use the getDownloadUrl() method to get the imageUrl
You don't need a package.
import 'package:http/http.dart' as http;
import 'package:flutter/material.dart';
import 'dart:typed_data';
TextButton(
onPressed: () async {
String imageUrl='https://......example';
Uint8List response = await http.get(Uri.parse(imageUrl)).then(
(value) => value.bodyBytes);
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
content: Image.memory(response),
);
},
);
},
child: Text(
'Show Image from memory',
));
This code alone works without any packages. Now that you have it in memory, you can store it and do other things with it. Convert it between base64 and Unit8List.
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,
I have some Uint8lists and I want to save them as jpg files.
Can anyone help?
By 'storage', do you mean write to a file? You don't really need "flutter" to do this. Just use the libraries provided by dart. Here is an example of downloading my gravatar which you can get as Uin8List and then saving it to a file.
import 'dart:io';
import 'dart:typed_data';
import 'package:http/http.dart' as http;
void main() {
http.get('https://www.gravatar.com/avatar/e944138e1114aefe4b08848a46465589').then((response) {
Uint8List bodyBytes = response.bodyBytes;
File('my_image.jpg').writeAsBytes(bodyBytes);
});
}
Here is a simple and short solution of your problem. Use this line in your code as I did:
"SettableMetadata(contentType: "image/jpeg"),"
Code:
if (kIsWeb) {
await ref.putData(
await imageFile.readAsBytes(),
SettableMetadata(contentType: "image/jpeg"),
);
url = await ref.getDownloadURL();
}