I am using the printing library in Flutter to show a PDF preview. It causes the default browser dialog to show up, and I have an option to save the PDF to my machine. When I save it to my machine, following is the name of the file:
I want to give it a name of my own choice. So far this is what I have tried:
- Set the 'name' parameter in
Printing.layoutPDF(The result is still the same) - Tried using
Printing.sharePDFand setting thefilenameparameter inside it. (I get my desired name, but this gets rid of the print preview, which I do not want).
The next solution that comes to my mind is to create a custom print preview and give a download option to the user that uses Printing.sharePDF under the hood.
I am asking here to know if there is perhaps a simpler solution to this problem that I am missing that will allow me to set the name when I press the browser print button.
Sample code that reproduces this problem is as follows. I am running this on Google Chrome:
import 'package:flutter/material.dart';
import 'package:printing/printing.dart';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as pw;
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Print Preview Demo',
home: const PrintPreviewPage(),
);
}
}
class PrintPreviewPage extends StatelessWidget {
const PrintPreviewPage({super.key});
Future<void> _printDocument() async {
final pdf = pw.Document();
pdf.addPage(
pw.Page(
build: (pw.Context context) => pw.Center(
child: pw.Text('Hello, Flutter Print!'),
),
),
);
await Printing.layoutPdf(
name:'my_pdf.pdf', //This is what I have tried (option 1)
onLayout: (PdfPageFormat format) async => pdf.save(),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Print Preview Demo')),
body: Center(
child: ElevatedButton(
onPressed: _printDocument,
child: const Text('Print Preview'),
),
),
);
}
}


final pdf = pw.Document(title:'my_pdf.pdf');. Now, when I press Save to save the pdf via the Chrome dialog, I see the title as the filename. I will be writing an answer to my question.