Home > Enterprise >  How to export data into a specific extension ? like PDF
How to export data into a specific extension ? like PDF

Time:12-08

I'm trying to program an app that let users write their novels or notes with fun features like listening to piano and raining sounds while writing...etc. Everything works fine and great but the problem is how can i turn those novels to PDF? ... I store everything as a "String" data type...ofc the user want to save his work which obviously I can do (Local storage or firebase) however i don't know about exporting data as a specific extension. how can I export the data as a PDF file if the user wanted to do that? is there a general way to do it?

CodePudding user response:

One possible solution can be pdf Flutter plugin. You can create and save a pdf file from strings. I tried this sample code and pdf is created:

//get the directory to save pdf file
late Directory? directory;
if (Platform.isIOS) {
  directory = await getApplicationDocumentsDirectory();
} else {
  directory = await getExternalStorageDirectory();
}
var path = directory?.path;

//create pdf file
final pdf = pw.Document();
pdf.addPage(
  pw.Page(
    build: (pw.Context context) => pw.Center(
      child: pw.Text('Hello World!'),
    ),
  ),
);

//save pdf file
await File('$path/test.pdf').writeAsBytes(await pdf.save());

You can use path_provider Flutter plugin to get the directory location

  • Related