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-02-19 08:22:24 -0400
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
f6e646a235a65bb57c1f02ba41d330b0a5dec826
f6e646a2
1 parent
1de51386
Review directPrint internals
Show whitespace changes
Inline
Side-by-side
Showing
13 changed files
with
133 additions
and
236 deletions
printing/CHANGELOG.md
printing/ios/Classes/PrintJob.swift
printing/ios/Classes/PrintingPlugin.swift
printing/lib/src/method_channel.dart
printing/lib/src/printing_web.dart
printing/linux/print_job.cc
printing/linux/print_job.h
printing/linux/printing_plugin.cc
printing/macos/Classes/PrintJob.swift
printing/macos/Classes/PrintingPlugin.swift
printing/windows/print_job.cpp
printing/windows/print_job.h
printing/windows/printing_plugin.cpp
printing/CHANGELOG.md
View file @
f6e646a
...
...
@@ -6,6 +6,7 @@
-
Add Page orientation on PdfPreview
-
Improve PrintJob object
-
Implement dynamic layout on iOS and macOS
-
Review directPrint internals
## 5.0.0-nullsafety.1
...
...
printing/ios/Classes/PrintJob.swift
View file @
f6e646a
...
...
@@ -87,32 +87,7 @@ public class PrintJob: UIPrintPageRenderer, UIPrintInteractionControllerDelegate
printing
.
onCompleted
(
printJob
:
self
,
completed
:
completed
,
error
:
error
?
.
localizedDescription
as
NSString
?)
}
func
directPrintPdf
(
name
:
String
,
data
:
Data
,
withPrinter
printerID
:
String
)
{
let
printing
=
UIPrintInteractionController
.
isPrintingAvailable
if
!
printing
{
self
.
printing
.
onCompleted
(
printJob
:
self
,
completed
:
false
,
error
:
"Printing not available"
)
return
}
let
controller
=
UIPrintInteractionController
.
shared
let
printInfo
=
UIPrintInfo
.
printInfo
()
printInfo
.
jobName
=
name
printInfo
.
outputType
=
.
general
controller
.
printInfo
=
printInfo
controller
.
printingItem
=
data
let
printerURL
=
URL
(
string
:
printerID
)
if
printerURL
==
nil
{
self
.
printing
.
onCompleted
(
printJob
:
self
,
completed
:
false
,
error
:
"Unable to find printer URL"
)
return
}
let
printer
=
UIPrinter
(
url
:
printerURL
!
)
controller
.
print
(
to
:
printer
,
completionHandler
:
completionHandler
)
}
func
printPdf
(
name
:
String
,
withPageSize
size
:
CGSize
,
andMargin
_
:
CGRect
)
{
func
printPdf
(
name
:
String
,
withPageSize
size
:
CGSize
,
andMargin
_
:
CGRect
,
withPrinter
printerID
:
String
?)
{
let
printing
=
UIPrintInteractionController
.
isPrintingAvailable
if
!
printing
{
self
.
printing
.
onCompleted
(
printJob
:
self
,
completed
:
false
,
error
:
"Printing not available"
)
...
...
@@ -139,6 +114,20 @@ public class PrintJob: UIPrintPageRenderer, UIPrintInteractionControllerDelegate
controller
.
showsPaperSelectionForLoadedPapers
=
true
controller
.
printPageRenderer
=
self
if
printerID
!=
nil
{
let
printerURL
=
URL
(
string
:
printerID
!
)
if
printerURL
==
nil
{
self
.
printing
.
onCompleted
(
printJob
:
self
,
completed
:
false
,
error
:
"Unable to find printer URL"
)
return
}
let
printer
=
UIPrinter
(
url
:
printerURL
!
)
controller
.
print
(
to
:
printer
,
completionHandler
:
completionHandler
)
return
}
controller
.
present
(
animated
:
true
,
completionHandler
:
completionHandler
)
}
...
...
printing/ios/Classes/PrintingPlugin.swift
View file @
f6e646a
...
...
@@ -54,12 +54,13 @@ public class PrintingPlugin: NSObject, FlutterPlugin {
let
args
=
call
.
arguments
!
as!
[
String
:
Any
]
if
call
.
method
==
"printPdf"
{
let
name
=
args
[
"name"
]
as!
String
let
width
=
CGFloat
((
args
[
"width"
]
as?
NSNumber
)?
.
floatValue
??
0.0
)
let
height
=
CGFloat
((
args
[
"height"
]
as?
NSNumber
)?
.
floatValue
??
0.0
)
let
marginLeft
=
CGFloat
((
args
[
"marginLeft"
]
as?
NSNumber
)?
.
floatValue
??
0.0
)
let
marginTop
=
CGFloat
((
args
[
"marginTop"
]
as?
NSNumber
)?
.
floatValue
??
0.0
)
let
marginRight
=
CGFloat
((
args
[
"marginRight"
]
as?
NSNumber
)?
.
floatValue
??
0.0
)
let
marginBottom
=
CGFloat
((
args
[
"marginBottom"
]
as?
NSNumber
)?
.
floatValue
??
0.0
)
let
printer
=
args
[
"printer"
]
as?
String
let
width
=
CGFloat
((
args
[
"width"
]
as!
NSNumber
)
.
floatValue
)
let
height
=
CGFloat
((
args
[
"height"
]
as!
NSNumber
)
.
floatValue
)
let
marginLeft
=
CGFloat
((
args
[
"marginLeft"
]
as!
NSNumber
)
.
floatValue
)
let
marginTop
=
CGFloat
((
args
[
"marginTop"
]
as!
NSNumber
)
.
floatValue
)
let
marginRight
=
CGFloat
((
args
[
"marginRight"
]
as!
NSNumber
)
.
floatValue
)
let
marginBottom
=
CGFloat
((
args
[
"marginBottom"
]
as!
NSNumber
)
.
floatValue
)
let
printJob
=
PrintJob
(
printing
:
self
,
index
:
args
[
"job"
]
as!
Int
)
jobs
[
args
[
"job"
]
as!
UInt32
]
=
printJob
printJob
.
printPdf
(
name
:
name
,
...
...
@@ -72,14 +73,7 @@ public class PrintingPlugin: NSObject, FlutterPlugin {
y
:
marginTop
,
width
:
width
-
marginRight
-
marginLeft
,
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
)
),
withPrinter
:
printer
)
result
(
NSNumber
(
value
:
1
))
}
else
if
call
.
method
==
"sharePdf"
{
let
object
=
args
[
"doc"
]
as!
FlutterStandardTypedData
...
...
printing/lib/src/method_channel.dart
View file @
f6e646a
...
...
@@ -207,14 +207,12 @@ class MethodChannelPrinting extends PrintingPlatform {
)
async
{
final
job
=
_printJobs
.
add
(
onCompleted:
Completer
<
bool
>(),
onLayout:
onLayout
,
);
final
bytes
=
await
onLayout
(
format
);
final
params
=
<
String
,
dynamic
>{
'name'
:
name
,
'printer'
:
printer
.
url
,
'doc'
:
bytes
,
'width'
:
format
.
width
,
'height'
:
format
.
height
,
'marginLeft'
:
format
.
marginLeft
,
...
...
@@ -223,10 +221,12 @@ class MethodChannelPrinting extends PrintingPlatform {
'marginBottom'
:
format
.
marginBottom
,
'job'
:
job
.
index
,
};
await
_channel
.
invokeMethod
<
int
>(
'directPrintPdf'
,
params
);
final
result
=
await
job
.
onCompleted
!.
future
;
await
_channel
.
invokeMethod
<
int
>(
'printPdf'
,
params
);
try
{
return
await
job
.
onCompleted
!.
future
;
}
finally
{
_printJobs
.
remove
(
job
.
index
);
return
result
;
}
}
@override
...
...
printing/lib/src/printing_web.dart
View file @
f6e646a
...
...
@@ -166,7 +166,7 @@ class PrintingPlugin extends PrintingPlatform {
String
name
,
PdfPageFormat
format
,
)
{
throw
UnimplementedError
(
);
return
layoutPdf
(
onLayout
,
name
,
format
);
}
@override
...
...
printing/linux/print_job.cc
View file @
f6e646a
...
...
@@ -109,15 +109,33 @@ static void job_completed(GtkPrintJob* gtk_print_job,
}
bool
print_job
::
print_pdf
(
const
gchar
*
name
,
const
gchar
*
printer
,
double
pageWidth
,
double
pageHeight
,
double
marginLeft
,
double
marginTop
,
double
marginRight
,
double
marginBottom
)
{
documentName
=
strdup
(
name
);
GtkPrintSettings
*
settings
;
GtkPageSetup
*
setup
;
if
(
printer
!=
nullptr
)
{
_printer
=
nullptr
;
auto
pname
=
strdup
(
printer
);
gtk_enumerate_printers
(
search_printer
,
pname
,
nullptr
,
true
);
free
(
pname
);
if
(
!
_printer
)
{
on_completed
(
this
,
false
,
"Printer not found"
);
return
false
;
}
settings
=
gtk_print_settings_new
();
setup
=
gtk_page_setup_new
();
}
else
{
auto
dialog
=
GTK_PRINT_UNIX_DIALOG
(
gtk_print_unix_dialog_new
(
documentN
ame
,
nullptr
));
GTK_PRINT_UNIX_DIALOG
(
gtk_print_unix_dialog_new
(
n
ame
,
nullptr
));
gtk_print_unix_dialog_set_manual_capabilities
(
dialog
,
(
GtkPrintCapabilities
)(
GTK_PRINT_CAPABILITY_GENERATE_PDF
));
...
...
@@ -128,46 +146,49 @@ bool print_job::print_pdf(const gchar* name,
switch
(
response
)
{
case
GTK_RESPONSE_OK
:
{
GtkPrinter
*
printer
=
gtk_print_unix_dialog_get_selected_printer
(
_
printer
=
gtk_print_unix_dialog_get_selected_printer
(
GTK_PRINT_UNIX_DIALOG
(
dialog
));
if
(
!
gtk_printer_accepts_pdf
(
printer
))
{
on_completed
(
this
,
false
,
"This printer does not accept PDF jobs"
);
break
;
}
GtkPrintSettings
*
settings
=
settings
=
gtk_print_unix_dialog_get_settings
(
GTK_PRINT_UNIX_DIALOG
(
dialog
));
GtkPageSetup
*
setup
=
setup
=
gtk_print_unix_dialog_get_page_setup
(
GTK_PRINT_UNIX_DIALOG
(
dialog
));
auto
width
=
gtk_page_setup_get_page_width
(
setup
,
GTK_UNIT_POINTS
);
auto
height
=
gtk_page_setup_get_page_height
(
setup
,
GTK_UNIT_POINTS
);
auto
marginLeft
=
gtk_page_setup_get_left_margin
(
setup
,
GTK_UNIT_POINTS
);
auto
marginTop
=
gtk_page_setup_get_top_margin
(
setup
,
GTK_UNIT_POINTS
);
auto
marginRight
=
gtk_page_setup_get_right_margin
(
setup
,
GTK_UNIT_POINTS
);
auto
marginBottom
=
gtk_page_setup_get_bottom_margin
(
setup
,
GTK_UNIT_POINTS
);
printJob
=
gtk_print_job_new
(
name
,
printer
,
settings
,
setup
);
on_layout
(
this
,
width
,
height
,
marginLeft
,
marginTop
,
marginRight
,
marginBottom
);
g_object_unref
(
settings
);
gtk_widget_destroy
(
GTK_WIDGET
(
dialog
));
return
true
;
}
}
break
;
case
GTK_RESPONSE_DELETE_EVENT
:
// Fall through.
case
GTK_RESPONSE_CANCEL
:
// Cancel
case
GTK_RESPONSE_APPLY
:
// Preview
break
;
}
default:
gtk_widget_destroy
(
GTK_WIDGET
(
dialog
));
on_completed
(
this
,
false
,
nullptr
);
return
true
;
}
}
if
(
!
gtk_printer_accepts_pdf
(
_printer
))
{
on_completed
(
this
,
false
,
"This printer does not accept PDF jobs"
);
g_object_unref
(
_printer
);
g_object_unref
(
settings
);
g_object_unref
(
setup
);
return
false
;
}
auto
_width
=
gtk_page_setup_get_page_width
(
setup
,
GTK_UNIT_POINTS
);
auto
_height
=
gtk_page_setup_get_page_height
(
setup
,
GTK_UNIT_POINTS
);
auto
_marginLeft
=
gtk_page_setup_get_left_margin
(
setup
,
GTK_UNIT_POINTS
);
auto
_marginTop
=
gtk_page_setup_get_top_margin
(
setup
,
GTK_UNIT_POINTS
);
auto
_marginRight
=
gtk_page_setup_get_right_margin
(
setup
,
GTK_UNIT_POINTS
);
auto
_marginBottom
=
gtk_page_setup_get_bottom_margin
(
setup
,
GTK_UNIT_POINTS
);
printJob
=
gtk_print_job_new
(
name
,
_printer
,
settings
,
setup
);
on_layout
(
this
,
_width
,
_height
,
_marginLeft
,
_marginTop
,
_marginRight
,
_marginBottom
);
g_object_unref
(
_printer
);
g_object_unref
(
settings
);
g_object_unref
(
setup
);
return
true
;
}
void
print_job
::
write_job
(
const
uint8_t
data
[],
size_t
size
)
{
...
...
printing/linux/print_job.h
View file @
f6e646a
...
...
@@ -24,7 +24,6 @@
class
print_job
{
private
:
const
int
index
;
char
*
documentName
=
nullptr
;
GtkPrintJob
*
printJob
;
public
:
...
...
@@ -42,6 +41,7 @@ class print_job {
const
gchar
*
printer
);
bool
print_pdf
(
const
gchar
*
name
,
const
gchar
*
printer
,
double
pageWidth
,
double
pageHeight
,
double
marginLeft
,
...
...
printing/linux/printing_plugin.cc
View file @
f6e646a
...
...
@@ -53,6 +53,9 @@ static void printing_plugin_handle_method_call(PrintingPlugin* self,
}
else
if
(
strcmp
(
method
,
"printPdf"
)
==
0
)
{
auto
name
=
fl_value_get_string
(
fl_value_lookup_string
(
args
,
"name"
));
auto
printerValue
=
fl_value_lookup_string
(
args
,
"printer"
);
auto
printer
=
printerValue
==
nullptr
?
nullptr
:
fl_value_get_string
(
printerValue
);
auto
jobNum
=
fl_value_get_int
(
fl_value_lookup_string
(
args
,
"job"
));
auto
pageWidth
=
fl_value_get_float
(
fl_value_lookup_string
(
args
,
"width"
));
auto
pageHeight
=
...
...
@@ -67,7 +70,7 @@ static void printing_plugin_handle_method_call(PrintingPlugin* self,
fl_value_get_float
(
fl_value_lookup_string
(
args
,
"marginBottom"
));
auto
job
=
new
print_job
(
jobNum
);
auto
res
=
job
->
print_pdf
(
name
,
pageWidth
,
pageHeight
,
marginLeft
,
auto
res
=
job
->
print_pdf
(
name
,
p
rinter
,
p
ageWidth
,
pageHeight
,
marginLeft
,
marginTop
,
marginRight
,
marginBottom
);
if
(
!
res
)
{
delete
job
;
...
...
@@ -75,18 +78,6 @@ static void printing_plugin_handle_method_call(PrintingPlugin* self,
g_autoptr
(
FlValue
)
result
=
fl_value_new_int
(
res
);
response
=
FL_METHOD_RESPONSE
(
fl_method_success_response_new
(
result
));
}
else
if
(
strcmp
(
method
,
"directPrintPdf"
)
==
0
)
{
auto
jobNum
=
fl_value_get_int
(
fl_value_lookup_string
(
args
,
"job"
));
auto
name
=
fl_value_get_string
(
fl_value_lookup_string
(
args
,
"name"
));
auto
printer
=
fl_value_get_string
(
fl_value_lookup_string
(
args
,
"printer"
));
auto
doc
=
fl_value_get_uint8_list
(
fl_value_lookup_string
(
args
,
"doc"
));
auto
size
=
fl_value_get_length
(
fl_value_lookup_string
(
args
,
"doc"
));
auto
job
=
std
::
make_unique
<
print_job
>
(
jobNum
);
auto
res
=
job
->
direct_print_pdf
(
name
,
doc
,
size
,
printer
);
g_autoptr
(
FlValue
)
result
=
fl_value_new_int
(
res
);
response
=
FL_METHOD_RESPONSE
(
fl_method_success_response_new
(
result
));
}
else
if
(
strcmp
(
method
,
"sharePdf"
)
==
0
)
{
auto
name
=
fl_value_get_string
(
fl_value_lookup_string
(
args
,
"name"
));
auto
doc
=
fl_value_get_uint8_list
(
fl_value_lookup_string
(
args
,
"doc"
));
...
...
printing/macos/Classes/PrintJob.swift
View file @
f6e646a
...
...
@@ -119,33 +119,7 @@ public class PrintJob: NSView, NSSharingServicePickerDelegate {
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
)
{
public
func
printPdf
(
name
:
String
,
withPageSize
size
:
CGSize
,
andMargin
_
:
CGRect
,
withPrinter
printer
:
String
?)
{
let
sharedInfo
=
NSPrintInfo
.
shared
let
sharedDict
=
sharedInfo
.
dictionary
()
let
printInfoDict
=
NSMutableDictionary
(
dictionary
:
sharedDict
)
...
...
@@ -160,6 +134,11 @@ public class PrintJob: NSView, NSSharingServicePickerDelegate {
printOperation
=
NSPrintOperation
(
view
:
self
,
printInfo
:
printInfo
)
printOperation
!.
jobTitle
=
name
printOperation
!.
printPanel
.
options
=
[
.
showsPreview
,
.
showsPaperSize
,
.
showsOrientation
]
if
printer
!=
nil
{
printInfo
.
printer
=
NSPrinter
(
name
:
printer
!
)
!
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
)
...
...
printing/macos/Classes/PrintingPlugin.swift
View file @
f6e646a
...
...
@@ -54,12 +54,13 @@ public class PrintingPlugin: NSObject, FlutterPlugin {
let
args
=
call
.
arguments
!
as!
[
String
:
Any
]
if
call
.
method
==
"printPdf"
{
let
name
=
args
[
"name"
]
as!
String
let
width
=
CGFloat
((
args
[
"width"
]
as?
NSNumber
)?
.
floatValue
??
0.0
)
let
height
=
CGFloat
((
args
[
"height"
]
as?
NSNumber
)?
.
floatValue
??
0.0
)
let
marginLeft
=
CGFloat
((
args
[
"marginLeft"
]
as?
NSNumber
)?
.
floatValue
??
0.0
)
let
marginTop
=
CGFloat
((
args
[
"marginTop"
]
as?
NSNumber
)?
.
floatValue
??
0.0
)
let
marginRight
=
CGFloat
((
args
[
"marginRight"
]
as?
NSNumber
)?
.
floatValue
??
0.0
)
let
marginBottom
=
CGFloat
((
args
[
"marginBottom"
]
as?
NSNumber
)?
.
floatValue
??
0.0
)
let
printer
=
args
[
"printer"
]
as?
String
let
width
=
CGFloat
((
args
[
"width"
]
as!
NSNumber
)
.
floatValue
)
let
height
=
CGFloat
((
args
[
"height"
]
as!
NSNumber
)
.
floatValue
)
let
marginLeft
=
CGFloat
((
args
[
"marginLeft"
]
as!
NSNumber
)
.
floatValue
)
let
marginTop
=
CGFloat
((
args
[
"marginTop"
]
as!
NSNumber
)
.
floatValue
)
let
marginRight
=
CGFloat
((
args
[
"marginRight"
]
as!
NSNumber
)
.
floatValue
)
let
marginBottom
=
CGFloat
((
args
[
"marginBottom"
]
as!
NSNumber
)
.
floatValue
)
let
printJob
=
PrintJob
(
printing
:
self
,
index
:
args
[
"job"
]
as!
Int
)
jobs
[
args
[
"job"
]
as!
UInt32
]
=
printJob
printJob
.
printPdf
(
name
:
name
,
...
...
@@ -72,21 +73,12 @@ public class PrintingPlugin: NSObject, FlutterPlugin {
y
:
marginTop
,
width
:
width
-
marginRight
-
marginLeft
,
height
:
height
-
marginBottom
-
marginTop
))
)
,
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
(
...
...
printing/windows/print_job.cpp
View file @
f6e646a
...
...
@@ -73,68 +73,15 @@ std::wstring fromUtf8(std::string str) {
PrintJob
::
PrintJob
(
Printing
*
printing
,
int
index
)
:
printing
(
printing
),
index
(
index
)
{}
bool
PrintJob
::
directPrintPdf
(
std
::
string
name
,
std
::
vector
<
uint8_t
>
data
,
std
::
string
withPrinter
)
{
FPDF_InitLibraryWithConfig
(
nullptr
);
auto
doc
=
FPDF_LoadMemDocument64
(
data
.
data
(),
data
.
size
(),
nullptr
);
if
(
!
doc
)
{
FPDF_DestroyLibrary
();
return
false
;
}
hDC
=
CreateDC
(
TEXT
(
"WINSPOOL"
),
fromUtf8
(
withPrinter
).
c_str
(),
NULL
,
NULL
);
if
(
!
hDC
)
{
FPDF_CloseDocument
(
doc
);
FPDF_DestroyLibrary
();
return
false
;
}
DOCINFO
docInfo
;
ZeroMemory
(
&
docInfo
,
sizeof
(
docInfo
));
docInfo
.
cbSize
=
sizeof
(
DOCINFO
);
auto
docName
=
fromUtf8
(
name
);
docInfo
.
lpszDocName
=
docName
.
c_str
();
StartDoc
(
hDC
,
&
docInfo
);
int
pageCount
=
FPDF_GetPageCount
(
doc
);
for
(
auto
i
=
0
;
i
<
pageCount
;
i
++
)
{
StartPage
(
hDC
);
auto
page
=
FPDF_LoadPage
(
doc
,
i
);
if
(
!
page
)
{
EndDoc
(
hDC
);
DeleteDC
(
hDC
);
FPDF_CloseDocument
(
doc
);
FPDF_DestroyLibrary
();
return
false
;
}
auto
pageWidth
=
FPDF_GetPageWidth
(
page
);
auto
pageHeight
=
FPDF_GetPageHeight
(
page
);
auto
dpiX
=
GetDeviceCaps
(
hDC
,
LOGPIXELSX
);
auto
dpiY
=
GetDeviceCaps
(
hDC
,
LOGPIXELSY
);
auto
width
=
static_cast
<
int
>
(
pageWidth
/
pdfDpi
*
dpiX
);
auto
height
=
static_cast
<
int
>
(
pageHeight
/
pdfDpi
*
dpiY
);
FPDF_RenderPage
(
hDC
,
page
,
0
,
0
,
width
,
height
,
0
,
FPDF_ANNOT
);
FPDF_ClosePage
(
page
);
EndPage
(
hDC
);
}
EndDoc
(
hDC
);
DeleteDC
(
hDC
);
FPDF_CloseDocument
(
doc
);
FPDF_DestroyLibrary
();
return
true
;
}
bool
PrintJob
::
printPdf
(
std
::
string
name
,
std
::
string
printer
)
{
documentName
=
name
;
bool
PrintJob
::
printPdf
(
std
::
string
name
)
{
if
(
printer
.
empty
()
)
{
PRINTDLG
pd
;
DEVMODE
*
dm
=
static_cast
<
DEVMODE
*>
(
GlobalAlloc
(
0
,
sizeof
(
DEVMODE
)));
ZeroMemory
(
dm
,
sizeof
(
DEVMODE
));
dm
->
dmFields
=
DM_ORIENTATION
;
dm
->
dmOrientation
=
2
;
printf
(
"Printing ...
\n
"
);
// Initialize PRINTDLG
ZeroMemory
(
&
pd
,
sizeof
(
pd
));
...
...
@@ -156,34 +103,38 @@ bool PrintJob::printPdf(std::string name) {
auto
r
=
PrintDlg
(
&
pd
);
if
(
r
==
1
)
{
auto
dpiX
=
static_cast
<
double
>
(
GetDeviceCaps
(
pd
.
hDC
,
LOGPIXELSX
))
/
pdfDpi
;
auto
dpiY
=
static_cast
<
double
>
(
GetDeviceCaps
(
pd
.
hDC
,
LOGPIXELSY
))
/
pdfDpi
;
hDC
=
pd
.
hDC
;
hDevMode
=
pd
.
hDevMode
;
hDevNames
=
pd
.
hDevNames
;
}
}
else
{
hDC
=
CreateDC
(
TEXT
(
"WINSPOOL"
),
fromUtf8
(
printer
).
c_str
(),
NULL
,
NULL
);
if
(
!
hDC
)
{
return
false
;
}
hDevMode
=
nullptr
;
hDevNames
=
nullptr
;
}
auto
dpiX
=
static_cast
<
double
>
(
GetDeviceCaps
(
hDC
,
LOGPIXELSX
))
/
pdfDpi
;
auto
dpiY
=
static_cast
<
double
>
(
GetDeviceCaps
(
hDC
,
LOGPIXELSY
))
/
pdfDpi
;
auto
pageWidth
=
static_cast
<
double
>
(
GetDeviceCaps
(
pd
.
hDC
,
PHYSICALWIDTH
))
/
dpiX
;
static_cast
<
double
>
(
GetDeviceCaps
(
hDC
,
PHYSICALWIDTH
))
/
dpiX
;
auto
pageHeight
=
static_cast
<
double
>
(
GetDeviceCaps
(
pd
.
hDC
,
PHYSICALHEIGHT
))
/
dpiY
;
auto
printableWidth
=
static_cast
<
double
>
(
GetDeviceCaps
(
pd
.
hDC
,
HORZRES
))
/
dpiX
;
static_cast
<
double
>
(
GetDeviceCaps
(
hDC
,
PHYSICALHEIGHT
))
/
dpiY
;
auto
printableWidth
=
static_cast
<
double
>
(
GetDeviceCaps
(
hDC
,
HORZRES
))
/
dpiX
;
auto
printableHeight
=
static_cast
<
double
>
(
GetDeviceCaps
(
pd
.
hDC
,
VERTRES
))
/
dpiY
;
static_cast
<
double
>
(
GetDeviceCaps
(
hDC
,
VERTRES
))
/
dpiY
;
auto
marginLeft
=
static_cast
<
double
>
(
GetDeviceCaps
(
pd
.
hDC
,
PHYSICALOFFSETX
))
/
dpiX
;
static_cast
<
double
>
(
GetDeviceCaps
(
hDC
,
PHYSICALOFFSETX
))
/
dpiX
;
auto
marginTop
=
static_cast
<
double
>
(
GetDeviceCaps
(
pd
.
hDC
,
PHYSICALOFFSETY
))
/
dpiY
;
static_cast
<
double
>
(
GetDeviceCaps
(
hDC
,
PHYSICALOFFSETY
))
/
dpiY
;
auto
marginRight
=
pageWidth
-
printableWidth
-
marginLeft
;
auto
marginBottom
=
pageHeight
-
printableHeight
-
marginTop
;
hDC
=
pd
.
hDC
;
hDevMode
=
pd
.
hDevMode
;
hDevNames
=
pd
.
hDevNames
;
documentName
=
name
;
printing
->
onLayout
(
this
,
pageWidth
,
pageHeight
,
marginLeft
,
marginTop
,
marginRight
,
marginBottom
);
return
true
;
}
return
false
;
}
std
::
vector
<
Printer
>
PrintJob
::
listPrinters
()
{
...
...
printing/windows/print_job.h
View file @
f6e646a
...
...
@@ -71,11 +71,7 @@ class PrintJob {
std
::
vector
<
Printer
>
listPrinters
();
bool
directPrintPdf
(
std
::
string
name
,
std
::
vector
<
uint8_t
>
data
,
std
::
string
printer
);
bool
printPdf
(
std
::
string
name
);
bool
printPdf
(
std
::
string
name
,
std
::
string
printer
);
void
writeJob
(
std
::
vector
<
uint8_t
>
data
);
...
...
printing/windows/printing_plugin.cpp
View file @
f6e646a
...
...
@@ -70,35 +70,18 @@ class PrintingPlugin : public flutter::Plugin {
auto
name
=
vName
!=
arguments
->
end
()
&&
!
vName
->
second
.
IsNull
()
?
std
::
get
<
std
::
string
>
(
vName
->
second
)
:
std
::
string
{
"document"
};
auto
vPrinter
=
arguments
->
find
(
flutter
::
EncodableValue
(
"printer"
));
auto
printer
=
vPrinter
!=
arguments
->
end
()
?
std
::
get
<
std
::
string
>
(
vPrinter
->
second
)
:
std
::
string
{};
auto
vJob
=
arguments
->
find
(
flutter
::
EncodableValue
(
"job"
));
auto
jobNum
=
vJob
!=
arguments
->
end
()
?
std
::
get
<
int
>
(
vJob
->
second
)
:
-
1
;
auto
job
=
new
PrintJob
{
&
printing
,
jobNum
};
auto
res
=
job
->
printPdf
(
name
);
auto
res
=
job
->
printPdf
(
name
,
printer
);
if
(
!
res
)
{
delete
job
;
}
result
->
Success
(
flutter
::
EncodableValue
(
res
?
1
:
0
));
}
else
if
(
method_call
.
method_name
().
compare
(
"directPrintPdf"
)
==
0
)
{
const
auto
*
arguments
=
std
::
get_if
<
flutter
::
EncodableMap
>
(
method_call
.
arguments
());
auto
vName
=
arguments
->
find
(
flutter
::
EncodableValue
(
"name"
));
auto
name
=
vName
!=
arguments
->
end
()
&&
!
vName
->
second
.
IsNull
()
?
std
::
get
<
std
::
string
>
(
vName
->
second
)
:
std
::
string
{
"document"
};
auto
vDoc
=
arguments
->
find
(
flutter
::
EncodableValue
(
"doc"
));
auto
doc
=
vDoc
!=
arguments
->
end
()
?
std
::
get
<
std
::
vector
<
uint8_t
>>
(
vDoc
->
second
)
:
std
::
vector
<
uint8_t
>
{};
auto
vPrinter
=
arguments
->
find
(
flutter
::
EncodableValue
(
"printer"
));
auto
printer
=
vPrinter
!=
arguments
->
end
()
?
std
::
get
<
std
::
string
>
(
vPrinter
->
second
)
:
std
::
string
{};
auto
vJob
=
arguments
->
find
(
flutter
::
EncodableValue
(
"job"
));
auto
jobNum
=
vJob
!=
arguments
->
end
()
?
std
::
get
<
int
>
(
vJob
->
second
)
:
-
1
;
auto
job
=
std
::
make_unique
<
PrintJob
>
(
&
printing
,
jobNum
);
auto
res
=
job
->
directPrintPdf
(
name
,
doc
,
printer
);
result
->
Success
(
flutter
::
EncodableValue
(
1
));
printing
.
onCompleted
(
job
.
get
(),
res
,
""
);
}
else
if
(
method_call
.
method_name
().
compare
(
"sharePdf"
)
==
0
)
{
const
auto
*
arguments
=
std
::
get_if
<
flutter
::
EncodableMap
>
(
method_call
.
arguments
());
...
...
Please
register
or
login
to post a comment