pdf 1.8.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 98

Pdf creation library for dart/flutter #

This library is divided into two parts:

  • a low-level Pdf creation library that takes care of the pdf bits generation.
  • a Widgets system similar to Flutter's, for easy high-level Pdf creation.

It can create a full multi-pages document with graphics, images, and text using TrueType fonts. With the ease of use you already know.

See an interactive demo here: https://davbfr.github.io/dart_pdf/.

Example document

Use the printing package https://pub.dev/packages/printing for full flutter print and share operation.

The coordinate system is using the internal Pdf unit:

  • 1.0 is defined as 1 / 72.0 inch
  • you can use the constants for centimeters, millimeters, and inch defined in PdfPageFormat

Buy Me A Coffee

Installing #

If you want to print the Pdf document on an actual printer with Flutter, follow the instructions at https://pub.dev/packages/printing

  1. Add this package to your package's pubspec.yaml file as described on the installation tab

  2. Import the libraries

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

Examples #

final pdf = pw.Document();

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

To load an image from a file:

final image = PdfImage.file(
  pdf.document,
  bytes: File('test.webp').readAsBytesSync(),
);

pdf.addPage(pw.Page(
    build: (pw.Context context) {
      return pw.Center(
        child: pw.Image(image),
      ); // Center
    })); // Page

To use a TrueType font:

final Uint8List fontData = File('open-sans.ttf').readAsBytesSync();
final ttf = pw.Font.ttf(fontData.buffer.asByteData());

pdf.addPage(pw.Page(
    pageFormat: PdfPageFormat.a4,
    build: (pw.Context context) {
      return pw.Center(
        child: pw.Text('Hello World', style: pw.TextStyle(font: ttf, fontSize: 40)),
      ); // Center
    })); // Page

To save the pdf file:

// On Flutter, use the [path_provider](https://pub.dev/packages/path_provider) library:
//   final output = await getTemporaryDirectory();
//   final file = File("${output.path}/example.pdf");
final file = File("example.pdf");
await file.writeAsBytes(pdf.save());

Encryption and Digital Signature #

Encryption using RC4-40, RC4-128, AES-128, and AES-256 is fully supported using a separate library. This library also provides SHA1 or SHA-256 Digital Signature using your x509 certificate. The graphic signature is represented by a clickable widget that shows Digital Signature information.

Drop me an email for availability and more information.

Changelog #

1.8.1 #

  • Fix Wrap break condition
  • Fix drawShape method [Paweł Szot]

1.8.0 #

  • Improve Table.fromTextArray()
  • Add curved LineDataSet Chart
  • Fix PdfColors.fromHex()
  • Update Barcode library to 1.9.0
  • Fix exif orientation crash
  • Fix Spacer Widget

1.7.1 #

  • Fix justified text softWrap issue
  • Set a default color for Dividers
  • Fix InheritedWidget issue with multiple pages

1.7.0 #

  • Implement Linear and Radial gradients in BoxDecoration
  • Fix PdfColors.shade()
  • Add dashed lines to Decoration Widgets
  • Add TableRow decoration
  • Add Chart Widget [Marco Papula]
  • Add Divider and VerticalDivider Widget
  • Replace Theme with ThemeData

1.6.2 #

  • Use the Barcode library to generate QR-Codes
  • Fix Jpeg size detection
  • Update dependency to Barcode 1.8.0
  • Fix graphic state operator

1.6.1 #

  • Fix Image width and height attributes

1.6.0 #

  • Improve Annotations
  • Implement table row vertical alignment
  • Improve Internal data structure
  • Remove deprecated functions
  • Optimize file size
  • Add PdfColor.shade
  • Uniformize examples
  • Fix context painting empty Table
  • Fix Text decoration placements
  • Improve image buffer management
  • Optimize memory footprint
  • Add an exception if a jpeg image is not a supported format
  • Add more image loading functions

1.5.0 #

  • Fix Align debug painting
  • Fix GridView when empty
  • Reorder MultiPage paint operations
  • Fix Bullet widget styling
  • Fix HSV and HSL Color constructors
  • Add PageTheme.copyWith
  • Add more font drawing options
  • Add Opacity Widget
  • Fix Text height with TrueType fonts
  • Convert Flex to a SpanningWidget
  • Add Partitions Widget
  • Fix a TrueType parser issue with some Chinese fonts

1.4.1 #

  • Update dependency to barcode ^1.5.0
  • Update type1 font warning URL
  • Fix Image fit

1.4.0 #

  • Improve BarcodeWidget
  • Fix BarcodeWidget positioning
  • Update dependency to barcode ^1.4.0

1.3.29 #

  • Use Barcode stable API

1.3.28 #

  • Add Barcode Widget
  • Add QrCode Widget

1.3.27 #

  • Add Roll Paper support
  • Implement custom table widths

1.3.26 #

  • Update Analysis options

1.3.25 #

  • Add more warnings on type1 fonts
  • Simplify PdfImage constructor
  • Implement Image orientation
  • Add Exif reader
  • Add support for GreyScale Jpeg
  • Add FullPage widget

1.3.24 #

  • Update Web example
  • Add more color functions
  • Fix Pdf format
  • Fix warning in tests
  • Fix warning in example
  • Format Java code
  • Add optional clipping on Page
  • Add Footer Widget
  • Fix Page orientation
  • Add Ascii85 test

1.3.23 #

  • Implement ListView.builder and ListView.separated

1.3.22 #

  • Fix Text alignment
  • Fix Theme creation

