Alban Lecuivre

feat: enable usage of printer's settings

... ... @@ -65,6 +65,7 @@ abstract class PrintingPlatform extends PlatformInterface {
String name,
PdfPageFormat format,
bool dynamicLayout,
bool usePrinterSettings,
);
/// Enumerate the available printers on the system.
... ...
... ... @@ -171,6 +171,7 @@ class MethodChannelPrinting extends PrintingPlatform {
String name,
PdfPageFormat format,
bool dynamicLayout,
bool usePrinterSettings,
) async {
final job = _printJobs.add(
onCompleted: Completer<bool>(),
... ... @@ -188,6 +189,7 @@ class MethodChannelPrinting extends PrintingPlatform {
'marginRight': format.marginRight,
'marginBottom': format.marginBottom,
'dynamic': dynamicLayout,
'usePrinterSettings': usePrinterSettings,
};
await _channel.invokeMethod<int>('printPdf', params);
... ...
... ... @@ -35,11 +35,16 @@ mixin Printing {
/// 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
///
/// Set [usePrinterSettings] to true to use the configuration defined by
/// the printer. May not work for all the printers and can depend on the
/// drivers. (Supported platforms: Windows)
static Future<bool> layoutPdf({
required LayoutCallback onLayout,
String name = 'Document',
PdfPageFormat format = PdfPageFormat.standard,
bool dynamicLayout = true,
bool usePrinterSettings = false,
}) {
return PrintingPlatform.instance.layoutPdf(
null,
... ... @@ -47,6 +52,7 @@ mixin Printing {
name,
format,
dynamicLayout,
usePrinterSettings,
);
}
... ... @@ -118,12 +124,17 @@ mixin Printing {
///
/// This is not supported on all platforms. Check the result of [info] to
/// find at runtime if this feature is available or not.
///
/// Set [usePrinterSettings] to true to use the configuration defined by
/// the printer. May not work for all the printers and can depend on the
/// drivers. (Supported platforms: Windows)
static FutureOr<bool> directPrintPdf({
required Printer printer,
required LayoutCallback onLayout,
String name = 'Document',
PdfPageFormat format = PdfPageFormat.standard,
bool dynamicLayout = true,
bool usePrinterSettings = false,
}) {
return PrintingPlatform.instance.layoutPdf(
printer,
... ... @@ -131,6 +142,7 @@ mixin Printing {
name,
format,
dynamicLayout,
usePrinterSettings,
);
}
... ...
... ... @@ -76,23 +76,30 @@ PrintJob::PrintJob(Printing* printing, int index)
bool PrintJob::printPdf(std::string name,
std::string printer,
double width,
double height) {
double height,
bool usePrinterSettings) {
documentName = name;
auto dm = static_cast<DEVMODE*>(GlobalAlloc(0, sizeof(DEVMODE)));
ZeroMemory(dm, sizeof(DEVMODE));
dm->dmSize = sizeof(DEVMODE);
dm->dmFields = DM_ORIENTATION | DM_PAPERSIZE | DM_PAPERLENGTH | DM_PAPERWIDTH;
dm->dmPaperSize = 0;
if (width > height) {
dm->dmOrientation = DMORIENT_LANDSCAPE;
dm->dmPaperWidth = static_cast<short>(round(height * 254 / 72));
dm->dmPaperLength = static_cast<short>(round(width * 254 / 72));
auto dm;
if (usePrinterSettings){
dm = NULL; // to use default driver config
} else {
dm->dmOrientation = DMORIENT_PORTRAIT;
dm->dmPaperWidth = static_cast<short>(round(width * 254 / 72));
dm->dmPaperLength = static_cast<short>(round(height * 254 / 72));
dm = static_cast<DEVMODE*>(GlobalAlloc(0, sizeof(DEVMODE)));
ZeroMemory(dm, sizeof(DEVMODE));
dm->dmSize = sizeof(DEVMODE);
dm->dmFields = DM_ORIENTATION | DM_PAPERSIZE | DM_PAPERLENGTH | DM_PAPERWIDTH;
dm->dmPaperSize = 0;
if (width > height) {
dm->dmOrientation = DMORIENT_LANDSCAPE;
dm->dmPaperWidth = static_cast<short>(round(height * 254 / 72));
dm->dmPaperLength = static_cast<short>(round(width * 254 / 72));
} else {
dm->dmOrientation = DMORIENT_PORTRAIT;
dm->dmPaperWidth = static_cast<short>(round(width * 254 / 72));
dm->dmPaperLength = static_cast<short>(round(height * 254 / 72));
}
}
if (printer.empty()) {
PRINTDLG pd;
... ...
... ... @@ -74,7 +74,8 @@ class PrintJob {
bool printPdf(std::string name,
std::string printer,
double width,
double height);
double height,
bool usePrinterSettings);
void writeJob(std::vector<uint8_t> data);
... ...
... ... @@ -78,10 +78,12 @@ class PrintingPlugin : public flutter::Plugin {
arguments->find(flutter::EncodableValue("width"))->second);
auto height = std::get<double>(
arguments->find(flutter::EncodableValue("height"))->second);
auto usePrinterSettings = std::get<bool>(
arguments->find(flutter::EncodableValue("usePrinterSettings"))->second);
auto vJob = arguments->find(flutter::EncodableValue("job"));
auto jobNum = vJob != arguments->end() ? std::get<int>(vJob->second) : -1;
auto job = new PrintJob{&printing, jobNum};
auto res = job->printPdf(name, printer, width, height);
auto res = job->printPdf(name, printer, width, height, usePrinterSettings);
if (!res) {
delete job;
}
... ...