Toggle navigation
Toggle navigation
This project
Loading...
Sign in
flutter_package
/
dart_pdf
Go to a project
Toggle navigation
Projects
Groups
Snippets
Help
Toggle navigation pinning
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Authored by
David PHAM-VAN
2020-01-14 09:34:09 -0500
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
9523c45a9d9f81399d6612b71584f8193a5095f2
9523c45a
1 parent
6fba0590
Migrate to the new Android plugins APIs
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
240 additions
and
159 deletions
printing/CHANGELOG.md
printing/android/src/main/java/net/nfet/flutter/printing/PrintingHandler.java
printing/android/src/main/java/net/nfet/flutter/printing/PrintingJob.java
printing/android/src/main/java/net/nfet/flutter/printing/PrintingPlugin.java
printing/pubspec.yaml
printing/CHANGELOG.md
View file @
9523c45
# Changelog
## 3.1.0
-
Migrate to the new Android plugins APIs
## 3.0.2
-
Add Raster PDF to Image
...
...
printing/android/src/main/java/net/nfet/flutter/printing/PrintingHandler.java
0 → 100644
View file @
9523c45
package
net
.
nfet
.
flutter
.
printing
;
import
android.app.Activity
;
import
android.print.PrintAttributes
;
import
androidx.annotation.NonNull
;
import
java.util.HashMap
;
import
io.flutter.plugin.common.MethodCall
;
import
io.flutter.plugin.common.MethodChannel
;
public
class
PrintingHandler
implements
MethodChannel
.
MethodCallHandler
{
private
final
Activity
activity
;
private
final
MethodChannel
channel
;
public
PrintingHandler
(
@NonNull
Activity
activity
,
@NonNull
MethodChannel
channel
)
{
this
.
activity
=
activity
;
this
.
channel
=
channel
;
}
@Override
public
void
onMethodCall
(
@NonNull
MethodCall
call
,
@NonNull
MethodChannel
.
Result
result
)
{
switch
(
call
.
method
)
{
case
"printPdf"
:
{
final
String
name
=
call
.
argument
(
"name"
);
final
Double
width
=
call
.
argument
(
"width"
);
final
Double
height
=
call
.
argument
(
"height"
);
final
Double
marginLeft
=
call
.
argument
(
"marginLeft"
);
final
Double
marginTop
=
call
.
argument
(
"marginTop"
);
final
Double
marginRight
=
call
.
argument
(
"marginRight"
);
final
Double
marginBottom
=
call
.
argument
(
"marginBottom"
);
final
PrintingJob
printJob
=
new
PrintingJob
(
activity
,
this
,
(
int
)
call
.
argument
(
"job"
));
assert
name
!=
null
;
printJob
.
printPdf
(
name
,
width
,
height
,
marginLeft
,
marginTop
,
marginRight
,
marginBottom
);
result
.
success
(
1
);
break
;
}
case
"cancelJob"
:
{
final
PrintingJob
printJob
=
new
PrintingJob
(
activity
,
this
,
(
int
)
call
.
argument
(
"job"
));
printJob
.
cancelJob
();
result
.
success
(
1
);
break
;
}
case
"sharePdf"
:
{
final
byte
[]
document
=
call
.
argument
(
"doc"
);
final
String
name
=
call
.
argument
(
"name"
);
PrintingJob
.
sharePdf
(
activity
,
document
,
name
);
result
.
success
(
1
);
break
;
}
case
"convertHtml"
:
{
Double
width
=
call
.
argument
(
"width"
);
Double
height
=
call
.
argument
(
"height"
);
Double
marginLeft
=
call
.
argument
(
"marginLeft"
);
Double
marginTop
=
call
.
argument
(
"marginTop"
);
Double
marginRight
=
call
.
argument
(
"marginRight"
);
Double
marginBottom
=
call
.
argument
(
"marginBottom"
);
final
PrintingJob
printJob
=
new
PrintingJob
(
activity
,
this
,
(
int
)
call
.
argument
(
"job"
));
assert
width
!=
null
;
assert
height
!=
null
;
assert
marginLeft
!=
null
;
assert
marginTop
!=
null
;
assert
marginRight
!=
null
;
assert
marginBottom
!=
null
;
PrintAttributes
.
Margins
margins
=
new
PrintAttributes
.
Margins
(
Double
.
valueOf
(
marginLeft
*
1000.0
).
intValue
(),
Double
.
valueOf
(
marginTop
*
1000.0
/
72.0
).
intValue
(),
Double
.
valueOf
(
marginRight
*
1000.0
/
72.0
).
intValue
(),
Double
.
valueOf
(
marginBottom
*
1000.0
/
72.0
).
intValue
());
PrintAttributes
.
MediaSize
size
=
new
PrintAttributes
.
MediaSize
(
"flutter_printing"
,
"Provided size"
,
Double
.
valueOf
(
width
*
1000.0
/
72.0
).
intValue
(),
Double
.
valueOf
(
height
*
1000.0
/
72.0
).
intValue
());
printJob
.
convertHtml
((
String
)
call
.
argument
(
"html"
),
size
,
margins
,
(
String
)
call
.
argument
(
"baseUrl"
));
result
.
success
(
1
);
break
;
}
case
"printingInfo"
:
{
result
.
success
(
PrintingJob
.
printingInfo
());
break
;
}
case
"rasterPdf"
:
{
final
byte
[]
document
=
call
.
argument
(
"doc"
);
final
int
[]
pages
=
call
.
argument
(
"pages"
);
Double
scale
=
call
.
argument
(
"scale"
);
final
PrintingJob
printJob
=
new
PrintingJob
(
activity
,
this
,
(
int
)
call
.
argument
(
"job"
));
printJob
.
rasterPdf
(
document
,
pages
,
scale
);
result
.
success
(
1
);
break
;
}
default
:
result
.
notImplemented
();
break
;
}
}
/// Request the Pdf document from flutter
void
onLayout
(
final
PrintingJob
printJob
,
Double
width
,
double
height
,
double
marginLeft
,
double
marginTop
,
double
marginRight
,
double
marginBottom
)
{
HashMap
<
String
,
Object
>
args
=
new
HashMap
<>();
args
.
put
(
"width"
,
width
);
args
.
put
(
"height"
,
height
);
args
.
put
(
"marginLeft"
,
marginLeft
);
args
.
put
(
"marginTop"
,
marginTop
);
args
.
put
(
"marginRight"
,
marginRight
);
args
.
put
(
"marginBottom"
,
marginBottom
);
args
.
put
(
"job"
,
printJob
.
index
);
channel
.
invokeMethod
(
"onLayout"
,
args
,
new
MethodChannel
.
Result
()
{
@Override
public
void
success
(
Object
result
)
{
if
(
result
instanceof
byte
[])
{
printJob
.
setDocument
((
byte
[])
result
);
}
else
{
printJob
.
cancelJob
();
}
}
@Override
public
void
error
(
String
errorCode
,
String
errorMessage
,
Object
errorDetails
)
{
printJob
.
cancelJob
();
}
@Override
public
void
notImplemented
()
{
printJob
.
cancelJob
();
}
});
}
/// send completion status to flutter
void
onCompleted
(
PrintingJob
printJob
,
boolean
completed
,
String
error
)
{
HashMap
<
String
,
Object
>
args
=
new
HashMap
<>();
args
.
put
(
"completed"
,
completed
);
args
.
put
(
"error"
,
error
);
args
.
put
(
"job"
,
printJob
.
index
);
channel
.
invokeMethod
(
"onCompleted"
,
args
);
}
/// send html to pdf data result to flutter
void
onHtmlRendered
(
PrintingJob
printJob
,
byte
[]
pdfData
)
{
HashMap
<
String
,
Object
>
args
=
new
HashMap
<>();
args
.
put
(
"doc"
,
pdfData
);
args
.
put
(
"job"
,
printJob
.
index
);
channel
.
invokeMethod
(
"onHtmlRendered"
,
args
);
}
/// send html to pdf conversion error to flutter
void
onHtmlError
(
PrintingJob
printJob
,
String
error
)
{
HashMap
<
String
,
Object
>
args
=
new
HashMap
<>();
args
.
put
(
"error"
,
error
);
args
.
put
(
"job"
,
printJob
.
index
);
channel
.
invokeMethod
(
"onHtmlError"
,
args
);
}
/// send pdf to raster data result to flutter
void
onPageRasterized
(
PrintingJob
printJob
,
byte
[]
imageData
,
int
width
,
int
height
)
{
HashMap
<
String
,
Object
>
args
=
new
HashMap
<>();
args
.
put
(
"image"
,
imageData
);
args
.
put
(
"width"
,
width
);
args
.
put
(
"height"
,
height
);
args
.
put
(
"job"
,
printJob
.
index
);
channel
.
invokeMethod
(
"onPageRasterized"
,
args
);
}
void
onPageRasterEnd
(
PrintingJob
printJob
)
{
HashMap
<
String
,
Object
>
args
=
new
HashMap
<>();
args
.
put
(
"job"
,
printJob
.
index
);
channel
.
invokeMethod
(
"onPageRasterEnd"
,
args
);
}
}
...
...
printing/android/src/main/java/net/nfet/flutter/printing/PrintingJob.java
View file @
9523c45
...
...
@@ -58,14 +58,14 @@ import java.util.HashMap;
public
class
PrintingJob
extends
PrintDocumentAdapter
{
private
static
PrintManager
printManager
;
private
final
Context
context
;
private
final
Printing
Plugin
printing
;
private
final
Printing
Handler
printing
;
private
PrintJob
printJob
;
private
byte
[]
documentData
;
private
String
jobName
;
private
LayoutResultCallback
callback
;
int
index
;
PrintingJob
(
Context
context
,
Printing
Plugin
printing
,
int
index
)
{
PrintingJob
(
Context
context
,
Printing
Handler
printing
,
int
index
)
{
this
.
context
=
context
;
this
.
printing
=
printing
;
this
.
index
=
index
;
...
...
printing/android/src/main/java/net/nfet/flutter/printing/PrintingPlugin.java
View file @
9523c45
...
...
@@ -17,12 +17,18 @@
package
net
.
nfet
.
flutter
.
printing
;
import
android.app.Activity
;
import
android.content.Context
;
import
android.print.PrintAttributes
;
import
androidx.annotation.NonNull
;
import
java.util.HashMap
;
import
io.flutter.embedding.engine.plugins.FlutterPlugin
;
import
io.flutter.embedding.engine.plugins.activity.ActivityAware
;
import
io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
;
import
io.flutter.plugin.common.BinaryMessenger
;
import
io.flutter.plugin.common.EventChannel
;
import
io.flutter.plugin.common.MethodCall
;
import
io.flutter.plugin.common.MethodChannel
;
import
io.flutter.plugin.common.MethodChannel.MethodCallHandler
;
...
...
@@ -32,17 +38,14 @@ import io.flutter.plugin.common.PluginRegistry.Registrar;
/**
* PrintingPlugin
*/
public
class
PrintingPlugin
implements
MethodCallHandler
{
private
final
Activity
activity
;
private
final
MethodChannel
channel
;
private
PrintingPlugin
(
@NonNull
Activity
activity
,
@NonNull
MethodChannel
channel
)
{
this
.
activity
=
activity
;
this
.
channel
=
channel
;
}
public
class
PrintingPlugin
implements
FlutterPlugin
,
ActivityAware
{
private
Activity
activity
;
private
MethodChannel
channel
;
private
PrintingHandler
handler
;
/**
* Plugin registration.
* for legacy Embedding API
*/
public
static
void
registerWith
(
Registrar
registrar
)
{
Activity
activity
=
registrar
.
activity
();
...
...
@@ -50,176 +53,60 @@ public class PrintingPlugin implements MethodCallHandler {
return
;
// We can't print without an activity
}
final
MethodChannel
channel
=
new
MethodChannel
(
registrar
.
messenger
(),
"net.nfet.printing"
);
channel
.
setMethodCallHandler
(
new
PrintingPlugin
(
activity
,
channel
));
final
PrintingPlugin
instance
=
new
PrintingPlugin
();
instance
.
onAttachedToEngine
(
registrar
.
messenger
());
instance
.
onAttachedToActivity
(
activity
);
}
@Override
public
void
onMethodCall
(
@NonNull
MethodCall
call
,
@NonNull
Result
result
)
{
switch
(
call
.
method
)
{
case
"printPdf"
:
{
final
String
name
=
call
.
argument
(
"name"
);
final
Double
width
=
call
.
argument
(
"width"
);
final
Double
height
=
call
.
argument
(
"height"
);
final
Double
marginLeft
=
call
.
argument
(
"marginLeft"
);
final
Double
marginTop
=
call
.
argument
(
"marginTop"
);
final
Double
marginRight
=
call
.
argument
(
"marginRight"
);
final
Double
marginBottom
=
call
.
argument
(
"marginBottom"
);
final
PrintingJob
printJob
=
new
PrintingJob
(
activity
,
this
,
(
int
)
call
.
argument
(
"job"
));
assert
name
!=
null
;
printJob
.
printPdf
(
name
,
width
,
height
,
marginLeft
,
marginTop
,
marginRight
,
marginBottom
);
result
.
success
(
1
);
break
;
}
case
"cancelJob"
:
{
final
PrintingJob
printJob
=
new
PrintingJob
(
activity
,
this
,
(
int
)
call
.
argument
(
"job"
));
printJob
.
cancelJob
();
result
.
success
(
1
);
break
;
}
case
"sharePdf"
:
{
final
byte
[]
document
=
call
.
argument
(
"doc"
);
final
String
name
=
call
.
argument
(
"name"
);
PrintingJob
.
sharePdf
(
activity
,
document
,
name
);
result
.
success
(
1
);
break
;
}
case
"convertHtml"
:
{
Double
width
=
call
.
argument
(
"width"
);
Double
height
=
call
.
argument
(
"height"
);
Double
marginLeft
=
call
.
argument
(
"marginLeft"
);
Double
marginTop
=
call
.
argument
(
"marginTop"
);
Double
marginRight
=
call
.
argument
(
"marginRight"
);
Double
marginBottom
=
call
.
argument
(
"marginBottom"
);
final
PrintingJob
printJob
=
new
PrintingJob
(
activity
,
this
,
(
int
)
call
.
argument
(
"job"
));
assert
width
!=
null
;
assert
height
!=
null
;
assert
marginLeft
!=
null
;
assert
marginTop
!=
null
;
assert
marginRight
!=
null
;
assert
marginBottom
!=
null
;
PrintAttributes
.
Margins
margins
=
new
PrintAttributes
.
Margins
(
Double
.
valueOf
(
marginLeft
*
1000.0
).
intValue
(),
Double
.
valueOf
(
marginTop
*
1000.0
/
72.0
).
intValue
(),
Double
.
valueOf
(
marginRight
*
1000.0
/
72.0
).
intValue
(),
Double
.
valueOf
(
marginBottom
*
1000.0
/
72.0
).
intValue
());
PrintAttributes
.
MediaSize
size
=
new
PrintAttributes
.
MediaSize
(
"flutter_printing"
,
"Provided size"
,
Double
.
valueOf
(
width
*
1000.0
/
72.0
).
intValue
(),
Double
.
valueOf
(
height
*
1000.0
/
72.0
).
intValue
());
printJob
.
convertHtml
((
String
)
call
.
argument
(
"html"
),
size
,
margins
,
(
String
)
call
.
argument
(
"baseUrl"
));
result
.
success
(
1
);
break
;
}
case
"printingInfo"
:
{
result
.
success
(
PrintingJob
.
printingInfo
());
break
;
}
case
"rasterPdf"
:
{
final
byte
[]
document
=
call
.
argument
(
"doc"
);
final
int
[]
pages
=
call
.
argument
(
"pages"
);
Double
scale
=
call
.
argument
(
"scale"
);
final
PrintingJob
printJob
=
new
PrintingJob
(
activity
,
this
,
(
int
)
call
.
argument
(
"job"
));
printJob
.
rasterPdf
(
document
,
pages
,
scale
);
result
.
success
(
1
);
break
;
}
default
:
result
.
notImplemented
();
break
;
}
public
void
onAttachedToEngine
(
FlutterPluginBinding
binding
)
{
onAttachedToEngine
(
binding
.
getBinaryMessenger
());
}
/// Request the Pdf document from flutter
void
onLayout
(
final
PrintingJob
printJob
,
Double
width
,
double
height
,
double
marginLeft
,
double
marginTop
,
double
marginRight
,
double
marginBottom
)
{
HashMap
<
String
,
Object
>
args
=
new
HashMap
<>();
args
.
put
(
"width"
,
width
);
args
.
put
(
"height"
,
height
);
private
void
onAttachedToEngine
(
BinaryMessenger
messenger
)
{
channel
=
new
MethodChannel
(
messenger
,
"net.nfet.printing"
);
args
.
put
(
"marginLeft"
,
marginLeft
);
args
.
put
(
"marginTop"
,
marginTop
);
args
.
put
(
"marginRight"
,
marginRight
);
args
.
put
(
"marginBottom"
,
marginBottom
);
args
.
put
(
"job"
,
printJob
.
index
);
channel
.
invokeMethod
(
"onLayout"
,
args
,
new
Result
()
{
@Override
public
void
success
(
Object
result
)
{
if
(
result
instanceof
byte
[])
{
printJob
.
setDocument
((
byte
[])
result
);
}
else
{
printJob
.
cancelJob
();
if
(
activity
!=
null
)
{
handler
=
new
PrintingHandler
(
activity
,
channel
);
channel
.
setMethodCallHandler
(
handler
);
}
}
@Override
public
void
error
(
String
errorCode
,
String
errorMessage
,
Object
errorDetails
)
{
printJob
.
cancelJob
();
public
void
onDetachedFromEngine
(
FlutterPluginBinding
binding
)
{
channel
.
setMethodCallHandler
(
null
);
channel
=
null
;
handler
=
null
;
}
@Override
public
void
notImplemented
()
{
printJob
.
cancelJob
();
public
void
onAttachedToActivity
(
ActivityPluginBinding
binding
)
{
onAttachedToActivity
(
binding
.
getActivity
());
}
});
}
/// send completion status to flutter
void
onCompleted
(
PrintingJob
printJob
,
boolean
completed
,
String
error
)
{
HashMap
<
String
,
Object
>
args
=
new
HashMap
<>();
args
.
put
(
"completed"
,
completed
);
args
.
put
(
"error"
,
error
);
args
.
put
(
"job"
,
printJob
.
index
);
private
void
onAttachedToActivity
(
Activity
_activity
)
{
activity
=
_activity
;
channel
.
invokeMethod
(
"onCompleted"
,
args
);
if
(
activity
!=
null
&&
channel
!=
null
)
{
handler
=
new
PrintingHandler
(
activity
,
channel
);
channel
.
setMethodCallHandler
(
handler
);
}
/// send html to pdf data result to flutter
void
onHtmlRendered
(
PrintingJob
printJob
,
byte
[]
pdfData
)
{
HashMap
<
String
,
Object
>
args
=
new
HashMap
<>();
args
.
put
(
"doc"
,
pdfData
);
args
.
put
(
"job"
,
printJob
.
index
);
channel
.
invokeMethod
(
"onHtmlRendered"
,
args
);
}
/// send html to pdf conversion error to flutter
void
onHtmlError
(
PrintingJob
printJob
,
String
error
)
{
HashMap
<
String
,
Object
>
args
=
new
HashMap
<>();
args
.
put
(
"error"
,
error
);
args
.
put
(
"job"
,
printJob
.
index
);
channel
.
invokeMethod
(
"onHtmlError"
,
args
);
@Override
public
void
onDetachedFromActivityForConfigChanges
()
{
onDetachedFromActivity
();
}
/// send pdf to raster data result to flutter
void
onPageRasterized
(
PrintingJob
printJob
,
byte
[]
imageData
,
int
width
,
int
height
)
{
HashMap
<
String
,
Object
>
args
=
new
HashMap
<>();
args
.
put
(
"image"
,
imageData
);
args
.
put
(
"width"
,
width
);
args
.
put
(
"height"
,
height
);
args
.
put
(
"job"
,
printJob
.
index
);
channel
.
invokeMethod
(
"onPageRasterized"
,
args
);
@Override
public
void
onReattachedToActivityForConfigChanges
(
ActivityPluginBinding
binding
)
{
onAttachedToActivity
(
binding
.
getActivity
());
}
void
onPageRasterEnd
(
PrintingJob
printJob
)
{
HashMap
<
String
,
Object
>
args
=
new
HashMap
<>();
args
.
put
(
"job"
,
printJob
.
index
);
channel
.
invokeMethod
(
"onPageRasterEnd"
,
args
);
@Override
public
void
onDetachedFromActivity
()
{
channel
.
setMethodCallHandler
(
null
);
activity
=
null
;
handler
=
null
;
}
}
...
...
printing/pubspec.yaml
View file @
9523c45
...
...
@@ -4,7 +4,7 @@ description: Plugin that allows Flutter apps to generate and print documents to
homepage
:
https://github.com/DavBfr/dart_pdf/tree/master/printing
repository
:
https://github.com/DavBfr/dart_pdf
issue_tracker
:
https://github.com/DavBfr/dart_pdf/issues
version
:
3.
0.2
version
:
3.
1.0
environment
:
sdk
:
"
>=2.3.0
<3.0.0"
...
...
Please
register
or
login
to post a comment