Create PDFs from HTML 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.
The Nutrient(opens in a new tab) API creates PDF files from HTML strings and Uniform Resource Identifiers (URIs). On Android, Nutrient uses the WebView(opens in a new tab) system. On iOS, Nutrient uses WebKit(opens in a new tab). Because these platform systems perform the conversion, Nutrient can’t support every conversion issue, including WebView(opens in a new tab) or WebKit(opens in a new tab) bugs.
The following examples show how to generate a PDF from an HTML string and a URI or URL.
License
To generate a PDF from HTML, contact Sales to add HTML-to-PDF conversion to your license.
Generate a PDF from an HTML string
The following example generates a PDF from an HTML string:
const html = '''<html> <head> <style type="text/css"> h1 { color: red; } </style> </head> <body> <h1>Hello, world!</h1> </body></html>''';
// Writable output path.final outputFilePath = '<writable-output-file-path>'
// Generate the PDF from the HTML string.String? generatedPdf = await Nutrient.generatePdfFromHtmlString(html, outputFilePath);if (generatedPdf == null) { print('Failed to generate PDF from HTML string.'); return;}
// Display the generated PDF.await Nutrient.present(generatedPdf);Generate a PDF from an HTML URL or URI
The following example generates a PDF from an HTML URL or URI:
// Writable output path.final htmlUrl = Uri.parse('https://nutrient.io');final outputFilePath = <writable-output-path>;
// Generate the PDF from the HTML URL. String? generatedPdf = await Nutrient.generatePdfFromHtmlUri(htmlUri, outputFilePath);
if (generatedPdf == null) { print('Failed to generate PDF from HTML URI.'); return; }
// Display the generated PDF. await Nutrient.present(generatedPdf);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).