Generate a PDF from a template in Flutter
PDF generation is available only on the legacy method-channel API (package:nutrient_flutter/nutrient_flutter.dart), which is deprecated in Nutrient Flutter SDK 6. The APIs on this page aren’t part of the bindings API yet. They still work in SDK 6; import the legacy library to use them alongside bindings.dart if you use both. Nutrient plans to add a bindings equivalent. In the meantime, refer to the platform adapters guide to use native processors. For details, refer to the Flutter SDK 6 migration guide.
Use Nutrient Flutter SDK to create a PDF document from a template. The template can use a page pattern(opens in a new tab) or a page from an existing PDF document(opens in a new tab).
License
To generate a PDF from a template, contact Sales to add the PDF Generation component to your license.
Predefined page patterns
The following table lists the predefined page patterns(opens in a new tab).
| 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 |
Create custom tiled templates
You can also build custom page templates from your own PDF documents. Use the PagePattern.fromDocument method with a file URI.
Many page template use cases require a tiled or patterned background. A page is tiled when it repeats one or more images on the page.
A tiled page template requires a correctly exported source document. The source PDF must contain a pattern. If you instantiate PagePattern(opens in a new tab) with PagePattern.fromDocument and the source document doesn’t contain a pattern, rendering fails silently.
To use a PDF as the source for a tiled page template, embed the pattern path information. You can use Adobe Illustrator or another vector editing tool.
When you create your own patterns, consider these requirements:
- Embed the path information in the PDF rendered on the page, not the actual PDF.
- Include spacing information in the pattern information if your custom pattern requires spacing between tiles.
For testing, use the sample template.
Generate PDFs from page patterns
To generate a PDF from page patterns, create a NewPage object(opens in a new tab) with the NewPage.fromPattern() method. This method takes PagePattern(opens in a new tab) as its parameter:
/// Custom tiled page PDF document.File patternTilesDocument = File('<readable-tiled-document-path');
String outputPath = '<writable-file-path>';
List<NewPage> pages = [
// Nutrient 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 Nutrient.generatePdf(pages, outputPath);Generate PDFs from existing PDF document pages
Use PdfPage(opens in a new tab) to generate a PDF from existing PDF document pages. This is useful when you need to merge pages from different PDF documents into a new PDF.
PdfPage differs 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(opens in a new tab) with the NewPage.fromPdfPage() method. This method takes PdfPage(opens in a new tab) 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 Nutrient.generatePdf(pages, outputPath);For more information about generating PDF files from HTML strings and URLs, refer to the Nutrient(opens in a new tab) API reference and the PDF generation example(opens in a new tab) from the Catalog app(opens in a new tab).