How to directly print a pdf file in assets with flutter? - flutter

I'm trying to print a pdf file using flutter app. I have used this Printing package to achieve this but the problem is it has this default page with options to save pdf and select printer. I don't want these options, I just want to print file in one click. How do I achieve this??
This is my code :
Scaffold(
body: Center(
child:
ElevatedButton(onPressed: ()async{
final pdf =await rootBundle.load("assets/dummy.pdf");
await Printing.layoutPdf(onLayout: (_) => pdf.buffer.asUint8List());
}, child: const Text("PRINT")),
),
);
I want print pdf without showing this page

Related

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

type 'Future<File>' is not a subtype of type 'File'

Important: I have searched a lot but did not find any solution that I can understand.
I am using photo manager to show images and then after selecting an image I want to navigate to the next screen and show that image there (later I want to upload that image to firebase storage).
So the type of File I am getting from the photo_manager package is Future < File > and I am receiving File from the second screen.
How can I convert future to file?
First Screen:
IconButton(
icon: const Icon(Icons.arrow_forward, color: primaryColor),
onPressed: () {
Get.to(PostDetail(
imgFile:
_controller.mediaList[_controller.selectedImageIndex].file,
));
},
),
Second Screen:
On the second screen, I'm getting File.
final File imgFile;
const PostDetail({this.imgFile});
Source Code:: Add Post Page
So as described in photo_manager docs, you need to get the file from the AssetEntity list before moving to the next page.
So on button pressed to go to next page get the file and then pass it to the Page
Below code should work:
IconButton(
icon: const Icon(Icons.arrow_forward, color: primaryColor),
onPressed: () async {
File file = await _controller.mediaList[_controller.selectedImageIndex].file; // image file
Get.to(PostDetail(
imgFile: file,
));
},
),
File and Future<File> are 2 different things. the File is a variable that its value is in the present. but Future<File> as its name suggests, its value exist in the Future which means its value takes some time to reach you.
for example, if you send a request to a webserver, the response takes some time to reach you. so, in that case the response is a Future.
in your case the image you are trying to get is supplied in a way that it takes time. to get the File from Future<File> you have to wait. So before sending the file to this Screen, use an async and wait to turn the Future<File> to File. (i dont know how you get the file)
for example,
File myFile = await getFileorWhaterver();
Your _controller.mediaList[_controller.selectedImageIndex].file is a Future<File> means that the value of the File is promised to be returned sometimes in the future. This usually because you are doing a network call asynchronously to get the data from API.
In order to convert it to File, you need to await till the moment that value is available. However you cannot do an async/await within the UI, that's why Flutter provide a widget named FutureBuilder to wait for a certain Future value, then let you use that value in the UI.
In your case, you can do this:
FutureBuilder(
future: _controller.mediaList[_controller.selectedImageIndex].file,
builder: (context, snapshot) {
return IconButton(
icon: const Icon(Icons.arrow_forward, color: primaryColor),
onPressed: () {
if (snapshot.hasData)
Get.to(PostDetail(imgFile: snapshot.data));
},
);
})

How to solve the error "Error parsing asset file" in advanced_pdf_viewer in flutter?

I am new to flutter and developing a custom app(just for testing). I have bunch of pdfs in docs folder. So to view respective pdf, I passed an id to the pdf viewer to load the pdf file from button. When I type the pdf name (static) in loaddocument() , it works fine.
FYI - all pdfs are starting with name of law and have integer value in end. for example law1.pdf.
here is the function code.
LoadPdf
import 'package:advance_pdf_viewer/advance_pdf_viewer.dart';
loadDocument() async {
String id= widget.lawid;
String filename= 'law$id';
print(filename);
document = await PDFDocument.fromAsset('docs/law/$filename.pdf');
setState(() => isLoading = false);
}
#override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
body: Center(
child: isLoading
? Center(child: CircularProgressIndicator())
: PDFViewer(
document: document,
zoomSteps: 1,
scrollDirection: Axis.vertical,
)),
));
}
I got the error when I pass the id from Button. The error is Error parsing asset file. I am using advance_pdf_viewer package. Please suggest me too if you have any other pdf viewer package. This package doesn't support continuous scroll (it renders one pager per scroll).
Your help would be appreciated.

How to show string value in flutter sqflite

I'm using sqflite in flutter for mobile devices's local database.
I made a test table and I success print a table's value using 'debugprint'.
But I can not print the value in device's screen. Maybe because asynchronous? (I don't know exactly)
I want to print the value in screen.
This is my source code.
This is a scaffold's body
body: Center(
child: Column(
children: <Widget>[
new Text( getFirst().toString() ),
],
),
),
And this is a function. debugPrint's value is what I wanted.
Future<String> getFirst() async {
final List<Map<String, dynamic>> maps = await _databaseHelper.db.rawQuery(
" SELECT attr1 FROM Test_t");
debugPrint(maps.toString());
return maps.toString();
}
You can't print the value as it's asynchronous. getFirst() - returns a future, to retrieve value you need to await for it.
There are few ways to do that:
FutureBuilder https://flutterigniter.com/build-widget-with-async-method-call/
https://flutter.dev/docs/development/ui/widgets/async
Could also use states to do that...

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(),
'*/*',
);