dart_pdf icon indicating copy to clipboard operation
dart_pdf copied to clipboard

Callback for Print Success/Failure Notification

Open samuelkchris opened this issue 10 months ago • 5 comments


Problem Statement 🚀

Developers using the Pdf for Dart and Flutter package currently lack a reliable method to confirm the success or failure of printing operations. This absence of feedback can lead to frustration when critical application actions depend on the outcome of print tasks.

Proposed Solution 💡

Integrate a callback mechanism into the Pdf for Dart and Flutter package that notifies developers of the print operation's success or failure. This callback could deliver a straightforward boolean value or an enum, such as PrintStatus.success or PrintStatus.failure, providing clear feedback on the print outcome.

Detailed Request Description 📝

The requested callback function would trigger upon the completion of the print task, communicating the status directly to the application. By implementing this feature, developers can seamlessly incorporate post-print actions based on the operation's outcome, enhancing the reliability and user experience of their applications.

Use Case Scenarios 🌟

  • Invoicing System: Upon successfully printing an invoice, update the invoice status and notify the user.
  • Report Generation: After printing a report, update a database record to reflect the print status.

Performance Considerations ⚡

Implement the callback function efficiently to minimize overhead, ensuring optimal resource management during print operations.

Compatibility and Integration 🔄

This feature will seamlessly integrate with Pdf for Dart and Flutter, compatible with the latest Flutter versions, providing a consistent experience for developers.

Community Impact 🤝

Enabling developers to handle print outcomes effectively will foster collaboration and innovation within the Pdf for Dart and Flutter community.

Security and Error Handling 🔒

Implement secure practices for handling print-related data and robust error recovery strategies using the callback function.

Proposed API Implementation 🛠️

import 'package:pdf/pdf.dart' as pw;
import 'package:printing/printing.dart';

final pdf = pw.Document();
// ... add content to the pdf ...

await Printing.layoutPdf(
  onLayout: (PdfPageFormat format) async {
    try {
      final result = await pdf.save();
      return result;
    } catch (e) {
      return null; // Handle error gracefully
    }
  },
  callback: (PrintJobStatus status) {
    if (status.completed) {
      // Handle successful print
      return PrintStatus.success;
    } else {
      // Handle print failure
      return PrintStatus.failure;
    }
  },
);

Long-term Benefits 🌱

This feature will empower developers to build more robust and user-friendly applications, enhancing productivity and user experience with Pdf for Dart and Flutter.


samuelkchris avatar Apr 22 '24 08:04 samuelkchris

The layoutPdf function's documentation is:

/// 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

so you would do:

import 'package:pdf/pdf.dart' as pw;
import 'package:printing/printing.dart';

final pdf = pw.Document();
// ... add content to the pdf ...

try {
final result = await Printing.layoutPdf(
  onLayout: (PdfPageFormat format) async {
    try {
      final result = await pdf.save();
      return result;
    } catch (e) {
      return null; // Handle error gracefully
    }
  });
    if (result) {
      // Handle successful print
      return PrintStatus.success;
    } else {
      // Handle print canceled
      return PrintStatus.canceled;
    }
} catch (e) {
// handle print error
      return PrintStatus.failure;
}

DavBfr avatar Apr 22 '24 09:04 DavBfr

The layoutPdf function's documentation is:

/// 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

so you would do:

import 'package:pdf/pdf.dart' as pw;
import 'package:printing/printing.dart';

final pdf = pw.Document();
// ... add content to the pdf ...

try {
final result = await Printing.layoutPdf(
  onLayout: (PdfPageFormat format) async {
    try {
      final result = await pdf.save();
      return result;
    } catch (e) {
      return null; // Handle error gracefully
    }
  });
    if (result) {
      // Handle successful print
      return PrintStatus.success;
    } else {
      // Handle print canceled
      return PrintStatus.canceled;
    }
} catch (e) {
// handle print error
      return PrintStatus.failure;
}

Hello.

This is working great on Android and iOS devices, but in Web platform It always return true, even without show the Preview Screen to print. So, with no users interaction to Print or Cancel I always get 'true' from the future.

alejantab avatar Apr 25 '24 13:04 alejantab

The layoutPdf function's documentation is:

/// 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

so you would do:

import 'package:pdf/pdf.dart' as pw;
import 'package:printing/printing.dart';

final pdf = pw.Document();
// ... add content to the pdf ...

try {
final result = await Printing.layoutPdf(
  onLayout: (PdfPageFormat format) async {
    try {
      final result = await pdf.save();
      return result;
    } catch (e) {
      return null; // Handle error gracefully
    }
  });
    if (result) {
      // Handle successful print
      return PrintStatus.success;
    } else {
      // Handle print canceled
      return PrintStatus.canceled;
    }
} catch (e) {
// handle print error
      return PrintStatus.failure;
}

Hello.

This is working great on Android and iOS devices, but in Web platform It always return true, even without show the Preview Screen to print. So, with no users interaction to Print or Cancel I always get 'true' from the future.

I think that always true is happening in windows as well. Even if you don't finish the dialog, it returns true

dcaldr avatar Apr 29 '24 10:04 dcaldr

Not work on desktop platform +1. That will be very great if it can be solved

cuishijie1991 avatar May 29 '24 06:05 cuishijie1991

Is there any plan to release an improvement for this bug, it would be great to know on the web if it could be printed or not ?

jsalazarf27 avatar Jun 17 '24 15:06 jsalazarf27