pdf 1.3.1

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 95

Pdf creation library for dart / flutter #

This library is divided in 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.

Use printing package https://pub.dartlang.org/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, milimeters and inch defined in PdfPageFormat

Example:

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

To load an image it is possible to use the dart library image:

Image image = decodeImage(Io.File('test.webp').readAsBytesSync());
PdfImage image = PdfImage(
  pdf,
	image: img.data.buffer.asUint8List(),
	width: img.width,
	height: img.height);
g.drawImage(image, 100.0, 100.0, 80.0);

To use a TrueType font:

PdfTtfFont ttf = PdfTtfFont(
  pdf,
  (File("open-sans.ttf").readAsBytesSync() as Uint8List).buffer.asByteData());
g.setColor(PdfColor(0.3, 0.3, 0.3));
g.drawString(ttf, 20.0, "Dart is awesome", 50.0, 30.0);

To save the image on Flutter, use the path_provider library:

Directory tempDir = await getTemporaryDirectory();
String tempPath = tempDir.path;
var file = File("$tempPath/file.pdf");
await file.writeAsBytes(pdf.save());

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 anf 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

example/main.dart

import 'dart:io';

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

void main() {
  final pdf = Document(deflate: zlib.encode);

  pdf.addPage(MultiPage(
      pageFormat:
          PdfPageFormat.letter.copyWith(marginBottom: 1.5 * PdfPageFormat.cm),
      crossAxisAlignment: CrossAxisAlignment.start,
      header: (Context context) {
        if (context.pageNumber == 1) return null;
        return Container(
            alignment: Alignment.centerRight,
            margin: EdgeInsets.only(bottom: 3.0 * PdfPageFormat.mm),
            padding: EdgeInsets.only(bottom: 3.0 * PdfPageFormat.mm),
            decoration: BoxDecoration(
                border:
                    BoxBorder(bottom: true, width: 0.5, color: PdfColor.grey)),
            child: Text("Portable Document Format",
                style: Theme.of(context)
                    .defaultTextStyle
                    .copyWith(color: PdfColor.grey)));
      },
      footer: (Context context) {
        return Container(
            alignment: Alignment.centerRight,
            margin: EdgeInsets.only(top: 1.0 * PdfPageFormat.cm),
            child: Text("Page ${context.pageNumber}",
                style: Theme.of(context)
                    .defaultTextStyle
                    .copyWith(color: PdfColor.grey)));
      },
      build: (Context context) => <Widget>[
            Header(
                level: 0,
                child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: <Widget>[
                      Text("Portable Document Format", textScaleFactor: 2.0),
                      PdfLogo()
                    ])),
            Paragraph(
                text:
                    "The Portable Document Format (PDF) is a file format developed by Adobe in the 1990s to present documents, including text formatting and images, in a manner independent of application software, hardware, and operating systems. Based on the PostScript language, each PDF file encapsulates a complete description of a fixed-layout flat document, including the text, fonts, vector graphics, raster images and other information needed to display it. PDF was standardized as an open format, ISO 32000, in 2008, and no longer requires any royalties for its implementation."),
            Paragraph(
                text:
                    "Today, PDF files may contain a variety of content besides flat text and graphics including logical structuring elements, interactive elements such as annotations and form-fields, layers, rich media (including video content) and three dimensional objects using U3D or PRC, and various other data formats.[citation needed] The PDF specification also provides for encryption and digital signatures, file attachments and metadata to enable workflows requiring these features."),
            Header(level: 1, text: "History and standardization"),
            Paragraph(
                text:
                    "Adobe Systems made the PDF specification available free of charge in 1993. In the early years PDF was popular mainly in desktop publishing workflows, and competed with a variety of formats such as DjVu, Envoy, Common Ground Digital Paper, Farallon Replica and even Adobe's own PostScript format."),
            Paragraph(
                text:
                    "PDF was a proprietary format controlled by Adobe until it was released as an open standard on July 1, 2008, and published by the International Organization for Standardization as ISO 32000-1:2008, at which time control of the specification passed to an ISO Committee of volunteer industry experts. In 2008, Adobe published a Public Patent License to ISO 32000-1 granting royalty-free rights for all patents owned by Adobe that are necessary to make, use, sell, and distribute PDF compliant implementations."),
            Paragraph(
                text:
                    "PDF 1.7, the sixth edition of the PDF specification that became ISO 32000-1, includes some proprietary technologies defined only by Adobe, such as Adobe XML Forms Architecture (XFA) and JavaScript extension for Acrobat, which are referenced by ISO 32000-1 as normative and indispensable for the full implementation of the ISO 32000-1 specification. These proprietary technologies are not standardized and their specification is published only on Adobe's website. Many of them are also not supported by popular third-party implementations of PDF."),
            Paragraph(
                text:
                    "On July 28, 2017, ISO 32000-2:2017 (PDF 2.0) was published. ISO 32000-2 does not include any proprietary technologies as normative references."),
            Header(level: 1, text: "Technical foundations"),
            Paragraph(text: "The PDF combines three technologies:"),
            Bullet(
                text:
                    "A subset of the PostScript page description programming language, for generating the layout and graphics."),
            Bullet(
                text:
                    "A font-embedding/replacement system to allow fonts to travel with the documents."),
            Bullet(
                text:
                    "A structured storage system to bundle these elements and any associated content into a single file, with data compression where appropriate."),
            Header(level: 2, text: "PostScript"),
            Paragraph(
                text:
                    "PostScript is a page description language run in an interpreter to generate an image, a process requiring many resources. It can handle graphics and standard features of programming languages such as if and loop commands. PDF is largely based on PostScript but simplified to remove flow control features like these, while graphics commands such as lineto remain."),
            Paragraph(
                text:
                    "Often, the PostScript-like PDF code is generated from a source PostScript file. The graphics commands that are output by the PostScript code are collected and tokenized. Any files, graphics, or fonts to which the document refers also are collected. Then, everything is compressed to a single file. Therefore, the entire PostScript world (fonts, layout, measurements) remains intact."),
            Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Paragraph(
                      text:
                          "As a document format, PDF has several advantages over PostScript:"),
                  Bullet(
                      text:
                          "PDF contains tokenized and interpreted results of the PostScript source code, for direct correspondence between changes to items in the PDF page description and changes to the resulting page appearance."),
                  Bullet(
                      text:
                          "PDF (from version 1.4) supports graphic transparency; PostScript does not."),
                  Bullet(
                      text:
                          "PostScript is an interpreted programming language with an implicit global state, so instructions accompanying the description of one page can affect the appearance of any following page. Therefore, all preceding pages in a PostScript document must be processed to determine the correct appearance of a given page, whereas each page in a PDF document is unaffected by the others. As a result, PDF viewers allow the user to quickly jump to the final pages of a long document, whereas a PostScript viewer needs to process all pages sequentially before being able to display the destination page (unless the optional PostScript Document Structuring Conventions have been carefully complied with)."),
                ]),
            Header(level: 1, text: "Content"),
            Paragraph(
                text:
                    "A PDF file is often a combination of vector graphics, text, and bitmap graphics. The basic types of content in a PDF are:"),
            Bullet(
                text:
                    "Text stored as content streams (i.e., not encoded in plain text)"),
            Bullet(
                text:
                    "Vector graphics for illustrations and designs that consist of shapes and lines"),
            Bullet(
                text:
                    "Raster graphics for photographs and other types of image"),
            Bullet(text: "Multimedia objects in the document"),
            Paragraph(
                text:
                    "In later PDF revisions, a PDF document can also support links (inside document or web page), forms, JavaScript (initially available as plugin for Acrobat 3.0), or any other types of embedded contents that can be handled using plug-ins."),
            Paragraph(
                text:
                    "PDF 1.6 supports interactive 3D documents embedded in the PDF - 3D drawings can be embedded using U3D or PRC and various other data formats."),
            Paragraph(
                text:
                    "Two PDF files that look similar on a computer screen may be of very different sizes. For example, a high resolution raster image takes more space than a low resolution one. Typically higher resolution is needed for printing documents than for displaying them on screen. Other things that may increase the size of a file is embedding full fonts, especially for Asiatic scripts, and storing text as graphics. "),
            Header(level: 1, text: "File formats and Adobe Acrobat versions"),
            Paragraph(
                text:
                    "The PDF file format has changed several times, and continues to evolve, along with the release of new versions of Adobe Acrobat. There have been nine versions of PDF and the corresponding version of the software:"),
            Table.fromTextArray(context: context, data: [
              ["Date", "PDF Version", "Acrobat Version"],
              ["1993", "PDF 1.0", "Acrobat 1"],
              ["1994", "PDF 1.1", "Acrobat 2"],
              ["1996", "PDF 1.2", "Acrobat 3"],
              ["1999", "PDF 1.3", "Acrobat 4"],
              ["2001", "PDF 1.4", "Acrobat 5"],
              ["2003", "PDF 1.5", "Acrobat 6"],
              ["2005", "PDF 1.6", "Acrobat 7"],
              ["2006", "PDF 1.7", "Acrobat 8"],
              ["2008", "PDF 1.7", "Acrobat 9"],
              ["2009", "PDF 1.7", "Acrobat 9.1"],
              ["2010", "PDF 1.7", "Acrobat X"],
              ["2012", "PDF 1.7", "Acrobat XI"],
              ["2017", "PDF 2.0", "Acrobat DC"],
            ]),
            Padding(padding: EdgeInsets.all(10)),
            Paragraph(
                text:
                    "Text is available under the Creative Commons Attribution Share Alike License.")
          ]));

  var file = File('example.pdf');
  file.writeAsBytesSync(pdf.document.save());
}

