Showing
5 changed files
with
33 additions
and
16 deletions
@@ -152,7 +152,7 @@ public class PrintingPlugin extends PrintDocumentAdapter implements MethodCallHa | @@ -152,7 +152,7 @@ public class PrintingPlugin extends PrintDocumentAdapter implements MethodCallHa | ||
152 | result.success(0); | 152 | result.success(0); |
153 | break; | 153 | break; |
154 | case "sharePdf": | 154 | case "sharePdf": |
155 | - sharePdf((byte[]) call.argument("doc")); | 155 | + sharePdf((byte[]) call.argument("doc"), (String) call.argument("name")); |
156 | result.success(0); | 156 | result.success(0); |
157 | break; | 157 | break; |
158 | default: | 158 | default: |
@@ -161,11 +161,16 @@ public class PrintingPlugin extends PrintDocumentAdapter implements MethodCallHa | @@ -161,11 +161,16 @@ public class PrintingPlugin extends PrintDocumentAdapter implements MethodCallHa | ||
161 | } | 161 | } |
162 | } | 162 | } |
163 | 163 | ||
164 | - private void sharePdf(byte[] data) { | 164 | + private void sharePdf(byte[] data, String name) { |
165 | try { | 165 | try { |
166 | final File externalFilesDirectory = | 166 | final File externalFilesDirectory = |
167 | - activity.getExternalFilesDir(Environment.DIRECTORY_PICTURES); | ||
168 | - File shareFile = File.createTempFile("document", ".pdf", externalFilesDirectory); | 167 | + activity.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS); |
168 | + File shareFile; | ||
169 | + if (name == null) { | ||
170 | + shareFile = File.createTempFile("document-", ".pdf", externalFilesDirectory); | ||
171 | + } else { | ||
172 | + shareFile = new File(externalFilesDirectory, name); | ||
173 | + } | ||
169 | 174 | ||
170 | FileOutputStream stream = new FileOutputStream(shareFile); | 175 | FileOutputStream stream = new FileOutputStream(shareFile); |
171 | stream.write(data); | 176 | stream.write(data); |
@@ -44,7 +44,7 @@ class MyAppState extends State<MyApp> { | @@ -44,7 +44,7 @@ class MyAppState extends State<MyApp> { | ||
44 | referenceBox.localToGlobal(referenceBox.paintBounds.bottomRight); | 44 | referenceBox.localToGlobal(referenceBox.paintBounds.bottomRight); |
45 | final Rect bounds = Rect.fromPoints(topLeft, bottomRight); | 45 | final Rect bounds = Rect.fromPoints(topLeft, bottomRight); |
46 | 46 | ||
47 | - Printing.sharePdf(document: pdf, bounds: bounds); | 47 | + Printing.sharePdf(document: pdf, filename: 'my-résumé.pdf', bounds: bounds); |
48 | } | 48 | } |
49 | 49 | ||
50 | Future<void> _printScreen() async { | 50 | Future<void> _printScreen() async { |
@@ -50,7 +50,8 @@ | @@ -50,7 +50,8 @@ | ||
50 | [[call.arguments objectForKey:@"x"] floatValue], | 50 | [[call.arguments objectForKey:@"x"] floatValue], |
51 | [[call.arguments objectForKey:@"y"] floatValue], | 51 | [[call.arguments objectForKey:@"y"] floatValue], |
52 | [[call.arguments objectForKey:@"w"] floatValue], | 52 | [[call.arguments objectForKey:@"w"] floatValue], |
53 | - [[call.arguments objectForKey:@"h"] floatValue])]; | 53 | + [[call.arguments objectForKey:@"h"] floatValue]) |
54 | + andName:[call.arguments objectForKey:@"name"]]; | ||
54 | result(@1); | 55 | result(@1); |
55 | } else { | 56 | } else { |
56 | result(FlutterMethodNotImplemented); | 57 | result(FlutterMethodNotImplemented); |
@@ -92,21 +93,27 @@ | @@ -92,21 +93,27 @@ | ||
92 | } | 93 | } |
93 | 94 | ||
94 | - (void)sharePdf:(nonnull FlutterStandardTypedData*)data | 95 | - (void)sharePdf:(nonnull FlutterStandardTypedData*)data |
95 | - withSourceRect:(CGRect)rect { | 96 | + withSourceRect:(CGRect)rect |
97 | + andName:(NSString*)name { | ||
96 | NSURL* tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory() | 98 | NSURL* tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory() |
97 | isDirectory:YES]; | 99 | isDirectory:YES]; |
98 | 100 | ||
99 | CFUUIDRef uuid = CFUUIDCreate(NULL); | 101 | CFUUIDRef uuid = CFUUIDCreate(NULL); |
100 | - assert(uuid != NULL); | 102 | + assert(uuid != nil); |
101 | 103 | ||
102 | CFStringRef uuidStr = CFUUIDCreateString(NULL, uuid); | 104 | CFStringRef uuidStr = CFUUIDCreateString(NULL, uuid); |
103 | - assert(uuidStr != NULL); | ||
104 | - | ||
105 | - NSURL* fileURL = [[tmpDirURL | ||
106 | - URLByAppendingPathComponent:[NSString | ||
107 | - stringWithFormat:@"pdf-%@", uuidStr]] | ||
108 | - URLByAppendingPathExtension:@"pdf"]; | ||
109 | - assert(fileURL != NULL); | 105 | + assert(uuidStr != nil); |
106 | + | ||
107 | + NSURL* fileURL; | ||
108 | + if ([name isEqual:[NSNull null]]) { | ||
109 | + fileURL = [[tmpDirURL | ||
110 | + URLByAppendingPathComponent:[NSString stringWithFormat:@"document-%@", | ||
111 | + uuidStr]] | ||
112 | + URLByAppendingPathExtension:@"pdf"]; | ||
113 | + } else { | ||
114 | + fileURL = [tmpDirURL URLByAppendingPathComponent:name]; | ||
115 | + } | ||
116 | + assert(fileURL != nil); | ||
110 | 117 | ||
111 | CFRelease(uuidStr); | 118 | CFRelease(uuidStr); |
112 | CFRelease(uuid); | 119 | CFRelease(uuid); |
@@ -65,7 +65,10 @@ mixin Printing { | @@ -65,7 +65,10 @@ mixin Printing { | ||
65 | 65 | ||
66 | /// Displays a platform popup to share the Pdf document to another application | 66 | /// Displays a platform popup to share the Pdf document to another application |
67 | static Future<void> sharePdf( | 67 | static Future<void> sharePdf( |
68 | - {PdfDocument document, List<int> bytes, Rect bounds}) async { | 68 | + {PdfDocument document, |
69 | + List<int> bytes, | ||
70 | + String filename, | ||
71 | + Rect bounds}) async { | ||
69 | assert(document != null || bytes != null); | 72 | assert(document != null || bytes != null); |
70 | assert(!(document == null && bytes == null)); | 73 | assert(!(document == null && bytes == null)); |
71 | 74 | ||
@@ -77,6 +80,7 @@ mixin Printing { | @@ -77,6 +80,7 @@ mixin Printing { | ||
77 | 80 | ||
78 | final Map<String, dynamic> params = <String, dynamic>{ | 81 | final Map<String, dynamic> params = <String, dynamic>{ |
79 | 'doc': Uint8List.fromList(bytes), | 82 | 'doc': Uint8List.fromList(bytes), |
83 | + 'name': filename, | ||
80 | 'x': bounds.left, | 84 | 'x': bounds.left, |
81 | 'y': bounds.top, | 85 | 'y': bounds.top, |
82 | 'w': bounds.width, | 86 | 'w': bounds.width, |
-
Please register or login to post a comment