Skip to content

Commit

Permalink
added new printing output type value with print pdf method (#1693)
Browse files Browse the repository at this point in the history
* added new printing output type value with print pdf method

* removed logs

* fix test with new output type value
  • Loading branch information
tapeo authored Jul 5, 2024
1 parent 2be58d5 commit c5f5d1c
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 5 deletions.
11 changes: 9 additions & 2 deletions printing/ios/Classes/PrintJob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,19 @@ public class PrintJob: UIPrintPageRenderer, UIPrintInteractionControllerDelegate
return paperList[0]
}

for paper in paperList {
if (paper.paperSize.width == currentSize!.width && paper.paperSize.height == currentSize!.height) ||
(paper.paperSize.width == currentSize!.height && paper.paperSize.height == currentSize!.width) {
return paper
}
}

let bestPaper = UIPrintPaper.bestPaper(forPageSize: currentSize!, withPapersFrom: paperList)

return bestPaper
}

func printPdf(name: String, withPageSize size: CGSize, andMargin margin: CGRect, withPrinter printerID: String?, dynamically dyn: Bool) {
func printPdf(name: String, withPageSize size: CGSize, andMargin margin: CGRect, withPrinter printerID: String?, dynamically dyn: Bool, outputType type: UIPrintInfo.OutputType) {
currentSize = size
dynamic = dyn
let printing = UIPrintInteractionController.isPrintingAvailable
Expand All @@ -175,7 +182,7 @@ public class PrintJob: UIPrintPageRenderer, UIPrintInteractionControllerDelegate

let printInfo = UIPrintInfo.printInfo()
printInfo.jobName = jobName!
printInfo.outputType = .general
printInfo.outputType = type
if orientation != nil {
printInfo.orientation = orientation!
orientation = nil
Expand Down
22 changes: 20 additions & 2 deletions printing/ios/Classes/PrintingPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,22 @@ public class PrintingPlugin: NSObject, FlutterPlugin {
let marginBottom = CGFloat((args["marginBottom"] as! NSNumber).floatValue)
let printJob = PrintJob(printing: self, index: args["job"] as! Int)
let dynamic = args["dynamic"] as! Bool


let outputType: UIPrintInfo.OutputType
switch (args["outputType"] as! Int) {
case 0:
outputType = UIPrintInfo.OutputType.general
case 1:
outputType = UIPrintInfo.OutputType.photo
case 2:
outputType = UIPrintInfo.OutputType.grayscale
case 3:
outputType = UIPrintInfo.OutputType.photoGrayscale
default:
outputType = UIPrintInfo.OutputType.general
}

jobs[args["job"] as! UInt32] = printJob
printJob.printPdf(name: name,
withPageSize: CGSize(
Expand All @@ -71,8 +87,10 @@ public class PrintingPlugin: NSObject, FlutterPlugin {
y: marginTop,
width: width - marginRight - marginLeft,
height: height - marginBottom - marginTop
), withPrinter: printer,
dynamically: dynamic)
),
withPrinter: printer,
dynamically: dynamic,
outputType: outputType)
result(NSNumber(value: 1))
} else if call.method == "sharePdf" {
let object = args["doc"] as! FlutterStandardTypedData
Expand Down
1 change: 1 addition & 0 deletions printing/lib/printing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export 'src/asset_utils.dart';
export 'src/cache.dart';
export 'src/callback.dart';
export 'src/fonts/gfonts.dart';
export 'src/output_type.dart';
export 'src/preview/action_bar_theme.dart';
export 'src/preview/actions.dart';
export 'src/preview/pdf_preview.dart';
Expand Down
2 changes: 2 additions & 0 deletions printing/lib/printing_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import 'package:web/web.dart' as web;
import 'src/callback.dart';
import 'src/interface.dart';
import 'src/mutex.dart';
import 'src/output_type.dart';
import 'src/pdfjs.dart';
import 'src/printer.dart';
import 'src/printing_info.dart';
Expand Down Expand Up @@ -156,6 +157,7 @@ class PrintingPlugin extends PrintingPlatform {
PdfPageFormat format,
bool dynamicLayout,
bool usePrinterSettings,
OutputType outputType,
) async {
late Uint8List result;
try {
Expand Down
2 changes: 2 additions & 0 deletions printing/lib/src/interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import 'package:plugin_platform_interface/plugin_platform_interface.dart';

import 'callback.dart';
import 'method_channel.dart';
import 'output_type.dart';
import 'printer.dart';
import 'printing_info.dart';
import 'raster.dart';
Expand Down Expand Up @@ -66,6 +67,7 @@ abstract class PrintingPlatform extends PlatformInterface {
PdfPageFormat format,
bool dynamicLayout,
bool usePrinterSettings,
OutputType outputType,
);

/// Enumerate the available printers on the system.
Expand Down
3 changes: 3 additions & 0 deletions printing/lib/src/method_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import 'package:pdf/pdf.dart';
import 'callback.dart';
import 'interface.dart';
import 'method_channel_ffi.dart' if (dart.library.js) 'method_channel_js.dart';
import 'output_type.dart';
import 'print_job.dart';
import 'printer.dart';
import 'printing_info.dart';
Expand Down Expand Up @@ -180,6 +181,7 @@ class MethodChannelPrinting extends PrintingPlatform {
PdfPageFormat format,
bool dynamicLayout,
bool usePrinterSettings,
OutputType outputType,
) async {
final job = _printJobs.add(
onCompleted: Completer<bool>(),
Expand All @@ -198,6 +200,7 @@ class MethodChannelPrinting extends PrintingPlatform {
'marginBottom': format.marginBottom,
'dynamic': dynamicLayout,
'usePrinterSettings': usePrinterSettings,
'outputType': outputType.index,
};

await _channel.invokeMethod<int>('printPdf', params);
Expand Down
6 changes: 6 additions & 0 deletions printing/lib/src/output_type.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
enum OutputType {
generic,
photo,
grayscale,
photoGrayscale,
}
8 changes: 8 additions & 0 deletions printing/lib/src/printing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import 'package:pdf/pdf.dart';

import 'callback.dart';
import 'interface.dart';
import 'output_type.dart';
import 'printer.dart';
import 'printing_info.dart';
import 'raster.dart';
Expand All @@ -39,12 +40,16 @@ mixin Printing {
/// 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)
/// Set [outputType] to [OutputType.generic] to use the default printing
/// system, or [OutputType.photos] to use the photo printing system.
/// (Supported platforms: iOS)
static Future<bool> layoutPdf({
required LayoutCallback onLayout,
String name = 'Document',
PdfPageFormat format = PdfPageFormat.standard,
bool dynamicLayout = true,
bool usePrinterSettings = false,
OutputType outputType = OutputType.generic,
}) {
return PrintingPlatform.instance.layoutPdf(
null,
Expand All @@ -53,6 +58,7 @@ mixin Printing {
format,
dynamicLayout,
usePrinterSettings,
outputType,
);
}

Expand Down Expand Up @@ -139,6 +145,7 @@ mixin Printing {
PdfPageFormat format = PdfPageFormat.standard,
bool dynamicLayout = true,
bool usePrinterSettings = false,
OutputType outputType = OutputType.generic,
}) {
return PrintingPlatform.instance.layoutPdf(
printer,
Expand All @@ -147,6 +154,7 @@ mixin Printing {
format,
dynamicLayout,
usePrinterSettings,
outputType,
);
}

Expand Down
2 changes: 1 addition & 1 deletion printing/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ topics:
- print
- printing
- report
version: 5.13.1
version: 5.13.2

environment:
sdk: ">=3.3.0 <4.0.0"
Expand Down
1 change: 1 addition & 0 deletions printing/test/printing_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ class MockPrinting extends Mock
PdfPageFormat format,
bool dynamicLayout,
bool usePrinterSettings,
OutputType outputType,
) async =>
true;

Expand Down

0 comments on commit c5f5d1c

Please sign in to comment.