1.3.21 #

  • Add TextDecoration

1.3.20 #

  • Fix Transform.rotateBox
  • Add Watermark widget
  • Add PageTheme

1.3.19 #

  • Fix Ascii85 encoding

1.3.18 #

  • Implement InlineSpan and WidgetSpan
  • Fix Theme.withFont factory
  • Implement InheritedWidget
  • Fix Web dependency
  • Add Web example

1.3.17 #

  • Fix MultiPage with multiple save() calls

1.3.16 #

  • Add better debug painting on Align Widget
  • Fix Transform placement when Alignment and Origin are Null
  • Add Transform.rotateBox constructor
  • Add Wrap Widget

1.3.15 #

  • Fix Image shape inside BoxDecoration

1.3.14 #

  • Add Document ID
  • Add encryption support
  • Increase PDF version to 1.7
  • Add document signature support
  • Default compress output if available

1.3.13 #

  • Do not modify the TTF font streams

1.3.12 #

  • Fix TextStyle constructor

1.3.11 #

  • Update Readme

1.3.10 #

  • Deprecate the document argument in Printing.sharePdf()
  • Add a default value to alpha in PdfColor variants
  • Fix Table Widget
  • Add Flexible and Spacer Widgets

1.3.9 #

  • Fix Transform Widget alignment
  • Fix CustomPaint Widget size
  • Add DecorationImage to BoxDecoration
  • Add default values to ClipRRect

1.3.8 #

  • Add jpeg image loading function
  • Add Theme::copyFrom() method
  • Allow Annotations in TextSpan
  • Add SizedBox Widget
  • Fix RichText Widget word spacing
  • Improve Theme and TextStyle
  • Implement properly RichText.softWrap
  • Set a proper value to context.pagesCount

1.3.7 #

  • Add Pdf Creation date
  • Support 64k glyphs per TTF font

1.3.6 #

  • Fix TTF Font SubSetting

1.3.5 #

  • Add some color functions
  • Remove color constants from PdfColor, use PdfColors
  • Add TTF Font SubSetting
  • Add Unicode support for TTF Fonts
  • Add Circular Progress Indicator

1.3.4 #

  • Add available dimensions for PdfPageFormat
  • Add Document properties
  • Add Page.orientation to force landscape or portrait
  • Improve MultiPage Widget
  • Convert GridView to a SpanningWidget
  • Add all Material Colors
  • Add Hyperlink widgets

1.3.3 #

  • Fix a bug with the RichText Widget
  • Update code to Dart 2.1.0
  • Add Document.save() method

1.3.2 #

  • Fix dart lint warnings
  • Improve font bounds calculation
  • Add RichText Widget
  • Fix MultiPage max-height
  • Add Stack Widget
  • Update Readme

1.3.1 #

  • Fix pana linting notices

1.3.0 #

  • Add a Flutter-like Widget system

1.2.0 #

  • Change license to Apache 2.0
  • Improve PdfRect
  • Add support for CMYK, HSL and HSV colors
  • Implement rounded rect

1.1.1 #

  • Improve PdfPoint and PdfRect
  • Change PdfColor.fromInt to const constructor
  • Fix drawShape Bézier curves
  • Add arcs to SVG drawShape
  • Add default page margins
  • Change license to Apache 2.0

1.1.0 #

  • Rename classes to satisfy Dart conventions
  • Remove useless new and const keywords
  • Mark some internal functions as protected
  • Fix annotations
  • Implement default fonts bounding box
  • Add Bézier Curve primitive
  • Implement drawShape
  • Add support for Jpeg images
  • Fix numeric conversions in graphic operations
  • Add Unicode support for annotations and info block
  • Add Flutter example

1.0.8 #

  • Fix monospace TTF font loading
  • Add PDFPageFormat::toString

1.0.7 #

  • Use lowercase page dimension constants

1.0.6 #

  • Fix TTF font name lookup

1.0.5 #

  • Remove dependency to dart:io
  • Add Contributing

1.0.4 #

  • Updated homepage
  • Update source formatting
  • Update README

1.0.3 #

  • Remove dependency to ttf_parser

1.0.2 #

  • Update SDK support for 2.0.0

1.0.1 #

  • Add example
  • Lower vector_math dependency version
  • Uses better page format object

1.0.0 #

  • Initial version

example/main.dart

// ignore_for_file: always_specify_types

import 'dart:io';

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

void main() {
  final doc = pw.Document();

  doc.addPage(
    pw.Page(
      build: (pw.Context context) => pw.Center(
        child: pw.Text('Hello World!'),
      ),
    ),
  );

  final file = File('example.pdf');
  file.writeAsBytesSync(doc.save());
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  pdf: ^1.8.1

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:pdf/pdf.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
96
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
98
Learn more about scoring.

We analyzed this package on Jun 4, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.2
  • pana: 0.13.8-dev

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.3.0 <3.0.0
archive ^2.0.10 2.0.13
barcode ^1.9.0 1.11.0
crypto ^2.0.6 2.1.5
image ^2.1.4 2.1.13
meta ^1.1.5 1.1.8
path_parsing ^0.1.4 0.1.4
utf ^0.9.0 0.9.0+5
vector_math ^2.0.0 2.0.8
Transitive dependencies
args 1.6.0
charcode 1.1.3
collection 1.14.12
convert 2.1.1
path 1.7.0
petitparser 3.0.4
qr 1.2.0
typed_data 1.1.6
xml 3.7.0 4.2.0
Dev dependencies
pedantic 1.9.0
test any