flutter_html_to_pdf_plus

pub package

Flutter plugin for generating PDF files from HTML

Usage

From a raw HTML content

var targetPath = "/your/sample/path";
var targetFileName = "example_pdf_file"

var generatedPdfFile = await FlutterHtmlToPdf.convertFromHtmlContent(
  content: htmlContent, 
  configuration: PdfPrinterConfiguration(
    targetPath: targetPath, 
    targetFileName: targetFileName,
    margins: PdfPrinterMargins(top: 10, bottom: 10, left: 10, right: 10),
    orientation: PrintOrientation.Landscape,
    printSize: PrintSize.A4
  ),
);

From an HTML file

var file = File("/sample_path/example.html");
var generatedPdfFile = await FlutterHtmlToPdf.convertFromHtmlFile(
  htmlFile: file,
  configuration: PdfPrinterConfiguration(
    targetPath: targetPath, 
    targetFileName: targetFileName,
    margins: PdfPrinterMargins(top: 10, bottom: 10, left: 10, right: 10),
    orientation: PrintOrientation.Landscape,
    printSize: PrintSize.A4
  ),
);

From an HTML file path

var filePath = "/sample_path/example.html";
var generatedPdfFile = await FlutterHtmlToPdf.convertFromHtmlFilePath(
  htmlFilePath: filePath,
  configuration: PdfPrinterConfiguration(
    targetPath: targetPath, 
    targetFileName: targetFileName,
    margins: PdfPrinterMargins(top: 10, bottom: 10, left: 10, right: 10),
    orientation: PrintOrientation.Landscape,
    printSize: PrintSize.A4
  ),
);

/!\ Caveats : Customization of orientation is not supported on iOS. Any config will be ignored.

Images

If your want to add local image from device to your HTML you need to pass path to image as src value.

<img src="file:///storage/example/your_sample_image.png" alt="web-img">

or if you want to use the image File object

<img src="${imageFile.path}" alt="web-img">

Many images inside your document can significantly affect the final file size so we suggest to use flutter_image_compress plugin to compress images before generating PDF.

Contributing

If you want to contribute, please submit a pull request or create an issue.

Credits

  • Thanks to Afur for the initial work on this plugin
  • Thanks to raister21 for their work on PDF Size & Orientation
  • Thanks to wiseminds for the inspiration for margins customization