Skip to content

Software Development at Program Tom LTD

Place for coding, programming, development and software in general.

Menu
  • Blog
  • PDF Booklets
  • Dev Utils & Content
  • Java Spring Boot Or Web Apps
  • English
    • български
    • English
    • Español
    • Português
    • हिन्दी
    • Русский
    • Deutsch
    • Français
    • Italiano
    • العربية
  • About Us
  • Flutter Apps
Menu
Generate PDF Flutter

How to Generate PDF in Flutter

Posted on March 2, 2025 by Toma Velev

To Generate PDF documents in Flutter can be achieved through several packages. Here are a few popular ones:

  1. pdf: This package provides a simple way to create and manipulate PDF documents in Dart.
  2. syncfusion_flutter_pdf: This package provides a comprehensive set of features for generating and manipulating PDF documents. https://pub.dev/packages/syncfusion_flutter_pdf The also have a lot of widget components thay may be used for free up until a moment you have enough money to pay them back https://www.syncfusion.com/sales/communitylicense

Here’s an example using the pdf package to generate a simple PDF:

First, add the following dependency to your pubspec.yaml file:

dependencies:
  pdf: ^3.11.3

Then, run flutter pub get to install the package.

Now, you can use the following code to generate a PDF:

import 'dart:io';

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

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

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

  @override
  Widget build(BuildContext context) => MaterialApp(
      home: MyHomePage(),
    );
}

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

  @override
  Widget build(BuildContext context) => Scaffold(
        appBar: AppBar(
          title: Text('Generate PDF'),
        ),
        body: Center(
          child: ElevatedButton(
            onPressed: () async {
              final pdf = pw.Document();
              pdf.addPage(pw.Page(
                build: (context) {
                  return pw.Column(
                    children: [
                      pw.Text('Hello, World!',
                          style: pw.TextStyle(fontSize: 24)),
                      pw.SizedBox(height: 20),
                      pw.Text('This is a sample PDF generated by Flutter.',
                          style: pw.TextStyle(fontSize: 18)),
                    ],
                  );
                },
              ));
              final bytes = await pdf.save();
              saveAndShareFile(bytes, 'sample.pdf');
            },
            child: Text('Generate PDF'),
          ),
        ),
      );

  void saveAndShareFile(List<int> bytes, String filename) async {
    final file = await File(filename).create();
    await file.writeAsBytes(bytes);
    if (kDebugMode) {
      print('PDF saved to $filename');
    }
  }
}

This code will generate a simple PDF with two lines of text and save it to the device’s storage. The saveAndShareFile function is used to create a file and write the PDF bytes to it.

Another Packages that may be mentioned here are:

  1. printing. This package allows you to print documents, including generating PDFs. It is a common in all printing to pdf on all operating systems, office software, etc – to have the feature “print to PDF“.
  2. flutter_pdfview: This package allows you to view PDFs in your Flutter app.

If you need to render HTML, not PDFs, you could read how to do it here: https://programtom.com/dev/2024/12/28/flutter-html/

  • What are ways to Optimize the backend endpoints in Spring Boot
  • Flutter image flickers
  • Could a Flutter App save a Flag even after uninstall
  • Could iOS flutter app logs be viewed while running in release mode – started after previous closed state
  • 6 Addictive Mobile Game Ideas Inspired by Flappy Bird’s Simplicity

Categories

  • Apps (20)
  • ChatGPT (19)
  • Choosing a Framework (38)
  • Flutter (206)
  • Graphical User Interface (13)
  • Marketing (114)
  • Software Development (270)
  • Spring (43)
  • StartUp (21)
  • Uncategorized (4)
  • Uncategorized (15)
  • Vaadin (14)

Tags

Algorithms (9) crypto (29) flutterdev (39) General (86) Java (7) QR & Bar Codes (3) Software Dev Choices (33) Spring Boot (1) standards (1) Theme (3) User Authentication & Authorization (9) User Experience (10) Utilities (19) WordPress (11)

Product categories

  • All Technologies (83)
    • Flutter Apps (23)
    • GPT (4)
    • Java (38)
    • Native Android (3)
    • PHP (9)
    • Spring (Boot) / Quarkus (35)
    • Utils (15)
    • Vaadin 24+ (27)
    • Vaadin 8 (1)
  • Apps (18)
    • Employees DB (1)
    • Notes (6)
    • Personal Budget (1)
    • Recipes Book (1)
    • Stuff Organizer (1)
    • To-Do (2)
  • PDF Books (3)
  • Source Code Generators (8)

Recent Posts

  • What are ways to Optimize the backend endpoints in Spring Boot
  • Flutter image flickers
  • Could a Flutter App save a Flag even after uninstall
  • Could iOS flutter app logs be viewed while running in release mode – started after previous closed state
  • 6 Addictive Mobile Game Ideas Inspired by Flappy Bird’s Simplicity

Post Categories

  • Apps (20)
  • ChatGPT (19)
  • Choosing a Framework (38)
  • Flutter (206)
  • Graphical User Interface (13)
  • Marketing (114)
  • Software Development (270)
  • Spring (43)
  • StartUp (21)
  • Uncategorized (4)
  • Uncategorized (15)
  • Vaadin (14)