PDF in Flutter | Complete Tutorial of Making/Saving PDF | FlutterTpoints

In this tutorial we are going to make a pdf using the flutter pdf library. Working with PDF in Flutter is the common requirement into the business app, e-commerce app, utility app and more etc. This library allow you to generate the digital pdf into the correct way so let’s wrap the things and continue to looking below for the full tutorial.

This tutorial will cover:

  • What is pdf in flutter?
  • What are the best pdf generator libraries in flutter?
  • How to generate pdf in pdf?
  • How to save generated pdf in flutter?

What is a PDF?

Flutter PDF refers to creating, reading, exporting or sharing text, images tables document directly inside a flutter application. With the help of packages like pdf or printing we can easily do:

  • Create pdf
  • View pdf
  • Share pdf
  • save pdf

Flutter allow both Android and IOS smooth pdf working.

What are the Best Pdf Generating Libraries in Flutter?

There some some libraries into Flutter to generate, view or export the pdf smoothly, those are as below:

1.pdf (free | most popular)

  • This is free pdf making library.
  • Mostly used library.
  • Supports, text, tables, images, layout and fonts, for more click here.

2.printing

  • Used for Pdf preview
  • Its totally free to use
  • It allows Print, Share and Download Option.
  • You can integrate perfectly with the pdf package easily.

3.flutter_pdfview

  • Its free also to view the pdf documents.
  • You can view the PDF document with the help of this package.

4.syncfusion_flutter_pdf

  • This is paid enterprises level package.
  • Advanced features like pdf editing and annotation.

How to Generate and Save a PDF into Flutter?

Let’s focus into the below steps to generate and save the pdf:

1. Step 1: Add Depedencies

Add the following dependencies into the pubspec.yml file :

You can run this command into the terminal:

Then run this command to sync the dependencies:

2.Step 2: Create a simple Pdf file

Using the pdf package create a simple widget file:

To load an image from mobile use this:

To load an image from asset file (web):

Create a Uint8List from the image

Create an image from the ImageBytes

implement the image in a container

To load an image from the network using the printing package:

To load an SVG:

To load the SVG from a Flutter asset, use await rootBundle.loadString('assets/file.svg')

To use a TrueType font:

Or using the printing package’s PdfGoogleFonts:

To display emojis:

3.Saving the PDF

To save the pdf file (Web): (saved as a unique name based on milliseconds since epoch)

Previewing PDF using printing

Add these libraries while creating the file widget:

For more ios and android setting plaase click here.

See all the examples:

To load an image from a Flutter asset:

To use a TrueType font from a flutter bundle:

To save the pdf file using the path_provider library:

You can also print the document using the iOS or Android print service:

Or share the document to other applications:

To print an HTML document:

import HTMLtoPDFWidgets

Convert a Pdf to images, one image per page, get only pages 1 and 2 at 72 dpi:

To print an existing Pdf file from a Flutter asset:

Display your PDF document 

This package also comes with a PdfPreview widget to display a pdf document.

This widget is compatible with Android, iOS, macOS, Linux, Windows and web.

Designing your PDF document 

A good starting point is to use PdfPreview which features hot-reload pdf build and refresh.

Take a look at the example tab for a sample project.

Update the _generatePdf method with your design.

This widget also features a debug switch at the bottom right to display the drawing constraints used. This switch is available only on debug builds.

Moving on to your production application, you can keep the _generatePdf function and print the document using:

final title = 'Flutter Demo';
await Printing.layoutPdf(onLayout: (format) => _generatePdf(format, title));

Thanks for visiting the tutorial for more tutorials click here.

Leave a Comment