Simple PDF Generating App in Flutter

In the realm of mobile app development, the ability to generate PDF documents is a valuable skill. Flutter, with its versatile widget library, allows developers to create PDFs seamlessly. In this blog post, we'll walk you through the process of building a simple PDF generating app in Flutter.

Prerequisites

Before we dive into the code, make sure you have Flutter installed on your machine. You'll also need an IDE of your choice, such as VS Code or Android Studio.

Setting Up the Project

Start by creating a new Flutter project:

flutter create pdf_generator_app 

Next, open your project in your preferred IDE.

Adding Dependencies

To generate PDFs in Flutter, we'll use the pdf package. Open your pubspec.yaml file and add the dependency:

dependencies: 
  pdf: ^3.10.4
  path_provider: ^2.1.1

Then, run flutter pub get in your terminal to fetch the package.

Designing the App

In this simple app, we'll have a button that, when pressed, generates a PDF document.

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:pdf/widgets.dart' as pw;
import 'package:path_provider/path_provider.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});


  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: Scaffold(
        appBar: AppBar(
          title: const Text('PDF Generator App'),
        ),
        body: Center(
          child: ElevatedButton(
            onPressed: () {
              // Generate PDF
              generatePDF();
            },
            child: const Text('Generate PDF'),
          ),
        ),
      ),
    );
  }


  void generatePDF() {
    final pdf = pw.Document();


    pdf.addPage(
      pw.Page(
        build: (pw.Context context) {
          return pw.Center(
            child: pw.Text('Hello World'),
          ); // Center
        },
      ),
    );


    // Save the PDF to a file
    savePDF(pdf);
  }


  Future<void> savePDF(pw.Document pdf) async {
    // Get external storage directory
    final dir = await getTemporaryDirectory();


    // Define the file path
    final file = File('${dir.path}/example.pdf');


    // Write the PDF to the file
    await file.writeAsBytes(await pdf.save());
  }
}

In this code, we have a simple Flutter app with a button. When the button is pressed, the generatePDF function is called.

Generating the PDF

The generatePDF function creates a new PDF document using the pw.Document class from the pdf package. It adds a page with a centered text saying "Hello World".

Saving the PDF

The savePDF function saves the generated PDF to the device's external storage. It uses the getExternalStorageDirectory function from the path_provider package to get the external storage directory. Then, it defines the file path and writes the PDF to the file.

To Know More About PDF Package

pdf: ^3.10.4

Complete Project in Github

https://github.com/WhiteListai-in/flutter-widgets

Conclusion: Creating PDFs with Flutter

With Flutter and the pdf package, generating PDFs becomes a seamless process. This simple app is just the beginning - you can expand on this foundation to create more complex PDF generating applications.

Happy coding, and may your Flutter apps produce beautifully formatted PDF documents!

Description of the image

Related Posts