X
    Categories: Flutter

Create pdf file in your app | Flutter

Create PDF :

Create PDF is a feature which can be used to create a documents based on the requirement for example like invoices generated from mobile apps.

In this blog we will be dealing with creation of pdf file in flutter app.In previous tutorial we have seen reading a pdf file in your flutter app.

A PDF (Portable Document Format) is a file format used to present documents in a manner that is independent of software, hardware, and operating systems. It was developed by Adobe Systems in the 1990s as a way to exchange documents that include text formatting and images, while preserving their original appearance across different devices and platforms.

PDF files can contain text, graphics, images, and other types of content. They can also include interactive elements such as hyperlinks, bookmarks, and forms. PDFs can be viewed, printed, and shared by anyone who has a compatible PDF reader software, such as Adobe Reader or Foxit Reader.

 

pubspec.yaml :

Add the required pdf and path provider dependencies and update versions accordingly.

dependencies:
  flutter:
    sdk: flutter


  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^1.0.2
  pdf: ^3.10.1
  path_provider: ^2.0.14

 

Create PDF Video Tutorial :

For more detailed information on flutter pdf file creation.

main.dart :

Let’s create a button such that when we tap on button a pdf file will be created and saved in the folder.

We will be creating pages and add them to document.

In the tutorial you will be saving the created pdf file in a location in video tutorial we have clearly guided you through the path where pdf file is stored so go through the tutorial to find the path.

This is how we add pages in our document file

pdf.addPage(
  p.Page(build: (context){
    return p.Column(
      children: [
        p.Text("AmplifyAbhi"),
        p.Text("AndroidCoding.in"),
        p.Text("Tested")
      ]
    );
  })
);

Like wise you can add multiple pages into your pdf document.

 


import 'dart:io';

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

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: const Text("Create a pdf"),),
        body: Center(
          child: TextButton(onPressed: (){
            makePdf();
          }, child: const Text("Create pdf"),),
        ),
      ),
    );
  }

  Future<void> makePdf() async{

    final pdf = p.Document();

    pdf.addPage(
      p.Page(build: (context){
        return p.Column(
          children: [
            p.Text("AmplifyAbhi"),
            p.Text("AndroidCoding.in"),
            p.Text("Tested")
          ]
        );
      })
    );

    Directory root = await getApplicationDocumentsDirectory();
    String path = '${root.path}/test.pdf';
    final file = File(path);
    await file.writeAsBytes(await pdf.save());
    print("Path  "+path);
  }
}

 

If you have any query in the create pdf tutorial do let us know in the comment section below. If you like this tutorial do like and share this tutorial for more interesting updates.

abhishek:
Related Post

This website uses cookies.