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
2019-02-03 14:56:27 -0500
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
d0cae426be32472fe88c28a57de02ccba40ac377
d0cae426
1 parent
4883c6e1
Use default margin in example
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
104 additions
and
14 deletions
pdf/lib/src/page_format.dart
printing/example/lib/document.dart
printing/example/lib/main.dart
printing/example/test/pdf_test.dart
pdf/lib/src/page_format.dart
View file @
d0cae42
...
...
@@ -69,6 +69,15 @@ class PdfPageFormat {
PdfPageFormat
get
portrait
=>
height
>=
width
?
this
:
copyWith
(
width:
height
,
height:
width
);
PdfPageFormat
applyMargin
(
{
double
left
,
double
top
,
double
right
,
double
bottom
})
=>
copyWith
(
marginLeft:
math
.
max
(
marginLeft
,
left
),
marginTop:
math
.
max
(
marginTop
,
top
),
marginRight:
math
.
max
(
marginRight
,
right
),
marginBottom:
math
.
max
(
marginBottom
,
bottom
),
);
@override
String
toString
()
{
return
"
${width}
x
$height
"
;
...
...
printing/example/lib/document.dart
View file @
d0cae42
...
...
@@ -3,21 +3,90 @@ import 'dart:async';
import
'package:pdf/pdf.dart'
;
Future
<
PdfDocument
>
generateDocument
()
async
{
Future
<
PdfDocument
>
generateDocument
(
PdfPageFormat
format
)
async
{
final
pdf
=
PdfDocument
(
deflate:
zlib
.
encode
);
final
page
=
PdfPage
(
pdf
,
pageFormat:
PdfPageFormat
.
a4
);
final
page
=
PdfPage
(
pdf
,
pageFormat:
format
.
applyMargin
(
left:
2.0
*
PdfPageFormat
.
cm
,
top:
2.0
*
PdfPageFormat
.
cm
,
right:
2.0
*
PdfPageFormat
.
cm
,
bottom:
2.0
*
PdfPageFormat
.
cm
));
final
g
=
page
.
getGraphics
();
final
font
=
PdfFont
.
helvetica
(
pdf
);
final
top
=
page
.
pageFormat
.
height
;
final
top
=
page
.
pageFormat
.
height
-
page
.
pageFormat
.
marginTop
;
g
.
setColor
(
PdfColor
.
orange
);
g
.
drawRect
(
page
.
pageFormat
.
marginLeft
,
page
.
pageFormat
.
marginBottom
,
page
.
pageFormat
.
width
-
page
.
pageFormat
.
marginRight
-
page
.
pageFormat
.
marginLeft
,
page
.
pageFormat
.
height
-
page
.
pageFormat
.
marginTop
-
page
.
pageFormat
.
marginBottom
);
g
.
strokePath
();
g
.
setColor
(
PdfColor
(
0.0
,
1.0
,
1.0
));
g
.
drawRect
(
50.0
*
PdfPageFormat
.
mm
,
top
-
80.0
*
PdfPageFormat
.
mm
,
100.0
*
PdfPageFormat
.
mm
,
50.0
*
PdfPageFormat
.
mm
);
g
.
drawRRect
(
50.0
*
PdfPageFormat
.
mm
,
top
-
80.0
*
PdfPageFormat
.
mm
,
100.0
*
PdfPageFormat
.
mm
,
50.0
*
PdfPageFormat
.
mm
,
20.0
,
20.0
,
);
g
.
fillPath
();
g
.
setColor
(
PdfColor
(
0.3
,
0.3
,
0.3
));
g
.
drawString
(
font
,
12.0
,
"Hello World!"
,
10.0
*
PdfPageFormat
.
mm
,
g
.
drawString
(
font
,
12.0
,
"Hello World!"
,
page
.
pageFormat
.
marginLeft
+
10.0
*
PdfPageFormat
.
mm
,
top
-
10.0
*
PdfPageFormat
.
mm
);
{
final
page
=
PdfPage
(
pdf
,
pageFormat:
format
.
applyMargin
(
left:
2.0
*
PdfPageFormat
.
cm
,
top:
2.0
*
PdfPageFormat
.
cm
,
right:
2.0
*
PdfPageFormat
.
cm
,
bottom:
2.0
*
PdfPageFormat
.
cm
));
final
g
=
page
.
getGraphics
();
final
font
=
PdfFont
.
helvetica
(
pdf
);
final
top
=
page
.
pageFormat
.
height
-
page
.
pageFormat
.
marginTop
;
g
.
setColor
(
PdfColor
.
orange
);
g
.
drawRect
(
page
.
pageFormat
.
marginLeft
,
page
.
pageFormat
.
marginBottom
,
page
.
pageFormat
.
width
-
page
.
pageFormat
.
marginRight
-
page
.
pageFormat
.
marginLeft
,
page
.
pageFormat
.
height
-
page
.
pageFormat
.
marginTop
-
page
.
pageFormat
.
marginBottom
);
g
.
strokePath
();
g
.
setColor
(
PdfColor
(
0.0
,
1.0
,
1.0
));
g
.
drawRRect
(
50.0
*
PdfPageFormat
.
mm
,
top
-
80.0
*
PdfPageFormat
.
mm
,
100.0
*
PdfPageFormat
.
mm
,
50.0
*
PdfPageFormat
.
mm
,
20.0
,
20.0
,
);
g
.
fillPath
();
g
.
setColor
(
PdfColor
(
0.3
,
0.3
,
0.3
));
g
.
drawString
(
font
,
12.0
,
"Hello World!"
,
page
.
pageFormat
.
marginLeft
+
10.0
*
PdfPageFormat
.
mm
,
top
-
10.0
*
PdfPageFormat
.
mm
);
}
return
pdf
;
}
...
...
printing/example/lib/main.dart
View file @
d0cae42
...
...
@@ -25,13 +25,13 @@ class MyAppState extends State<MyApp> {
void
_printPdf
()
async
{
print
(
"Print ..."
);
final
pdf
=
await
generateDocument
();
final
pdf
=
await
generateDocument
(
PdfPageFormat
.
a4
);
Printing
.
printPdf
(
document:
pdf
);
}
void
_sharePdf
()
async
{
print
(
"Share ..."
);
final
pdf
=
await
generateDocument
();
final
pdf
=
await
generateDocument
(
PdfPageFormat
.
a4
);
// Calculate the widget center for iPad sharing popup position
final
RenderBox
referenceBox
=
...
...
@@ -46,7 +46,6 @@ class MyAppState extends State<MyApp> {
}
Future
<
void
>
_printScreen
()
async
{
const
margin
=
10.0
*
PdfPageFormat
.
mm
;
final
pdf
=
PdfDocument
(
deflate:
zlib
.
encode
);
final
page
=
PdfPage
(
pdf
,
pageFormat:
PdfPageFormat
.
a4
);
final
g
=
page
.
getGraphics
();
...
...
@@ -58,8 +57,12 @@ class MyAppState extends State<MyApp> {
print
(
"Print Screen
${im.width}
x
${im.height}
..."
);
// Center the image
final
w
=
page
.
pageFormat
.
width
-
margin
*
2.0
;
final
h
=
page
.
pageFormat
.
height
-
margin
*
2.0
;
final
w
=
page
.
pageFormat
.
width
-
page
.
pageFormat
.
marginLeft
-
page
.
pageFormat
.
marginRight
;
final
h
=
page
.
pageFormat
.
height
-
page
.
pageFormat
.
marginTop
-
page
.
pageFormat
.
marginBottom
;
double
iw
,
ih
;
if
(
im
.
width
.
toDouble
()
/
im
.
height
.
toDouble
()
<
1.0
)
{
ih
=
h
;
...
...
@@ -71,8 +74,15 @@ class MyAppState extends State<MyApp> {
PdfImage
image
=
PdfImage
(
pdf
,
image:
bytes
.
buffer
.
asUint8List
(),
width:
im
.
width
,
height:
im
.
height
);
g
.
drawImage
(
image
,
margin
+
(
w
-
iw
)
/
2.0
,
page
.
pageFormat
.
height
-
margin
-
ih
-
(
h
-
ih
)
/
2.0
,
iw
,
ih
);
g
.
drawImage
(
image
,
page
.
pageFormat
.
marginLeft
+
(
w
-
iw
)
/
2.0
,
page
.
pageFormat
.
height
-
page
.
pageFormat
.
marginTop
-
ih
-
(
h
-
ih
)
/
2.0
,
iw
,
ih
);
Printing
.
printPdf
(
document:
pdf
);
}
...
...
printing/example/test/pdf_test.dart
View file @
d0cae42
...
...
@@ -2,11 +2,13 @@ import 'dart:io';
import
'package:flutter_test/flutter_test.dart'
;
import
'package:pdf/pdf.dart'
;
import
'package:printing_example/document.dart'
;
void
main
(
)
{
testWidgets
(
'Generate the Pdf document'
,
(
WidgetTester
tester
)
async
{
final
pdf
=
await
generateDocument
();
final
pdf
=
await
generateDocument
(
PdfPageFormat
.
a4
);
var
file
=
File
(
'document.pdf'
);
file
.
writeAsBytesSync
(
pdf
.
save
());
});
...
...
Please
register
or
login
to post a comment