layoutPdf method

  1. @override
Future<bool> layoutPdf(
  1. Printer? printer,
  2. LayoutCallback onLayout,
  3. String name,
  4. PdfPageFormat format,
  5. bool dynamicLayout,
  6. bool usePrinterSettings,
  7. OutputType outputType,
  8. bool forceCustomPrintPaper,
)
override

Prints a Pdf document to a local printer using the platform UI the Pdf document is re-built in a LayoutCallback each time the user changes a setting like the page format or orientation.

returns a future with a bool set to true if the document is printed and false if it is canceled. throws an exception in case of error

Implementation

@override
Future<bool> layoutPdf(
  Printer? printer,
  LayoutCallback onLayout,
  String name,
  PdfPageFormat format,
  bool dynamicLayout,
  bool usePrinterSettings,
  OutputType outputType,
  bool forceCustomPrintPaper,
) async {
  final job = _printJobs.add(
    onCompleted: Completer<bool>(),
    onLayout: onLayout,
  );

  final params = <String, dynamic>{
    if (printer != null) 'printer': printer.url,
    'name': name,
    'job': job.index,
    'width': format.width,
    'height': format.height,
    'marginLeft': format.marginLeft,
    'marginTop': format.marginTop,
    'marginRight': format.marginRight,
    'marginBottom': format.marginBottom,
    'dynamic': dynamicLayout,
    'usePrinterSettings': usePrinterSettings,
    'outputType': outputType.index,
    'forceCustomPrintPaper': forceCustomPrintPaper,
  };

  await _channel.invokeMethod<int>('printPdf', params);
  try {
    return await job.onCompleted!.future;
  } finally {
    _printJobs.remove(job.index);
  }
}