David PHAM-VAN

Add a filename parameter for sharing

# 1.3.4
* Fix dart lint warnings
* Add documentation
* Add a filename parameter for sharing
# 1.3.3
* Update Readme
... ...
... ... @@ -152,7 +152,7 @@ public class PrintingPlugin extends PrintDocumentAdapter implements MethodCallHa
result.success(0);
break;
case "sharePdf":
sharePdf((byte[]) call.argument("doc"));
sharePdf((byte[]) call.argument("doc"), (String) call.argument("name"));
result.success(0);
break;
default:
... ... @@ -161,11 +161,16 @@ public class PrintingPlugin extends PrintDocumentAdapter implements MethodCallHa
}
}
private void sharePdf(byte[] data) {
private void sharePdf(byte[] data, String name) {
try {
final File externalFilesDirectory =
activity.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File shareFile = File.createTempFile("document", ".pdf", externalFilesDirectory);
activity.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS);
File shareFile;
if (name == null) {
shareFile = File.createTempFile("document-", ".pdf", externalFilesDirectory);
} else {
shareFile = new File(externalFilesDirectory, name);
}
FileOutputStream stream = new FileOutputStream(shareFile);
stream.write(data);
... ...
... ... @@ -44,7 +44,7 @@ class MyAppState extends State<MyApp> {
referenceBox.localToGlobal(referenceBox.paintBounds.bottomRight);
final Rect bounds = Rect.fromPoints(topLeft, bottomRight);
Printing.sharePdf(document: pdf, bounds: bounds);
Printing.sharePdf(document: pdf, filename: 'my-résumé.pdf', bounds: bounds);
}
Future<void> _printScreen() async {
... ...
... ... @@ -50,7 +50,8 @@
[[call.arguments objectForKey:@"x"] floatValue],
[[call.arguments objectForKey:@"y"] floatValue],
[[call.arguments objectForKey:@"w"] floatValue],
[[call.arguments objectForKey:@"h"] floatValue])];
[[call.arguments objectForKey:@"h"] floatValue])
andName:[call.arguments objectForKey:@"name"]];
result(@1);
} else {
result(FlutterMethodNotImplemented);
... ... @@ -92,21 +93,27 @@
}
- (void)sharePdf:(nonnull FlutterStandardTypedData*)data
withSourceRect:(CGRect)rect {
withSourceRect:(CGRect)rect
andName:(NSString*)name {
NSURL* tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory()
isDirectory:YES];
CFUUIDRef uuid = CFUUIDCreate(NULL);
assert(uuid != NULL);
assert(uuid != nil);
CFStringRef uuidStr = CFUUIDCreateString(NULL, uuid);
assert(uuidStr != NULL);
NSURL* fileURL = [[tmpDirURL
URLByAppendingPathComponent:[NSString
stringWithFormat:@"pdf-%@", uuidStr]]
URLByAppendingPathExtension:@"pdf"];
assert(fileURL != NULL);
assert(uuidStr != nil);
NSURL* fileURL;
if ([name isEqual:[NSNull null]]) {
fileURL = [[tmpDirURL
URLByAppendingPathComponent:[NSString stringWithFormat:@"document-%@",
uuidStr]]
URLByAppendingPathExtension:@"pdf"];
} else {
fileURL = [tmpDirURL URLByAppendingPathComponent:name];
}
assert(fileURL != nil);
CFRelease(uuidStr);
CFRelease(uuid);
... ...
... ... @@ -65,7 +65,10 @@ mixin Printing {
/// Displays a platform popup to share the Pdf document to another application
static Future<void> sharePdf(
{PdfDocument document, List<int> bytes, Rect bounds}) async {
{PdfDocument document,
List<int> bytes,
String filename,
Rect bounds}) async {
assert(document != null || bytes != null);
assert(!(document == null && bytes == null));
... ... @@ -77,6 +80,7 @@ mixin Printing {
final Map<String, dynamic> params = <String, dynamic>{
'doc': Uint8List.fromList(bytes),
'name': filename,
'x': bounds.left,
'y': bounds.top,
'w': bounds.width,
... ...