Use this package as a library

1. Depend on it

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


dependencies:
  pdf: ^1.3.1

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter packages get

Alternatively, your editor might support pub get or flutter packages 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';
  
Version Uploaded Documentation Archive
1.3.1 Feb 6, 2019 Go to the documentation of pdf 1.3.1 Download pdf 1.3.1 archive
1.3.0 Feb 6, 2019 Go to the documentation of pdf 1.3.0 Download pdf 1.3.0 archive
1.2.0 Feb 3, 2019 Go to the documentation of pdf 1.2.0 Download pdf 1.2.0 archive
1.1.1 Jan 27, 2019 Go to the documentation of pdf 1.1.1 Download pdf 1.1.1 archive
1.1.0 Jan 21, 2019 Go to the documentation of pdf 1.1.0 Download pdf 1.1.0 archive
1.0.8 Dec 1, 2018 Go to the documentation of pdf 1.0.8 Download pdf 1.0.8 archive
1.0.7 Oct 28, 2018 Go to the documentation of pdf 1.0.7 Download pdf 1.0.7 archive
1.0.6 Sep 2, 2018 Go to the documentation of pdf 1.0.6 Download pdf 1.0.6 archive
1.0.5 Aug 21, 2018 Go to the documentation of pdf 1.0.5 Download pdf 1.0.5 archive
1.0.4 Aug 15, 2018 Go to the documentation of pdf 1.0.4 Download pdf 1.0.4 archive

All 14 versions...

Popularity:
Describes how popular the package is relative to other packages. [more]
90
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]
95
Learn more about scoring.

We analyzed this package on Feb 14, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.1.0
  • pana: 0.12.13

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:pdf/pdf.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.8.0 <3.0.0
meta ^1.1.5 1.1.7
utf ^0.9.0 0.9.0+5
vector_math ^2.0.0 2.0.8
Dev dependencies
test any