Showing
6 changed files
with
48 additions
and
28 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,15 +23,10 @@ public class PrintingHandler implements MethodChannel.MethodCallHandler { | @@ -21,15 +23,10 @@ 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) { |
| 26 | + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) { | ||
| 24 | switch (call.method) { | 27 | switch (call.method) { |
| 25 | case "printPdf": { | 28 | case "printPdf": { |
| 26 | final String name = call.argument("name"); | 29 | 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 | 30 | ||
| 34 | final PrintingJob printJob = | 31 | final PrintingJob printJob = |
| 35 | new PrintingJob(activity, this, (int) call.argument("job")); | 32 | new PrintingJob(activity, this, (int) call.argument("job")); |
| @@ -70,14 +67,15 @@ public class PrintingHandler implements MethodChannel.MethodCallHandler { | @@ -70,14 +67,15 @@ public class PrintingHandler implements MethodChannel.MethodCallHandler { | ||
| 70 | assert marginRight != null; | 67 | assert marginRight != null; |
| 71 | assert marginBottom != null; | 68 | assert marginBottom != null; |
| 72 | 69 | ||
| 73 | - PrintAttributes.Margins margins = | ||
| 74 | - new PrintAttributes.Margins(Double.valueOf(marginLeft * 1000.0).intValue(), | 70 | + PrintAttributes.Margins margins = new PrintAttributes.Margins( |
| 71 | + Double.valueOf(marginLeft * 1000.0).intValue(), | ||
| 75 | Double.valueOf(marginTop * 1000.0 / 72.0).intValue(), | 72 | Double.valueOf(marginTop * 1000.0 / 72.0).intValue(), |
| 76 | Double.valueOf(marginRight * 1000.0 / 72.0).intValue(), | 73 | Double.valueOf(marginRight * 1000.0 / 72.0).intValue(), |
| 77 | Double.valueOf(marginBottom * 1000.0 / 72.0).intValue()); | 74 | Double.valueOf(marginBottom * 1000.0 / 72.0).intValue()); |
| 78 | 75 | ||
| 79 | - PrintAttributes.MediaSize size = new PrintAttributes.MediaSize("flutter_printing", | ||
| 80 | - "Provided size", Double.valueOf(width * 1000.0 / 72.0).intValue(), | 76 | + PrintAttributes.MediaSize size = |
| 77 | + new PrintAttributes.MediaSize("flutter_printing", "Provided size", | ||
| 78 | + Double.valueOf(width * 1000.0 / 72.0).intValue(), | ||
| 81 | Double.valueOf(height * 1000.0 / 72.0).intValue()); | 79 | Double.valueOf(height * 1000.0 / 72.0).intValue()); |
| 82 | 80 | ||
| 83 | printJob.convertHtml((String) call.argument("html"), size, margins, | 81 | printJob.convertHtml((String) call.argument("html"), size, margins, |
| @@ -103,9 +101,13 @@ public class PrintingHandler implements MethodChannel.MethodCallHandler { | @@ -103,9 +101,13 @@ public class PrintingHandler implements MethodChannel.MethodCallHandler { | ||
| 103 | result.notImplemented(); | 101 | result.notImplemented(); |
| 104 | break; | 102 | break; |
| 105 | } | 103 | } |
| 104 | + } else { | ||
| 105 | + result.notImplemented(); | ||
| 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