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
2018-10-28 08:27:07 -0400
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
cce80208250abc38c4d9573467ce41c6115f9df2
cce80208
1 parent
4bfad691
Add screenshot example
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
54 additions
and
8 deletions
printing/example/lib/main.dart
printing/example/lib/main.dart
View file @
cce8020
...
...
@@ -2,29 +2,38 @@ import 'dart:io';
import
'dart:ui'
;
import
'package:flutter/material.dart'
;
import
'package:flutter/rendering.dart'
;
import
'package:pdf/pdf.dart'
;
import
'package:printing/printing.dart'
;
void
main
(
)
=>
runApp
(
new
MaterialApp
(
home:
new
MyApp
()));
class
MyApp
extends
StatelessWidget
{
class
MyApp
extends
StatefulWidget
{
@override
MyAppState
createState
()
{
return
new
MyAppState
();
}
}
class
MyAppState
extends
State
<
MyApp
>
{
final
shareWidget
=
new
GlobalKey
();
final
previewContainer
=
new
GlobalKey
();
PDFDocument
_generateDocument
()
{
final
pdf
=
new
PDFDocument
(
deflate:
zlib
.
encode
);
final
page
=
new
PDFPage
(
pdf
,
pageFormat:
PDFPageFormat
.
A
4
);
final
page
=
new
PDFPage
(
pdf
,
pageFormat:
PDFPageFormat
.
a
4
);
final
g
=
page
.
getGraphics
();
final
font
=
new
PDFFont
(
pdf
);
final
top
=
page
.
pageFormat
.
height
;
g
.
setColor
(
new
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
.
drawRect
(
50.0
*
PDFPageFormat
.
mm
,
top
-
80.0
*
PDFPageFormat
.
mm
,
100.0
*
PDFPageFormat
.
mm
,
50.0
*
PDFPageFormat
.
mm
);
g
.
fillPath
();
g
.
setColor
(
new
PDFColor
(
0.3
,
0.3
,
0.3
));
g
.
drawString
(
font
,
12.0
,
"Hello World!"
,
10.0
*
PDFPageFormat
.
MM
,
top
-
10.0
*
PDFPageFormat
.
MM
);
g
.
drawString
(
font
,
12.0
,
"Hello World!"
,
10.0
*
PDFPageFormat
.
mm
,
top
-
10.0
*
PDFPageFormat
.
mm
);
return
pdf
;
}
...
...
@@ -51,9 +60,43 @@ class MyApp extends StatelessWidget {
Printing
.
sharePdf
(
document:
pdf
,
bounds:
bounds
);
}
Future
<
void
>
_printScreen
()
async
{
const
margin
=
10.0
*
PDFPageFormat
.
mm
;
final
pdf
=
new
PDFDocument
(
deflate:
zlib
.
encode
);
final
page
=
new
PDFPage
(
pdf
,
pageFormat:
PDFPageFormat
.
a4
);
final
g
=
page
.
getGraphics
();
RenderRepaintBoundary
boundary
=
previewContainer
.
currentContext
.
findRenderObject
();
final
im
=
await
boundary
.
toImage
();
final
bytes
=
await
im
.
toByteData
(
format:
ImageByteFormat
.
rawRgba
);
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
;
double
iw
,
ih
;
if
(
im
.
width
.
toDouble
()
/
im
.
height
.
toDouble
()
<
1.0
)
{
ih
=
h
;
iw
=
im
.
width
.
toDouble
()
*
ih
/
im
.
height
.
toDouble
();
}
else
{
iw
=
w
;
ih
=
im
.
height
.
toDouble
()
*
iw
/
im
.
width
.
toDouble
();
}
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
);
Printing
.
printPdf
(
document:
pdf
);
}
@override
Widget
build
(
BuildContext
context
)
{
return
new
Scaffold
(
return
new
RepaintBoundary
(
key:
previewContainer
,
child:
new
Scaffold
(
appBar:
new
AppBar
(
title:
const
Text
(
'Pdf Printing Example'
),
),
...
...
@@ -67,9 +110,12 @@ class MyApp extends StatelessWidget {
key:
shareWidget
,
child:
new
Text
(
'Share Document'
),
onPressed:
_sharePdf
),
new
RaisedButton
(
child:
new
Text
(
'Print Screenshot'
),
onPressed:
_printScreen
),
],
),
),
);
)
);
}
}
...
...
Please
register
or
login
to post a comment