David PHAM-VAN

Review directPrint internals

... ... @@ -6,6 +6,7 @@
- Add Page orientation on PdfPreview
- Improve PrintJob object
- Implement dynamic layout on iOS and macOS
- Review directPrint internals
## 5.0.0-nullsafety.1
... ...
... ... @@ -87,32 +87,7 @@ public class PrintJob: UIPrintPageRenderer, UIPrintInteractionControllerDelegate
printing.onCompleted(printJob: self, completed: completed, error: error?.localizedDescription as NSString?)
}
func directPrintPdf(name: String, data: Data, withPrinter printerID: String) {
let printing = UIPrintInteractionController.isPrintingAvailable
if !printing {
self.printing.onCompleted(printJob: self, completed: false, error: "Printing not available")
return
}
let controller = UIPrintInteractionController.shared
let printInfo = UIPrintInfo.printInfo()
printInfo.jobName = name
printInfo.outputType = .general
controller.printInfo = printInfo
controller.printingItem = data
let printerURL = URL(string: printerID)
if printerURL == nil {
self.printing.onCompleted(printJob: self, completed: false, error: "Unable to find printer URL")
return
}
let printer = UIPrinter(url: printerURL!)
controller.print(to: printer, completionHandler: completionHandler)
}
func printPdf(name: String, withPageSize size: CGSize, andMargin _: CGRect) {
func printPdf(name: String, withPageSize size: CGSize, andMargin _: CGRect, withPrinter printerID: String?) {
let printing = UIPrintInteractionController.isPrintingAvailable
if !printing {
self.printing.onCompleted(printJob: self, completed: false, error: "Printing not available")
... ... @@ -139,6 +114,20 @@ public class PrintJob: UIPrintPageRenderer, UIPrintInteractionControllerDelegate
controller.showsPaperSelectionForLoadedPapers = true
controller.printPageRenderer = self
if printerID != nil {
let printerURL = URL(string: printerID!)
if printerURL == nil {
self.printing.onCompleted(printJob: self, completed: false, error: "Unable to find printer URL")
return
}
let printer = UIPrinter(url: printerURL!)
controller.print(to: printer, completionHandler: completionHandler)
return
}
controller.present(animated: true, completionHandler: completionHandler)
}
... ...
... ... @@ -54,12 +54,13 @@ public class PrintingPlugin: NSObject, FlutterPlugin {
let args = call.arguments! as! [String: Any]
if call.method == "printPdf" {
let name = args["name"] as! String
let width = CGFloat((args["width"] as? NSNumber)?.floatValue ?? 0.0)
let height = CGFloat((args["height"] as? NSNumber)?.floatValue ?? 0.0)
let marginLeft = CGFloat((args["marginLeft"] as? NSNumber)?.floatValue ?? 0.0)
let marginTop = CGFloat((args["marginTop"] as? NSNumber)?.floatValue ?? 0.0)
let marginRight = CGFloat((args["marginRight"] as? NSNumber)?.floatValue ?? 0.0)
let marginBottom = CGFloat((args["marginBottom"] as? NSNumber)?.floatValue ?? 0.0)
let printer = args["printer"] as? String
let width = CGFloat((args["width"] as! NSNumber).floatValue)
let height = CGFloat((args["height"] as! NSNumber).floatValue)
let marginLeft = CGFloat((args["marginLeft"] as! NSNumber).floatValue)
let marginTop = CGFloat((args["marginTop"] as! NSNumber).floatValue)
let marginRight = CGFloat((args["marginRight"] as! NSNumber).floatValue)
let marginBottom = CGFloat((args["marginBottom"] as! NSNumber).floatValue)
let printJob = PrintJob(printing: self, index: args["job"] as! Int)
jobs[args["job"] as! UInt32] = printJob
printJob.printPdf(name: name,
... ... @@ -72,14 +73,7 @@ public class PrintingPlugin: NSObject, FlutterPlugin {
y: marginTop,
width: width - marginRight - marginLeft,
height: height - marginBottom - marginTop
))
result(NSNumber(value: 1))
} else if call.method == "directPrintPdf" {
let name = args["name"] as! String
let printer = args["printer"] as! String
let object = args["doc"] as! FlutterStandardTypedData
let printJob = PrintJob(printing: self, index: args["job"] as! Int)
printJob.directPrintPdf(name: name, data: object.data, withPrinter: printer)
), withPrinter: printer)
result(NSNumber(value: 1))
} else if call.method == "sharePdf" {
let object = args["doc"] as! FlutterStandardTypedData
... ...
... ... @@ -207,14 +207,12 @@ class MethodChannelPrinting extends PrintingPlatform {
) async {
final job = _printJobs.add(
onCompleted: Completer<bool>(),
onLayout: onLayout,
);
final bytes = await onLayout(format);
final params = <String, dynamic>{
'name': name,
'printer': printer.url,
'doc': bytes,
'width': format.width,
'height': format.height,
'marginLeft': format.marginLeft,
... ... @@ -223,10 +221,12 @@ class MethodChannelPrinting extends PrintingPlatform {
'marginBottom': format.marginBottom,
'job': job.index,
};
await _channel.invokeMethod<int>('directPrintPdf', params);
final result = await job.onCompleted!.future;
_printJobs.remove(job.index);
return result;
await _channel.invokeMethod<int>('printPdf', params);
try {
return await job.onCompleted!.future;
} finally {
_printJobs.remove(job.index);
}
}
@override
... ...
... ... @@ -166,7 +166,7 @@ class PrintingPlugin extends PrintingPlatform {
String name,
PdfPageFormat format,
) {
throw UnimplementedError();
return layoutPdf(onLayout, name, format);
}
@override
... ...
... ... @@ -109,64 +109,85 @@ static void job_completed(GtkPrintJob* gtk_print_job,
}
bool print_job::print_pdf(const gchar* name,
const gchar* printer,
double pageWidth,
double pageHeight,
double marginLeft,
double marginTop,
double marginRight,
double marginBottom) {
documentName = strdup(name);
auto dialog =
GTK_PRINT_UNIX_DIALOG(gtk_print_unix_dialog_new(documentName, nullptr));
gtk_print_unix_dialog_set_manual_capabilities(
dialog, (GtkPrintCapabilities)(GTK_PRINT_CAPABILITY_GENERATE_PDF));
gtk_widget_realize(GTK_WIDGET(dialog));
auto response = gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_hide(GTK_WIDGET(dialog));
switch (response) {
case GTK_RESPONSE_OK: {
GtkPrinter* printer = gtk_print_unix_dialog_get_selected_printer(
GTK_PRINT_UNIX_DIALOG(dialog));
if (!gtk_printer_accepts_pdf(printer)) {
on_completed(this, false, "This printer does not accept PDF jobs");
break;
}
GtkPrintSettings* settings;
GtkPageSetup* setup;
if (printer != nullptr) {
_printer = nullptr;
auto pname = strdup(printer);
gtk_enumerate_printers(search_printer, pname, nullptr, true);
free(pname);
if (!_printer) {
on_completed(this, false, "Printer not found");
return false;
}
GtkPrintSettings* settings =
gtk_print_unix_dialog_get_settings(GTK_PRINT_UNIX_DIALOG(dialog));
GtkPageSetup* setup =
gtk_print_unix_dialog_get_page_setup(GTK_PRINT_UNIX_DIALOG(dialog));
settings = gtk_print_settings_new();
setup = gtk_page_setup_new();
} else {
auto dialog =
GTK_PRINT_UNIX_DIALOG(gtk_print_unix_dialog_new(name, nullptr));
gtk_print_unix_dialog_set_manual_capabilities(
dialog, (GtkPrintCapabilities)(GTK_PRINT_CAPABILITY_GENERATE_PDF));
gtk_widget_realize(GTK_WIDGET(dialog));
auto response = gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_hide(GTK_WIDGET(dialog));
switch (response) {
case GTK_RESPONSE_OK: {
_printer = gtk_print_unix_dialog_get_selected_printer(
GTK_PRINT_UNIX_DIALOG(dialog));
settings =
gtk_print_unix_dialog_get_settings(GTK_PRINT_UNIX_DIALOG(dialog));
setup =
gtk_print_unix_dialog_get_page_setup(GTK_PRINT_UNIX_DIALOG(dialog));
gtk_widget_destroy(GTK_WIDGET(dialog));
} break;
case GTK_RESPONSE_DELETE_EVENT: // Fall through.
case GTK_RESPONSE_CANCEL: // Cancel
case GTK_RESPONSE_APPLY: // Preview
default:
gtk_widget_destroy(GTK_WIDGET(dialog));
on_completed(this, false, nullptr);
return true;
}
}
auto width = gtk_page_setup_get_page_width(setup, GTK_UNIT_POINTS);
auto height = gtk_page_setup_get_page_height(setup, GTK_UNIT_POINTS);
auto marginLeft = gtk_page_setup_get_left_margin(setup, GTK_UNIT_POINTS);
auto marginTop = gtk_page_setup_get_top_margin(setup, GTK_UNIT_POINTS);
auto marginRight =
gtk_page_setup_get_right_margin(setup, GTK_UNIT_POINTS);
auto marginBottom =
gtk_page_setup_get_bottom_margin(setup, GTK_UNIT_POINTS);
if (!gtk_printer_accepts_pdf(_printer)) {
on_completed(this, false, "This printer does not accept PDF jobs");
g_object_unref(_printer);
g_object_unref(settings);
g_object_unref(setup);
return false;
}
printJob = gtk_print_job_new(name, printer, settings, setup);
auto _width = gtk_page_setup_get_page_width(setup, GTK_UNIT_POINTS);
auto _height = gtk_page_setup_get_page_height(setup, GTK_UNIT_POINTS);
auto _marginLeft = gtk_page_setup_get_left_margin(setup, GTK_UNIT_POINTS);
auto _marginTop = gtk_page_setup_get_top_margin(setup, GTK_UNIT_POINTS);
auto _marginRight = gtk_page_setup_get_right_margin(setup, GTK_UNIT_POINTS);
auto _marginBottom = gtk_page_setup_get_bottom_margin(setup, GTK_UNIT_POINTS);
on_layout(this, width, height, marginLeft, marginTop, marginRight,
marginBottom);
printJob = gtk_print_job_new(name, _printer, settings, setup);
g_object_unref(settings);
gtk_widget_destroy(GTK_WIDGET(dialog));
on_layout(this, _width, _height, _marginLeft, _marginTop, _marginRight,
_marginBottom);
return true;
}
case GTK_RESPONSE_DELETE_EVENT: // Fall through.
case GTK_RESPONSE_CANCEL: // Cancel
case GTK_RESPONSE_APPLY: // Preview
break;
}
g_object_unref(_printer);
g_object_unref(settings);
g_object_unref(setup);
gtk_widget_destroy(GTK_WIDGET(dialog));
on_completed(this, false, nullptr);
return true;
}
... ...
... ... @@ -24,7 +24,6 @@
class print_job {
private:
const int index;
char* documentName = nullptr;
GtkPrintJob* printJob;
public:
... ... @@ -42,6 +41,7 @@ class print_job {
const gchar* printer);
bool print_pdf(const gchar* name,
const gchar* printer,
double pageWidth,
double pageHeight,
double marginLeft,
... ...
... ... @@ -53,6 +53,9 @@ static void printing_plugin_handle_method_call(PrintingPlugin* self,
} else if (strcmp(method, "printPdf") == 0) {
auto name = fl_value_get_string(fl_value_lookup_string(args, "name"));
auto printerValue = fl_value_lookup_string(args, "printer");
auto printer =
printerValue == nullptr ? nullptr : fl_value_get_string(printerValue);
auto jobNum = fl_value_get_int(fl_value_lookup_string(args, "job"));
auto pageWidth = fl_value_get_float(fl_value_lookup_string(args, "width"));
auto pageHeight =
... ... @@ -67,7 +70,7 @@ static void printing_plugin_handle_method_call(PrintingPlugin* self,
fl_value_get_float(fl_value_lookup_string(args, "marginBottom"));
auto job = new print_job(jobNum);
auto res = job->print_pdf(name, pageWidth, pageHeight, marginLeft,
auto res = job->print_pdf(name, printer, pageWidth, pageHeight, marginLeft,
marginTop, marginRight, marginBottom);
if (!res) {
delete job;
... ... @@ -75,18 +78,6 @@ static void printing_plugin_handle_method_call(PrintingPlugin* self,
g_autoptr(FlValue) result = fl_value_new_int(res);
response = FL_METHOD_RESPONSE(fl_method_success_response_new(result));
} else if (strcmp(method, "directPrintPdf") == 0) {
auto jobNum = fl_value_get_int(fl_value_lookup_string(args, "job"));
auto name = fl_value_get_string(fl_value_lookup_string(args, "name"));
auto printer = fl_value_get_string(fl_value_lookup_string(args, "printer"));
auto doc = fl_value_get_uint8_list(fl_value_lookup_string(args, "doc"));
auto size = fl_value_get_length(fl_value_lookup_string(args, "doc"));
auto job = std::make_unique<print_job>(jobNum);
auto res = job->direct_print_pdf(name, doc, size, printer);
g_autoptr(FlValue) result = fl_value_new_int(res);
response = FL_METHOD_RESPONSE(fl_method_success_response_new(result));
} else if (strcmp(method, "sharePdf") == 0) {
auto name = fl_value_get_string(fl_value_lookup_string(args, "name"));
auto doc = fl_value_get_uint8_list(fl_value_lookup_string(args, "doc"));
... ... @@ -234,4 +225,4 @@ void on_completed(print_job* job, bool completed, const char* error) {
fl_method_channel_invoke_method(channel, "onCompleted", map, nullptr, nullptr,
nullptr);
}
\ No newline at end of file
}
... ...
... ... @@ -119,33 +119,7 @@ public class PrintJob: NSView, NSSharingServicePickerDelegate {
return printers
}
public func directPrintPdf(name: String, data: Data, withPrinter printer: String, width: CGFloat, height: CGFloat) {
let sharedInfo = NSPrintInfo.shared
let sharedDict = sharedInfo.dictionary()
let printInfoDict = NSMutableDictionary(dictionary: sharedDict)
let printInfo = NSPrintInfo(dictionary: printInfoDict as! [NSPrintInfo.AttributeKey: Any])
printInfo.printer = NSPrinter(name: printer)!
let bytesPointer = UnsafeMutablePointer<UInt8>.allocate(capacity: data.count)
data.copyBytes(to: bytesPointer, count: data.count)
let dataProvider = CGDataProvider(dataInfo: nil, data: bytesPointer, size: data.count, releaseData: dataProviderReleaseDataCallback)
pdfDocument = CGPDFDocument(dataProvider!)
printInfo.paperSize = NSSize(width: width, height: height)
if width > height {
printInfo.orientation = NSPrintInfo.PaperOrientation.landscape
}
// Print the custom view
printOperation = NSPrintOperation(view: self, printInfo: printInfo)
printOperation!.jobTitle = name
printOperation!.showsPrintPanel = false
printOperation!.showsProgressPanel = false
let window = NSApplication.shared.mainWindow!
printOperation!.runModal(for: window, delegate: self, didRun: #selector(printOperationDidRun(printOperation:success:contextInfo:)), contextInfo: nil)
}
public func printPdf(name: String, withPageSize size: CGSize, andMargin _: CGRect) {
public func printPdf(name: String, withPageSize size: CGSize, andMargin _: CGRect, withPrinter printer: String?) {
let sharedInfo = NSPrintInfo.shared
let sharedDict = sharedInfo.dictionary()
let printInfoDict = NSMutableDictionary(dictionary: sharedDict)
... ... @@ -160,6 +134,11 @@ public class PrintJob: NSView, NSSharingServicePickerDelegate {
printOperation = NSPrintOperation(view: self, printInfo: printInfo)
printOperation!.jobTitle = name
printOperation!.printPanel.options = [.showsPreview, .showsPaperSize, .showsOrientation]
if printer != nil {
printInfo.printer = NSPrinter(name: printer!)!
printOperation!.showsPrintPanel = false
printOperation!.showsProgressPanel = false
}
let window = NSApplication.shared.mainWindow!
printOperation!.runModal(for: window, delegate: self, didRun: #selector(printOperationDidRun(printOperation:success:contextInfo:)), contextInfo: nil)
... ...
... ... @@ -54,12 +54,13 @@ public class PrintingPlugin: NSObject, FlutterPlugin {
let args = call.arguments! as! [String: Any]
if call.method == "printPdf" {
let name = args["name"] as! String
let width = CGFloat((args["width"] as? NSNumber)?.floatValue ?? 0.0)
let height = CGFloat((args["height"] as? NSNumber)?.floatValue ?? 0.0)
let marginLeft = CGFloat((args["marginLeft"] as? NSNumber)?.floatValue ?? 0.0)
let marginTop = CGFloat((args["marginTop"] as? NSNumber)?.floatValue ?? 0.0)
let marginRight = CGFloat((args["marginRight"] as? NSNumber)?.floatValue ?? 0.0)
let marginBottom = CGFloat((args["marginBottom"] as? NSNumber)?.floatValue ?? 0.0)
let printer = args["printer"] as? String
let width = CGFloat((args["width"] as! NSNumber).floatValue)
let height = CGFloat((args["height"] as! NSNumber).floatValue)
let marginLeft = CGFloat((args["marginLeft"] as! NSNumber).floatValue)
let marginTop = CGFloat((args["marginTop"] as! NSNumber).floatValue)
let marginRight = CGFloat((args["marginRight"] as! NSNumber).floatValue)
let marginBottom = CGFloat((args["marginBottom"] as! NSNumber).floatValue)
let printJob = PrintJob(printing: self, index: args["job"] as! Int)
jobs[args["job"] as! UInt32] = printJob
printJob.printPdf(name: name,
... ... @@ -72,21 +73,12 @@ public class PrintingPlugin: NSObject, FlutterPlugin {
y: marginTop,
width: width - marginRight - marginLeft,
height: height - marginBottom - marginTop
))
), withPrinter: printer)
result(NSNumber(value: 1))
} else if call.method == "listPrinters" {
let printJob = PrintJob(printing: self, index: -1)
let r = printJob.listPrinters()
result(r)
} else if call.method == "directPrintPdf" {
let name = args["name"] as! String
let printer = args["printer"] as! String
let object = args["doc"] as! FlutterStandardTypedData
let width = CGFloat((args["width"] as? NSNumber)?.floatValue ?? 0.0)
let height = CGFloat((args["height"] as? NSNumber)?.floatValue ?? 0.0)
let printJob = PrintJob(printing: self, index: args["job"] as! Int)
printJob.directPrintPdf(name: name, data: object.data, withPrinter: printer, width: width, height: height)
result(NSNumber(1))
} else if call.method == "sharePdf" {
let object = args["doc"] as! FlutterStandardTypedData
PrintJob.sharePdf(
... ...
... ... @@ -73,119 +73,70 @@ std::wstring fromUtf8(std::string str) {
PrintJob::PrintJob(Printing* printing, int index)
: printing(printing), index(index) {}
bool PrintJob::directPrintPdf(std::string name,
std::vector<uint8_t> data,
std::string withPrinter) {
FPDF_InitLibraryWithConfig(nullptr);
auto doc = FPDF_LoadMemDocument64(data.data(), data.size(), nullptr);
if (!doc) {
FPDF_DestroyLibrary();
return false;
}
hDC = CreateDC(TEXT("WINSPOOL"), fromUtf8(withPrinter).c_str(), NULL, NULL);
if (!hDC) {
FPDF_CloseDocument(doc);
FPDF_DestroyLibrary();
return false;
}
DOCINFO docInfo;
ZeroMemory(&docInfo, sizeof(docInfo));
docInfo.cbSize = sizeof(DOCINFO);
auto docName = fromUtf8(name);
docInfo.lpszDocName = docName.c_str();
StartDoc(hDC, &docInfo);
int pageCount = FPDF_GetPageCount(doc);
for (auto i = 0; i < pageCount; i++) {
StartPage(hDC);
auto page = FPDF_LoadPage(doc, i);
if (!page) {
EndDoc(hDC);
DeleteDC(hDC);
FPDF_CloseDocument(doc);
FPDF_DestroyLibrary();
bool PrintJob::printPdf(std::string name, std::string printer) {
documentName = name;
if (printer.empty()) {
PRINTDLG pd;
DEVMODE* dm = static_cast<DEVMODE*>(GlobalAlloc(0, sizeof(DEVMODE)));
ZeroMemory(dm, sizeof(DEVMODE));
dm->dmFields = DM_ORIENTATION;
dm->dmOrientation = 2;
// Initialize PRINTDLG
ZeroMemory(&pd, sizeof(pd));
pd.lStructSize = sizeof(pd);
// Initialize PRINTDLG
pd.hwndOwner = nullptr;
pd.hDevMode = dm;
pd.hDevNames = nullptr; // Don't forget to free or store hDevNames.
pd.hDC = nullptr;
pd.Flags = PD_USEDEVMODECOPIES | PD_RETURNDC | PD_PRINTSETUP |
PD_NOSELECTION | PD_NOPAGENUMS;
pd.nCopies = 1;
pd.nFromPage = 0xFFFF;
pd.nToPage = 0xFFFF;
pd.nMinPage = 1;
pd.nMaxPage = 0xFFFF;
auto r = PrintDlg(&pd);
if (r == 1) {
hDC = pd.hDC;
hDevMode = pd.hDevMode;
hDevNames = pd.hDevNames;
}
} else {
hDC = CreateDC(TEXT("WINSPOOL"), fromUtf8(printer).c_str(), NULL, NULL);
if (!hDC) {
return false;
}
auto pageWidth = FPDF_GetPageWidth(page);
auto pageHeight = FPDF_GetPageHeight(page);
auto dpiX = GetDeviceCaps(hDC, LOGPIXELSX);
auto dpiY = GetDeviceCaps(hDC, LOGPIXELSY);
auto width = static_cast<int>(pageWidth / pdfDpi * dpiX);
auto height = static_cast<int>(pageHeight / pdfDpi * dpiY);
FPDF_RenderPage(hDC, page, 0, 0, width, height, 0, FPDF_ANNOT);
FPDF_ClosePage(page);
EndPage(hDC);
hDevMode = nullptr;
hDevNames = nullptr;
}
EndDoc(hDC);
DeleteDC(hDC);
FPDF_CloseDocument(doc);
FPDF_DestroyLibrary();
auto dpiX = static_cast<double>(GetDeviceCaps(hDC, LOGPIXELSX)) / pdfDpi;
auto dpiY = static_cast<double>(GetDeviceCaps(hDC, LOGPIXELSY)) / pdfDpi;
auto pageWidth =
static_cast<double>(GetDeviceCaps(hDC, PHYSICALWIDTH)) / dpiX;
auto pageHeight =
static_cast<double>(GetDeviceCaps(hDC, PHYSICALHEIGHT)) / dpiY;
auto printableWidth = static_cast<double>(GetDeviceCaps(hDC, HORZRES)) / dpiX;
auto printableHeight =
static_cast<double>(GetDeviceCaps(hDC, VERTRES)) / dpiY;
auto marginLeft =
static_cast<double>(GetDeviceCaps(hDC, PHYSICALOFFSETX)) / dpiX;
auto marginTop =
static_cast<double>(GetDeviceCaps(hDC, PHYSICALOFFSETY)) / dpiY;
auto marginRight = pageWidth - printableWidth - marginLeft;
auto marginBottom = pageHeight - printableHeight - marginTop;
printing->onLayout(this, pageWidth, pageHeight, marginLeft, marginTop,
marginRight, marginBottom);
return true;
}
bool PrintJob::printPdf(std::string name) {
PRINTDLG pd;
DEVMODE* dm = static_cast<DEVMODE*>(GlobalAlloc(0, sizeof(DEVMODE)));
ZeroMemory(dm, sizeof(DEVMODE));
dm->dmFields = DM_ORIENTATION;
dm->dmOrientation = 2;
printf("Printing ... \n");
// Initialize PRINTDLG
ZeroMemory(&pd, sizeof(pd));
pd.lStructSize = sizeof(pd);
// Initialize PRINTDLG
pd.hwndOwner = nullptr;
pd.hDevMode = dm;
pd.hDevNames = nullptr; // Don't forget to free or store hDevNames.
pd.hDC = nullptr;
pd.Flags = PD_USEDEVMODECOPIES | PD_RETURNDC | PD_PRINTSETUP |
PD_NOSELECTION | PD_NOPAGENUMS;
pd.nCopies = 1;
pd.nFromPage = 0xFFFF;
pd.nToPage = 0xFFFF;
pd.nMinPage = 1;
pd.nMaxPage = 0xFFFF;
auto r = PrintDlg(&pd);
if (r == 1) {
auto dpiX = static_cast<double>(GetDeviceCaps(pd.hDC, LOGPIXELSX)) / pdfDpi;
auto dpiY = static_cast<double>(GetDeviceCaps(pd.hDC, LOGPIXELSY)) / pdfDpi;
auto pageWidth =
static_cast<double>(GetDeviceCaps(pd.hDC, PHYSICALWIDTH)) / dpiX;
auto pageHeight =
static_cast<double>(GetDeviceCaps(pd.hDC, PHYSICALHEIGHT)) / dpiY;
auto printableWidth =
static_cast<double>(GetDeviceCaps(pd.hDC, HORZRES)) / dpiX;
auto printableHeight =
static_cast<double>(GetDeviceCaps(pd.hDC, VERTRES)) / dpiY;
auto marginLeft =
static_cast<double>(GetDeviceCaps(pd.hDC, PHYSICALOFFSETX)) / dpiX;
auto marginTop =
static_cast<double>(GetDeviceCaps(pd.hDC, PHYSICALOFFSETY)) / dpiY;
auto marginRight = pageWidth - printableWidth - marginLeft;
auto marginBottom = pageHeight - printableHeight - marginTop;
hDC = pd.hDC;
hDevMode = pd.hDevMode;
hDevNames = pd.hDevNames;
documentName = name;
printing->onLayout(this, pageWidth, pageHeight, marginLeft, marginTop,
marginRight, marginBottom);
return true;
}
return false;
}
std::vector<Printer> PrintJob::listPrinters() {
LPTSTR defaultPrinter;
DWORD size = 0;
... ...
... ... @@ -71,11 +71,7 @@ class PrintJob {
std::vector<Printer> listPrinters();
bool directPrintPdf(std::string name,
std::vector<uint8_t> data,
std::string printer);
bool printPdf(std::string name);
bool printPdf(std::string name, std::string printer);
void writeJob(std::vector<uint8_t> data);
... ...
... ... @@ -70,35 +70,18 @@ class PrintingPlugin : public flutter::Plugin {
auto name = vName != arguments->end() && !vName->second.IsNull()
? std::get<std::string>(vName->second)
: std::string{"document"};
auto vPrinter = arguments->find(flutter::EncodableValue("printer"));
auto printer = vPrinter != arguments->end()
? std::get<std::string>(vPrinter->second)
: std::string{};
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);
auto res = job->printPdf(name, printer);
if (!res) {
delete job;
}
result->Success(flutter::EncodableValue(res ? 1 : 0));
} else if (method_call.method_name().compare("directPrintPdf") == 0) {
const auto* arguments =
std::get_if<flutter::EncodableMap>(method_call.arguments());
auto vName = arguments->find(flutter::EncodableValue("name"));
auto name = vName != arguments->end() && !vName->second.IsNull()
? std::get<std::string>(vName->second)
: std::string{"document"};
auto vDoc = arguments->find(flutter::EncodableValue("doc"));
auto doc = vDoc != arguments->end()
? std::get<std::vector<uint8_t>>(vDoc->second)
: std::vector<uint8_t>{};
auto vPrinter = arguments->find(flutter::EncodableValue("printer"));
auto printer = vPrinter != arguments->end()
? std::get<std::string>(vPrinter->second)
: std::string{};
auto vJob = arguments->find(flutter::EncodableValue("job"));
auto jobNum = vJob != arguments->end() ? std::get<int>(vJob->second) : -1;
auto job = std::make_unique<PrintJob>(&printing, jobNum);
auto res = job->directPrintPdf(name, doc, printer);
result->Success(flutter::EncodableValue(1));
printing.onCompleted(job.get(), res, "");
} else if (method_call.method_name().compare("sharePdf") == 0) {
const auto* arguments =
std::get_if<flutter::EncodableMap>(method_call.arguments());
... ...