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
2021-01-02 12:16:08 -0400
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
8bb80e90537fc67d3f9b8ef12b71515aea915d23
8bb80e90
1 parent
86a040a8
Improve macOS directPrint
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
97 additions
and
51 deletions
printing/lib/src/method_channel.dart
printing/lib/src/printer.dart
printing/lib/src/printing.dart
printing/lib/src/printing_info.dart
printing/macos/Classes/PrintJob.swift
printing/macos/Classes/PrintingPlugin.swift
printing/lib/src/method_channel.dart
View file @
8bb80e9
...
...
@@ -160,15 +160,7 @@ class MethodChannelPrinting extends PrintingPlatform {
final
printers
=
<
Printer
>[];
for
(
final
printer
in
list
)
{
printers
.
add
(
Printer
(
url:
printer
[
'url'
],
name:
printer
[
'name'
],
model:
printer
[
'model'
],
location:
printer
[
'location'
],
comment:
printer
[
'comment'
],
isDefault:
printer
[
'default'
],
available:
printer
[
'available'
],
));
printers
.
add
(
Printer
.
fromMap
(
printer
));
}
return
printers
;
...
...
@@ -187,12 +179,7 @@ class MethodChannelPrinting extends PrintingPlatform {
if
(
printer
==
null
)
{
return
null
;
}
return
Printer
(
url:
printer
[
'url'
],
name:
printer
[
'name'
],
model:
printer
[
'model'
],
location:
printer
[
'location'
],
);
return
Printer
.
fromMap
(
printer
);
}
@override
...
...
@@ -215,6 +202,12 @@ class MethodChannelPrinting extends PrintingPlatform {
'name'
:
name
,
'printer'
:
printer
.
url
,
'doc'
:
bytes
,
'width'
:
format
.
width
,
'height'
:
format
.
height
,
'marginLeft'
:
format
.
marginLeft
,
'marginTop'
:
format
.
marginTop
,
'marginRight'
:
format
.
marginRight
,
'marginBottom'
:
format
.
marginBottom
,
'job'
:
job
.
index
,
};
await
_channel
.
invokeMethod
<
int
>(
'directPrintPdf'
,
params
);
...
...
printing/lib/src/printer.dart
View file @
8bb80e9
...
...
@@ -22,13 +22,27 @@ class Printer {
/// Create a printer information
const
Printer
({
@required
this
.
url
,
this
.
name
,
String
name
,
this
.
model
,
this
.
location
,
this
.
comment
,
this
.
isDefault
=
false
,
this
.
available
=
true
,
})
:
assert
(
url
!=
null
);
bool
isDefault
,
bool
isAvailable
,
})
:
assert
(
url
!=
null
),
name
=
name
??
url
,
isDefault
=
isDefault
??
false
,
isAvailable
=
isAvailable
??
true
;
/// Create an information object from a dictionnary
factory
Printer
.
fromMap
(
Map
<
dynamic
,
dynamic
>
map
)
=>
Printer
(
url:
map
[
'url'
],
name:
map
[
'name'
],
model:
map
[
'model'
],
location:
map
[
'location'
],
comment:
map
[
'comment'
],
isDefault:
map
[
'default'
],
isAvailable:
map
[
'available'
],
);
/// The platform specific printer identification
final
String
url
;
...
...
@@ -49,8 +63,14 @@ class Printer {
final
bool
isDefault
;
/// The printer is available for printing
final
bool
a
vailable
;
final
bool
isA
vailable
;
@override
String
toString
()
=>
name
??
url
;
String
toString
()
=>
'''
$runtimeType
$name
url:
$url
location:
$location
model:
$model
comment:
$comment
isDefault:
$isDefault
isAvailable:
$isAvailable
'''
;
}
...
...
printing/lib/src/printing.dart
View file @
8bb80e9
...
...
@@ -91,7 +91,7 @@ mixin Printing {
title:
Text
(
title
??
'Select Printer'
),
children:
[
for
(
final
printer
in
printers
)
if
(
printer
.
a
vailable
)
if
(
printer
.
isA
vailable
)
SimpleDialogOption
(
child:
Text
(
printer
.
name
,
...
...
printing/lib/src/printing_info.dart
View file @
8bb80e9
...
...
@@ -72,7 +72,7 @@ class PrintingInfo {
final
bool
canRaster
;
@override
String
toString
()
=>
'''
PrintingInfo
:
String
toString
()
=>
'''
$runtimeType
:
canPrint:
$canPrint
directPrint:
$directPrint
dynamicLayout:
$dynamicLayout
...
...
@@ -80,17 +80,4 @@ class PrintingInfo {
canListPrinters:
$canListPrinters
canShare:
$canShare
canRaster:
$canRaster
'''
;
/// Returns a map representation of this object
Map
<
dynamic
,
dynamic
>
asMap
()
{
return
<
dynamic
,
dynamic
>{
'canPrint'
:
canPrint
,
'directPrint'
:
directPrint
,
'dynamicLayout'
:
dynamicLayout
,
'canConvertHtml'
:
canConvertHtml
,
'canListPrinters'
:
canListPrinters
,
'canShare'
:
canShare
,
'canRaster'
:
canRaster
,
};
}
}
...
...
printing/macos/Classes/PrintJob.swift
View file @
8bb80e9
...
...
@@ -43,8 +43,10 @@ public class PrintJob: NSView, NSSharingServicePickerDelegate {
// Return the number of pages available for printing
override
public
func
knowsPageRange
(
_
range
:
NSRangePointer
)
->
Bool
{
setFrameSize
(
printOperation
!.
printPanel
.
printInfo
.
paperSize
)
setBoundsSize
(
printOperation
!.
printPanel
.
printInfo
.
paperSize
)
let
size
=
printOperation
!.
showsPrintPanel
?
printOperation
!.
printPanel
.
printInfo
.
paperSize
:
printOperation
!.
printInfo
.
paperSize
setFrameSize
(
size
)
setBoundsSize
(
size
)
range
.
pointee
.
length
=
pdfDocument
?
.
numberOfPages
??
0
return
true
}
...
...
@@ -78,7 +80,47 @@ public class PrintJob: NSView, NSSharingServicePickerDelegate {
}
}
public
func
directPrintPdf
(
name
_
:
String
,
data
_
:
Data
,
withPrinter
_
:
String
)
{}
public
func
listPrinters
()
->
[
NSDictionary
]
{
var
printers
:
Array
=
[
NSDictionary
]()
for
name
in
NSPrinter
.
printerNames
{
let
printer
=
NSPrinter
(
name
:
name
)
let
pr
:
NSDictionary
=
[
"url"
:
name
,
"name"
:
name
,
"model"
:
printer
!.
type
,
]
printers
.
append
(
pr
)
}
return
printers
}
public
func
directPrintPdf
(
name
:
String
,
data
:
Data
,
withPrinter
printer
:
String
,
width
:
CGFloat
,
height
:
CGFloat
)
{
let
sharedInfo
=
NSPrintInfo
.
shared
let
sharedDict
=
sharedInfo
.
dictionary
()
let
printInfoDict
=
NSMutableDictionary
(
dictionary
:
sharedDict
)
let
printInfo
=
NSPrintInfo
(
dictionary
:
printInfoDict
as!
[
NSPrintInfo
.
AttributeKey
:
Any
])
printInfo
.
printer
=
NSPrinter
(
name
:
printer
)
!
let
bytesPointer
=
UnsafeMutablePointer
<
UInt8
>.
allocate
(
capacity
:
data
.
count
)
data
.
copyBytes
(
to
:
bytesPointer
,
count
:
data
.
count
)
let
dataProvider
=
CGDataProvider
(
dataInfo
:
nil
,
data
:
bytesPointer
,
size
:
data
.
count
,
releaseData
:
dataProviderReleaseDataCallback
)
pdfDocument
=
CGPDFDocument
(
dataProvider
!
)
printInfo
.
paperSize
=
NSSize
(
width
:
width
,
height
:
height
)
if
width
>
height
{
printInfo
.
orientation
=
NSPrintInfo
.
PaperOrientation
.
landscape
}
// Print the custom view
printOperation
=
NSPrintOperation
(
view
:
self
,
printInfo
:
printInfo
)
printOperation
!.
jobTitle
=
name
printOperation
!.
showsPrintPanel
=
false
printOperation
!.
showsProgressPanel
=
false
let
window
=
NSApplication
.
shared
.
mainWindow
!
printOperation
!.
runModal
(
for
:
window
,
delegate
:
self
,
didRun
:
#selector(
printOperationDidRun(printOperation:success:contextInfo:)
)
,
contextInfo
:
nil
)
}
public
func
printPdf
(
name
:
String
,
withPageSize
size
:
CGSize
,
andMargin
_
:
CGRect
)
{
let
sharedInfo
=
NSPrintInfo
.
shared
...
...
@@ -86,6 +128,7 @@ public class PrintJob: NSView, NSSharingServicePickerDelegate {
let
printInfoDict
=
NSMutableDictionary
(
dictionary
:
sharedDict
)
let
printInfo
=
NSPrintInfo
(
dictionary
:
printInfoDict
as!
[
NSPrintInfo
.
AttributeKey
:
Any
])
printInfo
.
paperSize
=
size
if
size
.
width
>
size
.
height
{
printInfo
.
orientation
=
NSPrintInfo
.
PaperOrientation
.
landscape
}
...
...
@@ -171,10 +214,6 @@ public class PrintJob: NSView, NSSharingServicePickerDelegate {
}
}
public
static
func
pickPrinter
(
result
:
@escaping
FlutterResult
,
withSourceRect
_
:
CGRect
)
{
result
(
NSNumber
(
value
:
1
))
}
public
func
rasterPdf
(
data
:
Data
,
pages
:
[
Int
]?,
scale
:
CGFloat
)
{
let
provider
=
CGDataProvider
(
data
:
data
as
CFData
)
!
let
document
=
CGPDFDocument
(
provider
)
!
...
...
@@ -220,12 +259,13 @@ public class PrintJob: NSView, NSSharingServicePickerDelegate {
public
static
func
printingInfo
()
->
NSDictionary
{
let
data
:
NSDictionary
=
[
"directPrint"
:
fals
e
,
"directPrint"
:
tru
e
,
"dynamicLayout"
:
false
,
"canPrint"
:
true
,
"canConvertHtml"
:
true
,
"canShare"
:
true
,
"canRaster"
:
true
,
"canListPrinters"
:
true
,
]
return
data
}
...
...
printing/macos/Classes/PrintingPlugin.swift
View file @
8bb80e9
...
...
@@ -56,13 +56,19 @@ public class PrintingPlugin: NSObject, FlutterPlugin {
height
:
height
-
marginBottom
-
marginTop
))
result
(
NSNumber
(
value
:
1
))
// } else if call.method == "directPrintPdf" {
// let name = args["name"] as! String
// let printer = args["printer"] as! String
// let object = args["doc"] as! FlutterStandardTypedData
// let printJob = PrintJob(printing: self, index: args["job"] as! Int)
// printJob.directPrintPdf(name: name, data: object.data, withPrinter: printer)
// result(NSNumber(value: 1))
}
else
if
call
.
method
==
"listPrinters"
{
let
printJob
=
PrintJob
(
printing
:
self
,
index
:
-
1
)
let
r
=
printJob
.
listPrinters
()
result
(
r
)
}
else
if
call
.
method
==
"directPrintPdf"
{
let
name
=
args
[
"name"
]
as!
String
let
printer
=
args
[
"printer"
]
as!
String
let
object
=
args
[
"doc"
]
as!
FlutterStandardTypedData
let
width
=
CGFloat
((
args
[
"width"
]
as?
NSNumber
)?
.
floatValue
??
0.0
)
let
height
=
CGFloat
((
args
[
"height"
]
as?
NSNumber
)?
.
floatValue
??
0.0
)
let
printJob
=
PrintJob
(
printing
:
self
,
index
:
args
[
"job"
]
as!
Int
)
printJob
.
directPrintPdf
(
name
:
name
,
data
:
object
.
data
,
withPrinter
:
printer
,
width
:
width
,
height
:
height
)
result
(
NSNumber
(
1
))
}
else
if
call
.
method
==
"sharePdf"
{
let
object
=
args
[
"doc"
]
as!
FlutterStandardTypedData
PrintJob
.
sharePdf
(
...
...
Please
register
or
login
to post a comment