Alban Lecuivre

feat: enable usage of printer's settings

@@ -65,6 +65,7 @@ abstract class PrintingPlatform extends PlatformInterface { @@ -65,6 +65,7 @@ abstract class PrintingPlatform extends PlatformInterface {
65 String name, 65 String name,
66 PdfPageFormat format, 66 PdfPageFormat format,
67 bool dynamicLayout, 67 bool dynamicLayout,
  68 + bool usePrinterSettings,
68 ); 69 );
69 70
70 /// Enumerate the available printers on the system. 71 /// Enumerate the available printers on the system.
@@ -171,6 +171,7 @@ class MethodChannelPrinting extends PrintingPlatform { @@ -171,6 +171,7 @@ class MethodChannelPrinting extends PrintingPlatform {
171 String name, 171 String name,
172 PdfPageFormat format, 172 PdfPageFormat format,
173 bool dynamicLayout, 173 bool dynamicLayout,
  174 + bool usePrinterSettings,
174 ) async { 175 ) async {
175 final job = _printJobs.add( 176 final job = _printJobs.add(
176 onCompleted: Completer<bool>(), 177 onCompleted: Completer<bool>(),
@@ -188,6 +189,7 @@ class MethodChannelPrinting extends PrintingPlatform { @@ -188,6 +189,7 @@ class MethodChannelPrinting extends PrintingPlatform {
188 'marginRight': format.marginRight, 189 'marginRight': format.marginRight,
189 'marginBottom': format.marginBottom, 190 'marginBottom': format.marginBottom,
190 'dynamic': dynamicLayout, 191 'dynamic': dynamicLayout,
  192 + 'usePrinterSettings': usePrinterSettings,
191 }; 193 };
192 194
193 await _channel.invokeMethod<int>('printPdf', params); 195 await _channel.invokeMethod<int>('printPdf', params);
@@ -35,11 +35,16 @@ mixin Printing { @@ -35,11 +35,16 @@ mixin Printing {
35 /// returns a future with a `bool` set to true if the document is printed 35 /// returns a future with a `bool` set to true if the document is printed
36 /// and false if it is canceled. 36 /// and false if it is canceled.
37 /// throws an exception in case of error 37 /// throws an exception in case of error
  38 + ///
  39 + /// Set [usePrinterSettings] to true to use the configuration defined by
  40 + /// the printer. May not work for all the printers and can depend on the
  41 + /// drivers. (Supported platforms: Windows)
38 static Future<bool> layoutPdf({ 42 static Future<bool> layoutPdf({
39 required LayoutCallback onLayout, 43 required LayoutCallback onLayout,
40 String name = 'Document', 44 String name = 'Document',
41 PdfPageFormat format = PdfPageFormat.standard, 45 PdfPageFormat format = PdfPageFormat.standard,
42 bool dynamicLayout = true, 46 bool dynamicLayout = true,
  47 + bool usePrinterSettings = false,
43 }) { 48 }) {
44 return PrintingPlatform.instance.layoutPdf( 49 return PrintingPlatform.instance.layoutPdf(
45 null, 50 null,
@@ -47,6 +52,7 @@ mixin Printing { @@ -47,6 +52,7 @@ mixin Printing {
47 name, 52 name,
48 format, 53 format,
49 dynamicLayout, 54 dynamicLayout,
  55 + usePrinterSettings,
50 ); 56 );
51 } 57 }
52 58
@@ -118,12 +124,17 @@ mixin Printing { @@ -118,12 +124,17 @@ mixin Printing {
118 /// 124 ///
119 /// This is not supported on all platforms. Check the result of [info] to 125 /// This is not supported on all platforms. Check the result of [info] to
120 /// find at runtime if this feature is available or not. 126 /// find at runtime if this feature is available or not.
  127 + ///
  128 + /// Set [usePrinterSettings] to true to use the configuration defined by
  129 + /// the printer. May not work for all the printers and can depend on the
  130 + /// drivers. (Supported platforms: Windows)
121 static FutureOr<bool> directPrintPdf({ 131 static FutureOr<bool> directPrintPdf({
122 required Printer printer, 132 required Printer printer,
123 required LayoutCallback onLayout, 133 required LayoutCallback onLayout,
124 String name = 'Document', 134 String name = 'Document',
125 PdfPageFormat format = PdfPageFormat.standard, 135 PdfPageFormat format = PdfPageFormat.standard,
126 bool dynamicLayout = true, 136 bool dynamicLayout = true,
  137 + bool usePrinterSettings = false,
127 }) { 138 }) {
128 return PrintingPlatform.instance.layoutPdf( 139 return PrintingPlatform.instance.layoutPdf(
129 printer, 140 printer,
@@ -131,6 +142,7 @@ mixin Printing { @@ -131,6 +142,7 @@ mixin Printing {
131 name, 142 name,
132 format, 143 format,
133 dynamicLayout, 144 dynamicLayout,
  145 + usePrinterSettings,
134 ); 146 );
135 } 147 }
136 148
@@ -76,23 +76,30 @@ PrintJob::PrintJob(Printing* printing, int index) @@ -76,23 +76,30 @@ PrintJob::PrintJob(Printing* printing, int index)
76 bool PrintJob::printPdf(std::string name, 76 bool PrintJob::printPdf(std::string name,
77 std::string printer, 77 std::string printer,
78 double width, 78 double width,
79 - double height) { 79 + double height,
  80 + bool usePrinterSettings) {
80 documentName = name; 81 documentName = name;
81 82
82 - auto dm = static_cast<DEVMODE*>(GlobalAlloc(0, sizeof(DEVMODE)));  
83 - ZeroMemory(dm, sizeof(DEVMODE));  
84 - dm->dmSize = sizeof(DEVMODE);  
85 - dm->dmFields = DM_ORIENTATION | DM_PAPERSIZE | DM_PAPERLENGTH | DM_PAPERWIDTH;  
86 - dm->dmPaperSize = 0;  
87 - if (width > height) {  
88 - dm->dmOrientation = DMORIENT_LANDSCAPE;  
89 - dm->dmPaperWidth = static_cast<short>(round(height * 254 / 72));  
90 - dm->dmPaperLength = static_cast<short>(round(width * 254 / 72)); 83 + auto dm;
  84 + if (usePrinterSettings){
  85 + dm = NULL; // to use default driver config
91 } else { 86 } else {
92 - dm->dmOrientation = DMORIENT_PORTRAIT;  
93 - dm->dmPaperWidth = static_cast<short>(round(width * 254 / 72));  
94 - dm->dmPaperLength = static_cast<short>(round(height * 254 / 72)); 87 + dm = static_cast<DEVMODE*>(GlobalAlloc(0, sizeof(DEVMODE)));
  88 + ZeroMemory(dm, sizeof(DEVMODE));
  89 + dm->dmSize = sizeof(DEVMODE);
  90 + dm->dmFields = DM_ORIENTATION | DM_PAPERSIZE | DM_PAPERLENGTH | DM_PAPERWIDTH;
  91 + dm->dmPaperSize = 0;
  92 + if (width > height) {
  93 + dm->dmOrientation = DMORIENT_LANDSCAPE;
  94 + dm->dmPaperWidth = static_cast<short>(round(height * 254 / 72));
  95 + dm->dmPaperLength = static_cast<short>(round(width * 254 / 72));
  96 + } else {
  97 + dm->dmOrientation = DMORIENT_PORTRAIT;
  98 + dm->dmPaperWidth = static_cast<short>(round(width * 254 / 72));
  99 + dm->dmPaperLength = static_cast<short>(round(height * 254 / 72));
  100 + }
95 } 101 }
  102 +
96 103
97 if (printer.empty()) { 104 if (printer.empty()) {
98 PRINTDLG pd; 105 PRINTDLG pd;
@@ -74,7 +74,8 @@ class PrintJob { @@ -74,7 +74,8 @@ class PrintJob {
74 bool printPdf(std::string name, 74 bool printPdf(std::string name,
75 std::string printer, 75 std::string printer,
76 double width, 76 double width,
77 - double height); 77 + double height,
  78 + bool usePrinterSettings);
78 79
79 void writeJob(std::vector<uint8_t> data); 80 void writeJob(std::vector<uint8_t> data);
80 81
@@ -78,10 +78,12 @@ class PrintingPlugin : public flutter::Plugin { @@ -78,10 +78,12 @@ class PrintingPlugin : public flutter::Plugin {
78 arguments->find(flutter::EncodableValue("width"))->second); 78 arguments->find(flutter::EncodableValue("width"))->second);
79 auto height = std::get<double>( 79 auto height = std::get<double>(
80 arguments->find(flutter::EncodableValue("height"))->second); 80 arguments->find(flutter::EncodableValue("height"))->second);
  81 + auto usePrinterSettings = std::get<bool>(
  82 + arguments->find(flutter::EncodableValue("usePrinterSettings"))->second);
81 auto vJob = arguments->find(flutter::EncodableValue("job")); 83 auto vJob = arguments->find(flutter::EncodableValue("job"));
82 auto jobNum = vJob != arguments->end() ? std::get<int>(vJob->second) : -1; 84 auto jobNum = vJob != arguments->end() ? std::get<int>(vJob->second) : -1;
83 auto job = new PrintJob{&printing, jobNum}; 85 auto job = new PrintJob{&printing, jobNum};
84 - auto res = job->printPdf(name, printer, width, height); 86 + auto res = job->printPdf(name, printer, width, height, usePrinterSettings);
85 if (!res) { 87 if (!res) {
86 delete job; 88 delete job;
87 } 89 }