Showing
6 changed files
with
118 additions
and
98 deletions
@@ -35,16 +35,6 @@ for documentation. | @@ -35,16 +35,6 @@ for documentation. | ||
35 | use_frameworks! # <-- Add this line | 35 | use_frameworks! # <-- Add this line |
36 | ``` | 36 | ``` |
37 | 37 | ||
38 | -4. Set minimum Android version in `android/app/build.gradle`: | ||
39 | - | ||
40 | - ```java | ||
41 | - defaultConfig { | ||
42 | - ... | ||
43 | - minSdkVersion 21 // <-- Change this line to 21 or more | ||
44 | - ... | ||
45 | - } | ||
46 | - ``` | ||
47 | - | ||
48 | ## Examples | 38 | ## Examples |
49 | 39 | ||
50 | ```dart | 40 | ```dart |
@@ -17,16 +17,20 @@ | @@ -17,16 +17,20 @@ | ||
17 | package android.print; | 17 | package android.print; |
18 | 18 | ||
19 | import android.content.Context; | 19 | import android.content.Context; |
20 | +import android.os.Build; | ||
20 | import android.os.CancellationSignal; | 21 | import android.os.CancellationSignal; |
21 | import android.os.ParcelFileDescriptor; | 22 | import android.os.ParcelFileDescriptor; |
22 | import android.util.Log; | 23 | import android.util.Log; |
23 | 24 | ||
25 | +import androidx.annotation.RequiresApi; | ||
26 | + | ||
24 | import java.io.File; | 27 | import java.io.File; |
25 | import java.io.FileInputStream; | 28 | import java.io.FileInputStream; |
26 | import java.io.FileNotFoundException; | 29 | import java.io.FileNotFoundException; |
27 | import java.io.IOException; | 30 | import java.io.IOException; |
28 | import java.io.InputStream; | 31 | import java.io.InputStream; |
29 | 32 | ||
33 | +@RequiresApi(api = Build.VERSION_CODES.KITKAT) | ||
30 | public class PdfConvert { | 34 | public class PdfConvert { |
31 | public static void print(final Context context, final PrintDocumentAdapter adapter, | 35 | public static void print(final Context context, final PrintDocumentAdapter adapter, |
32 | final PrintAttributes attributes, final Result result) { | 36 | final PrintAttributes attributes, final Result result) { |
1 | package net.nfet.flutter.printing; | 1 | package net.nfet.flutter.printing; |
2 | 2 | ||
3 | import android.app.Activity; | 3 | import android.app.Activity; |
4 | +import android.os.Build; | ||
4 | import android.print.PrintAttributes; | 5 | import android.print.PrintAttributes; |
5 | 6 | ||
6 | import androidx.annotation.NonNull; | 7 | import androidx.annotation.NonNull; |
8 | +import androidx.annotation.RequiresApi; | ||
7 | 9 | ||
8 | import java.util.HashMap; | 10 | import java.util.HashMap; |
9 | 11 | ||
@@ -21,91 +23,91 @@ public class PrintingHandler implements MethodChannel.MethodCallHandler { | @@ -21,91 +23,91 @@ public class PrintingHandler implements MethodChannel.MethodCallHandler { | ||
21 | 23 | ||
22 | @Override | 24 | @Override |
23 | public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) { | 25 | public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) { |
24 | - switch (call.method) { | ||
25 | - case "printPdf": { | ||
26 | - final String name = call.argument("name"); | ||
27 | - final Double width = call.argument("width"); | ||
28 | - final Double height = call.argument("height"); | ||
29 | - final Double marginLeft = call.argument("marginLeft"); | ||
30 | - final Double marginTop = call.argument("marginTop"); | ||
31 | - final Double marginRight = call.argument("marginRight"); | ||
32 | - final Double marginBottom = call.argument("marginBottom"); | ||
33 | - | ||
34 | - final PrintingJob printJob = | ||
35 | - new PrintingJob(activity, this, (int) call.argument("job")); | ||
36 | - assert name != null; | ||
37 | - printJob.printPdf(name); | ||
38 | - | ||
39 | - result.success(1); | ||
40 | - break; | ||
41 | - } | ||
42 | - case "cancelJob": { | ||
43 | - final PrintingJob printJob = | ||
44 | - new PrintingJob(activity, this, (int) call.argument("job")); | ||
45 | - printJob.cancelJob(null); | ||
46 | - result.success(1); | ||
47 | - break; | ||
48 | - } | ||
49 | - case "sharePdf": { | ||
50 | - final byte[] document = call.argument("doc"); | ||
51 | - final String name = call.argument("name"); | ||
52 | - PrintingJob.sharePdf(activity, document, name); | ||
53 | - result.success(1); | ||
54 | - break; | ||
55 | - } | ||
56 | - case "convertHtml": { | ||
57 | - Double width = call.argument("width"); | ||
58 | - Double height = call.argument("height"); | ||
59 | - Double marginLeft = call.argument("marginLeft"); | ||
60 | - Double marginTop = call.argument("marginTop"); | ||
61 | - Double marginRight = call.argument("marginRight"); | ||
62 | - Double marginBottom = call.argument("marginBottom"); | ||
63 | - final PrintingJob printJob = | ||
64 | - new PrintingJob(activity, this, (int) call.argument("job")); | ||
65 | - | ||
66 | - assert width != null; | ||
67 | - assert height != null; | ||
68 | - assert marginLeft != null; | ||
69 | - assert marginTop != null; | ||
70 | - assert marginRight != null; | ||
71 | - assert marginBottom != null; | ||
72 | - | ||
73 | - PrintAttributes.Margins margins = | ||
74 | - new PrintAttributes.Margins(Double.valueOf(marginLeft * 1000.0).intValue(), | ||
75 | - Double.valueOf(marginTop * 1000.0 / 72.0).intValue(), | ||
76 | - Double.valueOf(marginRight * 1000.0 / 72.0).intValue(), | ||
77 | - Double.valueOf(marginBottom * 1000.0 / 72.0).intValue()); | ||
78 | - | ||
79 | - PrintAttributes.MediaSize size = new PrintAttributes.MediaSize("flutter_printing", | ||
80 | - "Provided size", Double.valueOf(width * 1000.0 / 72.0).intValue(), | ||
81 | - Double.valueOf(height * 1000.0 / 72.0).intValue()); | ||
82 | - | ||
83 | - printJob.convertHtml((String) call.argument("html"), size, margins, | ||
84 | - (String) call.argument("baseUrl")); | ||
85 | - result.success(1); | ||
86 | - break; | ||
87 | - } | ||
88 | - case "printingInfo": { | ||
89 | - result.success(PrintingJob.printingInfo()); | ||
90 | - break; | ||
91 | - } | ||
92 | - case "rasterPdf": { | ||
93 | - final byte[] document = call.argument("doc"); | ||
94 | - final int[] pages = call.argument("pages"); | ||
95 | - Double scale = call.argument("scale"); | ||
96 | - final PrintingJob printJob = | ||
97 | - new PrintingJob(activity, this, (int) call.argument("job")); | ||
98 | - printJob.rasterPdf(document, pages, scale); | ||
99 | - result.success(1); | ||
100 | - break; | 26 | + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) { |
27 | + switch (call.method) { | ||
28 | + case "printPdf": { | ||
29 | + final String name = call.argument("name"); | ||
30 | + | ||
31 | + final PrintingJob printJob = | ||
32 | + new PrintingJob(activity, this, (int) call.argument("job")); | ||
33 | + assert name != null; | ||
34 | + printJob.printPdf(name); | ||
35 | + | ||
36 | + result.success(1); | ||
37 | + break; | ||
38 | + } | ||
39 | + case "cancelJob": { | ||
40 | + final PrintingJob printJob = | ||
41 | + new PrintingJob(activity, this, (int) call.argument("job")); | ||
42 | + printJob.cancelJob(null); | ||
43 | + result.success(1); | ||
44 | + break; | ||
45 | + } | ||
46 | + case "sharePdf": { | ||
47 | + final byte[] document = call.argument("doc"); | ||
48 | + final String name = call.argument("name"); | ||
49 | + PrintingJob.sharePdf(activity, document, name); | ||
50 | + result.success(1); | ||
51 | + break; | ||
52 | + } | ||
53 | + case "convertHtml": { | ||
54 | + Double width = call.argument("width"); | ||
55 | + Double height = call.argument("height"); | ||
56 | + Double marginLeft = call.argument("marginLeft"); | ||
57 | + Double marginTop = call.argument("marginTop"); | ||
58 | + Double marginRight = call.argument("marginRight"); | ||
59 | + Double marginBottom = call.argument("marginBottom"); | ||
60 | + final PrintingJob printJob = | ||
61 | + new PrintingJob(activity, this, (int) call.argument("job")); | ||
62 | + | ||
63 | + assert width != null; | ||
64 | + assert height != null; | ||
65 | + assert marginLeft != null; | ||
66 | + assert marginTop != null; | ||
67 | + assert marginRight != null; | ||
68 | + assert marginBottom != null; | ||
69 | + | ||
70 | + PrintAttributes.Margins margins = new PrintAttributes.Margins( | ||
71 | + Double.valueOf(marginLeft * 1000.0).intValue(), | ||
72 | + Double.valueOf(marginTop * 1000.0 / 72.0).intValue(), | ||
73 | + Double.valueOf(marginRight * 1000.0 / 72.0).intValue(), | ||
74 | + Double.valueOf(marginBottom * 1000.0 / 72.0).intValue()); | ||
75 | + | ||
76 | + PrintAttributes.MediaSize size = | ||
77 | + new PrintAttributes.MediaSize("flutter_printing", "Provided size", | ||
78 | + Double.valueOf(width * 1000.0 / 72.0).intValue(), | ||
79 | + Double.valueOf(height * 1000.0 / 72.0).intValue()); | ||
80 | + | ||
81 | + printJob.convertHtml((String) call.argument("html"), size, margins, | ||
82 | + (String) call.argument("baseUrl")); | ||
83 | + result.success(1); | ||
84 | + break; | ||
85 | + } | ||
86 | + case "printingInfo": { | ||
87 | + result.success(PrintingJob.printingInfo()); | ||
88 | + break; | ||
89 | + } | ||
90 | + case "rasterPdf": { | ||
91 | + final byte[] document = call.argument("doc"); | ||
92 | + final int[] pages = call.argument("pages"); | ||
93 | + Double scale = call.argument("scale"); | ||
94 | + final PrintingJob printJob = | ||
95 | + new PrintingJob(activity, this, (int) call.argument("job")); | ||
96 | + printJob.rasterPdf(document, pages, scale); | ||
97 | + result.success(1); | ||
98 | + break; | ||
99 | + } | ||
100 | + default: | ||
101 | + result.notImplemented(); | ||
102 | + break; | ||
101 | } | 103 | } |
102 | - default: | ||
103 | - result.notImplemented(); | ||
104 | - break; | 104 | + } else { |
105 | + result.notImplemented(); | ||
105 | } | 106 | } |
106 | } | 107 | } |
107 | 108 | ||
108 | /// Request the Pdf document from flutter | 109 | /// Request the Pdf document from flutter |
110 | + @RequiresApi(api = Build.VERSION_CODES.KITKAT) | ||
109 | void onLayout(final PrintingJob printJob, Double width, double height, double marginLeft, | 111 | void onLayout(final PrintingJob printJob, Double width, double height, double marginLeft, |
110 | double marginTop, double marginRight, double marginBottom) { | 112 | double marginTop, double marginRight, double marginBottom) { |
111 | HashMap<String, Object> args = new HashMap<>(); | 113 | HashMap<String, Object> args = new HashMap<>(); |
@@ -141,6 +143,7 @@ public class PrintingHandler implements MethodChannel.MethodCallHandler { | @@ -141,6 +143,7 @@ public class PrintingHandler implements MethodChannel.MethodCallHandler { | ||
141 | } | 143 | } |
142 | 144 | ||
143 | /// send completion status to flutter | 145 | /// send completion status to flutter |
146 | + @RequiresApi(api = Build.VERSION_CODES.KITKAT) | ||
144 | void onCompleted(PrintingJob printJob, boolean completed, String error) { | 147 | void onCompleted(PrintingJob printJob, boolean completed, String error) { |
145 | HashMap<String, Object> args = new HashMap<>(); | 148 | HashMap<String, Object> args = new HashMap<>(); |
146 | args.put("completed", completed); | 149 | args.put("completed", completed); |
@@ -152,6 +155,7 @@ public class PrintingHandler implements MethodChannel.MethodCallHandler { | @@ -152,6 +155,7 @@ public class PrintingHandler implements MethodChannel.MethodCallHandler { | ||
152 | } | 155 | } |
153 | 156 | ||
154 | /// send html to pdf data result to flutter | 157 | /// send html to pdf data result to flutter |
158 | + @RequiresApi(api = Build.VERSION_CODES.KITKAT) | ||
155 | void onHtmlRendered(PrintingJob printJob, byte[] pdfData) { | 159 | void onHtmlRendered(PrintingJob printJob, byte[] pdfData) { |
156 | HashMap<String, Object> args = new HashMap<>(); | 160 | HashMap<String, Object> args = new HashMap<>(); |
157 | args.put("doc", pdfData); | 161 | args.put("doc", pdfData); |
@@ -161,6 +165,7 @@ public class PrintingHandler implements MethodChannel.MethodCallHandler { | @@ -161,6 +165,7 @@ public class PrintingHandler implements MethodChannel.MethodCallHandler { | ||
161 | } | 165 | } |
162 | 166 | ||
163 | /// send html to pdf conversion error to flutter | 167 | /// send html to pdf conversion error to flutter |
168 | + @RequiresApi(api = Build.VERSION_CODES.KITKAT) | ||
164 | void onHtmlError(PrintingJob printJob, String error) { | 169 | void onHtmlError(PrintingJob printJob, String error) { |
165 | HashMap<String, Object> args = new HashMap<>(); | 170 | HashMap<String, Object> args = new HashMap<>(); |
166 | args.put("error", error); | 171 | args.put("error", error); |
@@ -170,6 +175,7 @@ public class PrintingHandler implements MethodChannel.MethodCallHandler { | @@ -170,6 +175,7 @@ public class PrintingHandler implements MethodChannel.MethodCallHandler { | ||
170 | } | 175 | } |
171 | 176 | ||
172 | /// send pdf to raster data result to flutter | 177 | /// send pdf to raster data result to flutter |
178 | + @RequiresApi(api = Build.VERSION_CODES.KITKAT) | ||
173 | void onPageRasterized(PrintingJob printJob, byte[] imageData, int width, int height) { | 179 | void onPageRasterized(PrintingJob printJob, byte[] imageData, int width, int height) { |
174 | HashMap<String, Object> args = new HashMap<>(); | 180 | HashMap<String, Object> args = new HashMap<>(); |
175 | args.put("image", imageData); | 181 | args.put("image", imageData); |
@@ -180,6 +186,8 @@ public class PrintingHandler implements MethodChannel.MethodCallHandler { | @@ -180,6 +186,8 @@ public class PrintingHandler implements MethodChannel.MethodCallHandler { | ||
180 | channel.invokeMethod("onPageRasterized", args); | 186 | channel.invokeMethod("onPageRasterized", args); |
181 | } | 187 | } |
182 | 188 | ||
189 | + /// The page has been converted to an image | ||
190 | + @RequiresApi(api = Build.VERSION_CODES.KITKAT) | ||
183 | void onPageRasterEnd(PrintingJob printJob) { | 191 | void onPageRasterEnd(PrintingJob printJob) { |
184 | HashMap<String, Object> args = new HashMap<>(); | 192 | HashMap<String, Object> args = new HashMap<>(); |
185 | args.put("job", printJob.index); | 193 | args.put("job", printJob.index); |
@@ -18,20 +18,35 @@ package net.nfet.flutter.printing; | @@ -18,20 +18,35 @@ package net.nfet.flutter.printing; | ||
18 | 18 | ||
19 | import android.content.Context; | 19 | import android.content.Context; |
20 | import android.content.Intent; | 20 | import android.content.Intent; |
21 | +import android.content.pm.PackageManager; | ||
22 | +import android.content.pm.ResolveInfo; | ||
21 | import android.content.res.Configuration; | 23 | import android.content.res.Configuration; |
24 | +import android.graphics.Bitmap; | ||
25 | +import android.graphics.Matrix; | ||
26 | +import android.graphics.pdf.PdfRenderer; | ||
22 | import android.net.Uri; | 27 | import android.net.Uri; |
28 | +import android.os.Build; | ||
23 | import android.os.Bundle; | 29 | import android.os.Bundle; |
24 | import android.os.CancellationSignal; | 30 | import android.os.CancellationSignal; |
31 | +import android.os.Handler; | ||
32 | +import android.os.Looper; | ||
25 | import android.os.ParcelFileDescriptor; | 33 | import android.os.ParcelFileDescriptor; |
26 | import android.print.PageRange; | 34 | import android.print.PageRange; |
27 | import android.print.PdfConvert; | 35 | import android.print.PdfConvert; |
28 | import android.print.PrintAttributes; | 36 | import android.print.PrintAttributes; |
37 | +import android.print.PrintDocumentAdapter; | ||
38 | +import android.print.PrintDocumentInfo; | ||
29 | import android.print.PrintJob; | 39 | import android.print.PrintJob; |
40 | +import android.print.PrintJobInfo; | ||
30 | import android.print.PrintManager; | 41 | import android.print.PrintManager; |
42 | +import android.util.Log; | ||
31 | import android.webkit.WebView; | 43 | import android.webkit.WebView; |
32 | import android.webkit.WebViewClient; | 44 | import android.webkit.WebViewClient; |
45 | + | ||
33 | import androidx.annotation.NonNull; | 46 | import androidx.annotation.NonNull; |
47 | +import androidx.annotation.RequiresApi; | ||
34 | import androidx.core.content.FileProvider; | 48 | import androidx.core.content.FileProvider; |
49 | + | ||
35 | import java.io.File; | 50 | import java.io.File; |
36 | import java.io.FileInputStream; | 51 | import java.io.FileInputStream; |
37 | import java.io.FileOutputStream; | 52 | import java.io.FileOutputStream; |
@@ -39,16 +54,12 @@ import java.io.IOException; | @@ -39,16 +54,12 @@ import java.io.IOException; | ||
39 | import java.io.OutputStream; | 54 | import java.io.OutputStream; |
40 | import java.nio.ByteBuffer; | 55 | import java.nio.ByteBuffer; |
41 | import java.util.HashMap; | 56 | import java.util.HashMap; |
42 | -import android.content.pm.PackageManager; | ||
43 | -import android.content.pm.ProviderInfo; | ||
44 | -import android.content.pm.ResolveInfo; | ||
45 | import java.util.List; | 57 | import java.util.List; |
46 | 58 | ||
47 | - | ||
48 | - | ||
49 | /** | 59 | /** |
50 | * PrintJob | 60 | * PrintJob |
51 | */ | 61 | */ |
62 | +@RequiresApi(api = Build.VERSION_CODES.KITKAT) | ||
52 | public class PrintingJob extends PrintDocumentAdapter { | 63 | public class PrintingJob extends PrintDocumentAdapter { |
53 | private static PrintManager printManager; | 64 | private static PrintManager printManager; |
54 | private final Context context; | 65 | private final Context context; |
@@ -216,11 +227,14 @@ public class PrintingJob extends PrintDocumentAdapter { | @@ -216,11 +227,14 @@ public class PrintingJob extends PrintDocumentAdapter { | ||
216 | shareIntent.putExtra(Intent.EXTRA_STREAM, apkURI); | 227 | shareIntent.putExtra(Intent.EXTRA_STREAM, apkURI); |
217 | shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); | 228 | shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); |
218 | Intent chooserIntent = Intent.createChooser(shareIntent, null); | 229 | Intent chooserIntent = Intent.createChooser(shareIntent, null); |
219 | - List<ResolveInfo> resInfoList = context.getPackageManager().queryIntentActivities(chooserIntent, PackageManager.MATCH_DEFAULT_ONLY); | 230 | + List<ResolveInfo> resInfoList = context.getPackageManager().queryIntentActivities( |
231 | + chooserIntent, PackageManager.MATCH_DEFAULT_ONLY); | ||
220 | 232 | ||
221 | for (ResolveInfo resolveInfo : resInfoList) { | 233 | for (ResolveInfo resolveInfo : resInfoList) { |
222 | String packageName = resolveInfo.activityInfo.packageName; | 234 | String packageName = resolveInfo.activityInfo.packageName; |
223 | - context.grantUriPermission(packageName, apkURI, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION); | 235 | + context.grantUriPermission(packageName, apkURI, |
236 | + Intent.FLAG_GRANT_WRITE_URI_PERMISSION | ||
237 | + | Intent.FLAG_GRANT_READ_URI_PERMISSION); | ||
224 | } | 238 | } |
225 | context.startActivity(chooserIntent); | 239 | context.startActivity(chooserIntent); |
226 | shareFile.deleteOnExit(); | 240 | shareFile.deleteOnExit(); |
@@ -4,7 +4,7 @@ description: Plugin that allows Flutter apps to generate and print documents to | @@ -4,7 +4,7 @@ description: Plugin that allows Flutter apps to generate and print documents to | ||
4 | homepage: https://github.com/DavBfr/dart_pdf/tree/master/printing | 4 | homepage: https://github.com/DavBfr/dart_pdf/tree/master/printing |
5 | repository: https://github.com/DavBfr/dart_pdf | 5 | repository: https://github.com/DavBfr/dart_pdf |
6 | issue_tracker: https://github.com/DavBfr/dart_pdf/issues | 6 | issue_tracker: https://github.com/DavBfr/dart_pdf/issues |
7 | -version: 3.6.2 | 7 | +version: 3.6.3 |
8 | 8 | ||
9 | environment: | 9 | environment: |
10 | sdk: ">=2.3.0 <3.0.0" | 10 | sdk: ">=2.3.0 <3.0.0" |
-
Please register or login to post a comment