Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Callback for Print Success/Failure Notification #1649

Open
samuelkchris opened this issue Apr 22, 2024 · 3 comments
Open

Callback for Print Success/Failure Notification #1649

samuelkchris opened this issue Apr 22, 2024 · 3 comments
Labels
enhancement New feature or request needs triage

Comments

@samuelkchris
Copy link


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 samuelkchris added enhancement New feature or request needs triage labels Apr 22, 2024
@DavBfr
Copy link
Owner

DavBfr commented Apr 22, 2024

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
Copy link

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
Copy link

dcaldr commented Apr 29, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request needs triage
Projects
None yet
Development

No branches or pull requests

4 participants