DavBfr / dart_pdf

Pdf creation module for dart/flutter
https://pub.dev/packages/pdf
Apache License 2.0
1.4k stars 625 forks source link

Callback for Print Success/Failure Notification #1649

Open samuelkchris opened 5 months ago

samuelkchris commented 5 months ago

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 🌟

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.


DavBfr commented 5 months ago

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;
}
alejantab commented 5 months ago

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.

dcaldr commented 5 months ago

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

cuishijie1991 commented 4 months ago

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

jsalazarf27 commented 3 months ago

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 ?