Generate a PDF from a Template in Flutter

PSPDFKit for Flutter enables you to create a new PDF document from a template. The template can be a page pattern or a page from an existing PDF document.

License

To generate a PDF from a template, contact Sales to add the PDF Generation component to your license.

Predefined Page Patterns

The table below summarizes the predefined list of page patterns.

Template
Page Pattern Constant
Blank PagePattern.blank
Dots 5 mm PagePattern.dots_5mm
Grid 5 mm PagePattern.grid_5mm
Line 5 mm PagePattern.lines_5mm
Line 7 mm PagePattern.lines_7mm

Creating Custom Tiled Templates

In addition to using the predefined patterns, you can build customized page templates from your own PDF documents. To do this, use the PagePattern.fromDocument method that takes a file URI as its parameter.

Many use cases for page templates require the background to be tiled/patterned. A page is tiled when there are one or more images repeated on the page.

Creating a tiled page template requires the source document to be exported correctly. This means that the source PDF needs to contain a pattern itself. If a PagePattern is instantiated using PagePattern.fromDocument and the source document doesn’t contain a pattern, the rendering will fail silently.

To use a PDF as a source for a tiled page template, ensure it has the pattern path information embedded. To do this, use Adobe Illustrator or any other vector editing tool.

When creating your own patterns, consider the following:

  • The path information is embedded in the PDF rendered on the page, and not the actual PDF.

  • If your custom pattern requires spacing between tiles, include this information in the pattern information.

For testing purposes, use this sample template.

Generating PDFs from Page Patterns

To generate a PDF from page patterns, create a NewPage object with the NewPage.fromPattern() method that takes PagePattern as its parameter:

/// Custom tiled page PDF document.
File patternTilesDocument = File('<readable-tiled-document-path');

String outputPath = '<writable-file-path>';

List<NewPage> pages = [

    // PSPDFKit predefined patterns.
    NewPage.fromPattern(PagePattern.blank),
    NewPage.fromPattern(PagePattern.grid5mm),
    NewPage.fromPattern(PagePattern.line5mm),
    NewPage.fromPattern(PagePattern.dots5mm),

    // Page from a custom tiled page PDF document.
    NewPage.fromPattern(
        PagePattern.fromDocument(patternTilesDocument.uri, 0)),
];

// Generate PDF from page templates and save it at `[outputPath]`.
var filePath = await PspdfkitProcessor.instance.generatePdf(pages, outputPath);

Generating PDFs from Existing PDF Document Pages

PdfPage enables you to generate a PDF from existing PDF document pages. This is useful for generating a PDF by merging pages from different PDF document pages.

PdfPage is different from the PagePattern.fromDocument creation method of the PagePattern class. Unlike PagePattern, PdfPage expects a standard PDF document and a page index for the source document.

To generate a PDF from existing PDF document pages, create a NewPage object with the NewPage.fromPdfPage() method that takes PdfPage as its parameter:

// Source document file.
File sourceDocument = File('readable-output-path');

String outputPath = '<writable-output-path>';

List<NewPage> pages = [
    // New page from an existing document page.
    NewPage.fromPdfPage(
        PdfPage(
            sourceDocumentUri: sourceDocument.uri,
            pageIndex: 4,
        )
    )
];

var filePath = await PspdfkitProcessor.instance.generatePdf(pages, outputPath);

For more information on generating PDF files from HTML strings and URLs, see the PspdfkitProcessor documentation and the PDF Generation Example from the Catalog app.