Generate a PDF from an image 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.
To convert an image to a PDF, follow these steps:
- Create a
PdfImagePageobject(opens in a new tab) to specify the image location and compression instructions. - Create a
NewPageobject(opens in a new tab) to specify the page size and thePdfImagePageobject(opens in a new tab) you created. - Call the
Nutrient.generatePdf()method with these parameters:- A list of
NewPageobjects(opens in a new tab). - An
outputFileparameter that specifies where Nutrient saves the generated PDF.
- A list of
Generate PDFs from images
The following example creates a PDF from an image:
// Image file from which to generate the PDF.File img = File('<readable-image-path>');
// File path where the generated PDF will be saved.String outputPath = '<writable-output-file-path>';
// A list of PDF pages from the image URI.List<NewPage> pages = [ NewPage.fromImage( PdfImagePage.fromUri(img.uri, PagePosition.center)), NewPage.fromImage( PdfImagePage.fromUri(img.uri, PagePosition.center)),];// Generate a PDF from an image and save it at `[outputPath]`.var filePath = await Nutrient.generatePdf( pages, outputPath); // Or `generatePDFFromImage`.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).