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-03-22 09:01:09 -0300
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
ee1e8f0d32ead5ffc77652c9c5e60bb9b63b51ad
ee1e8f0d
1 parent
880f9d43
Add text input to the demo
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
94 additions
and
21 deletions
demo/lib/app.dart
demo/lib/calendar.dart
demo/lib/certificate.dart
demo/lib/data.dart
demo/lib/document.dart
demo/lib/invoice.dart
demo/lib/report.dart
demo/lib/resume.dart
demo/test/printing_test.dart
demo/lib/app.dart
View file @
ee1e8f0
...
...
@@ -16,6 +16,7 @@
import
'dart:async'
;
import
'dart:io'
;
import
'dart:typed_data'
;
import
'package:flutter/foundation.dart'
;
import
'package:flutter/material.dart'
;
...
...
@@ -28,6 +29,7 @@ import 'package:printing_demo/certificate.dart';
import
'package:url_launcher/url_launcher.dart'
as
ul
;
import
'calendar.dart'
;
import
'data.dart'
;
import
'document.dart'
;
import
'invoice.dart'
;
import
'report.dart'
;
...
...
@@ -40,7 +42,7 @@ class MyApp extends StatefulWidget {
Example
(
'INVOICE'
,
'invoice.dart'
,
generateInvoice
),
Example
(
'REPORT'
,
'report.dart'
,
generateReport
),
Example
(
'CALENDAR'
,
'calendar.dart'
,
generateCalendar
),
Example
(
'CERTIFICATE'
,
'certificate.dart'
,
generateCertificate
),
Example
(
'CERTIFICATE'
,
'certificate.dart'
,
generateCertificate
,
true
),
];
@override
...
...
@@ -55,12 +57,21 @@ class MyAppState extends State<MyApp> with SingleTickerProviderStateMixin {
PrintingInfo
?
printingInfo
;
var
_data
=
CustomData
();
var
_hasData
=
false
;
var
_pending
=
false
;
@override
void
initState
()
{
super
.
initState
();
_init
();
}
@override
void
didChangeDependencies
()
{
super
.
didChangeDependencies
();
}
Future
<
void
>
_init
()
async
{
final
info
=
await
Printing
.
info
();
...
...
@@ -73,6 +84,18 @@ class MyAppState extends State<MyApp> with SingleTickerProviderStateMixin {
setState
(()
{
_tab
=
_tabController
!.
index
;
});
if
(
MyApp
.
examples
[
_tab
].
needsData
&&
!
_hasData
&&
!
_pending
)
{
_pending
=
true
;
askName
(
context
).
then
((
value
)
{
if
(
value
!=
null
)
{
setState
(()
{
_data
=
CustomData
(
name:
value
);
_hasData
=
true
;
_pending
=
false
;
});
}
});
}
});
setState
(()
{
...
...
@@ -81,10 +104,7 @@ class MyAppState extends State<MyApp> with SingleTickerProviderStateMixin {
}
void
_showPrintedToast
(
BuildContext
context
)
{
final
scaffold
=
Scaffold
.
of
(
context
);
// ignore: deprecated_member_use
scaffold
.
showSnackBar
(
ScaffoldMessenger
.
of
(
context
).
showSnackBar
(
const
SnackBar
(
content:
Text
(
'Document printed successfully'
),
),
...
...
@@ -92,10 +112,7 @@ class MyAppState extends State<MyApp> with SingleTickerProviderStateMixin {
}
void
_showSharedToast
(
BuildContext
context
)
{
final
scaffold
=
Scaffold
.
of
(
context
);
// ignore: deprecated_member_use
scaffold
.
showSnackBar
(
ScaffoldMessenger
.
of
(
context
).
showSnackBar
(
const
SnackBar
(
content:
Text
(
'Document shared successfully'
),
),
...
...
@@ -144,7 +161,7 @@ class MyAppState extends State<MyApp> with SingleTickerProviderStateMixin {
),
body:
PdfPreview
(
maxPageWidth:
700
,
build:
MyApp
.
examples
[
_tab
].
builder
,
build:
(
format
)
=>
MyApp
.
examples
[
_tab
].
builder
(
format
,
_data
)
,
actions:
actions
,
onPrinted:
_showPrintedToast
,
onShared:
_showSharedToast
,
...
...
@@ -162,14 +179,47 @@ class MyAppState extends State<MyApp> with SingleTickerProviderStateMixin {
'https://github.com/DavBfr/dart_pdf/blob/master/demo/lib/
${MyApp.examples[_tab].file}
'
,
);
}
Future
<
String
?>
askName
(
BuildContext
context
)
{
return
showDialog
<
String
>(
barrierDismissible:
false
,
context:
context
,
builder:
(
context
)
{
final
controller
=
TextEditingController
();
return
AlertDialog
(
title:
Text
(
'Please type your name:'
),
contentPadding:
EdgeInsets
.
symmetric
(
horizontal:
20
),
content:
TextField
(
decoration:
InputDecoration
(
hintText:
'[your name]'
),
controller:
controller
,
),
actions:
[
TextButton
(
onPressed:
()
{
if
(
controller
.
text
!=
''
)
{
Navigator
.
pop
(
context
,
controller
.
text
);
}
},
child:
Text
(
'OK'
),
),
],
);
});
}
}
typedef
LayoutCallbackWithData
=
Future
<
Uint8List
>
Function
(
PdfPageFormat
pageFormat
,
CustomData
data
);
class
Example
{
const
Example
(
this
.
name
,
this
.
file
,
this
.
builder
);
const
Example
(
this
.
name
,
this
.
file
,
this
.
builder
,
[
this
.
needsData
=
false
]
);
final
String
name
;
final
String
file
;
final
LayoutCallback
builder
;
final
LayoutCallbackWithData
builder
;
final
bool
needsData
;
}
...
...
demo/lib/calendar.dart
View file @
ee1e8f0
...
...
@@ -21,6 +21,8 @@ import 'package:intl/intl.dart';
import
'package:pdf/pdf.dart'
;
import
'package:pdf/widgets.dart'
;
import
'data.dart'
;
class
Calendar
extends
StatelessWidget
{
Calendar
({
this
.
date
,
...
...
@@ -174,7 +176,8 @@ class Calendar extends StatelessWidget {
}
}
Future
<
Uint8List
>
generateCalendar
(
PdfPageFormat
pageFormat
)
async
{
Future
<
Uint8List
>
generateCalendar
(
PdfPageFormat
pageFormat
,
CustomData
data
)
async
{
//Create a PDF document.
final
document
=
Document
();
final
date
=
DateTime
.
now
();
...
...
demo/lib/certificate.dart
View file @
ee1e8f0
...
...
@@ -23,6 +23,8 @@ import 'package:pdf/pdf.dart';
import
'package:pdf/widgets.dart'
as
pw
;
import
'package:vector_math/vector_math_64.dart'
;
import
'data.dart'
;
final
_cache
=
<
String
,
Uint8List
>{};
Future
<
Uint8List
>
_download
(
String
url
)
async
{
...
...
@@ -41,7 +43,8 @@ Future<pw.Font> _downloadFont(String url) async {
return
pw
.
Font
.
ttf
(
data
.
buffer
.
asByteData
());
}
Future
<
Uint8List
>
generateCertificate
(
PdfPageFormat
pageFormat
)
async
{
Future
<
Uint8List
>
generateCertificate
(
PdfPageFormat
pageFormat
,
CustomData
data
)
async
{
final
lorem
=
pw
.
LoremText
();
final
pdf
=
pw
.
Document
();
...
...
@@ -97,7 +100,7 @@ Future<Uint8List> generateCertificate(PdfPageFormat pageFormat) async {
child:
pw
.
Divider
(
color:
PdfColors
.
grey
,
thickness:
1.5
),
),
pw
.
Text
(
'Louis Mitchell'
,
data
.
name
,
style:
pw
.
TextStyle
(
fontWeight:
pw
.
FontWeight
.
bold
,
fontSize:
20
,
...
...
@@ -127,7 +130,7 @@ Future<Uint8List> generateCertificate(PdfPageFormat pageFormat) async {
pw
.
Padding
(
padding:
pw
.
EdgeInsets
.
symmetric
(
horizontal:
10
),
child:
pw
.
Text
(
'
Rubix Cube Challenge
'
,
'
Flutter PDF Demo
'
,
style:
pw
.
TextStyle
(
fontSize:
10
,
),
...
...
demo/lib/data.dart
0 → 100644
View file @
ee1e8f0
class
CustomData
{
const
CustomData
({
this
.
name
=
'[your name]'
});
final
String
name
;
}
...
...
demo/lib/document.dart
View file @
ee1e8f0
...
...
@@ -19,7 +19,10 @@ import 'dart:typed_data';
import
'package:pdf/pdf.dart'
;
import
'package:pdf/widgets.dart'
as
pw
;
Future
<
Uint8List
>
generateDocument
(
PdfPageFormat
format
)
async
{
import
'data.dart'
;
Future
<
Uint8List
>
generateDocument
(
PdfPageFormat
format
,
CustomData
data
)
async
{
final
doc
=
pw
.
Document
(
pageMode:
PdfPageMode
.
outlines
);
doc
.
addPage
(
pw
.
MultiPage
(
...
...
demo/lib/invoice.dart
View file @
ee1e8f0
...
...
@@ -21,7 +21,10 @@ import 'package:intl/intl.dart';
import
'package:pdf/pdf.dart'
;
import
'package:pdf/widgets.dart'
as
pw
;
Future
<
Uint8List
>
generateInvoice
(
PdfPageFormat
pageFormat
)
async
{
import
'data.dart'
;
Future
<
Uint8List
>
generateInvoice
(
PdfPageFormat
pageFormat
,
CustomData
data
)
async
{
final
lorem
=
pw
.
LoremText
();
final
products
=
<
Product
>[
...
...
demo/lib/report.dart
View file @
ee1e8f0
...
...
@@ -21,7 +21,10 @@ import 'package:flutter/services.dart';
import
'package:pdf/pdf.dart'
;
import
'package:pdf/widgets.dart'
as
pw
;
Future
<
Uint8List
>
generateReport
(
PdfPageFormat
pageFormat
)
async
{
import
'data.dart'
;
Future
<
Uint8List
>
generateReport
(
PdfPageFormat
pageFormat
,
CustomData
data
)
async
{
const
tableHeaders
=
[
'Category'
,
'Budget'
,
'Expense'
,
'Result'
];
const
dataTable
=
[
...
...
demo/lib/resume.dart
View file @
ee1e8f0
...
...
@@ -21,12 +21,13 @@ import 'dart:typed_data';
import
'package:flutter/services.dart'
;
import
'package:pdf/pdf.dart'
;
import
'package:pdf/widgets.dart'
as
pw
;
import
'package:printing_demo/data.dart'
;
const
PdfColor
green
=
PdfColor
.
fromInt
(
0xff9ce5d0
);
const
PdfColor
lightGreen
=
PdfColor
.
fromInt
(
0xffcdf1e7
);
const
sep
=
120.0
;
Future
<
Uint8List
>
generateResume
(
PdfPageFormat
format
)
async
{
Future
<
Uint8List
>
generateResume
(
PdfPageFormat
format
,
CustomData
data
)
async
{
final
doc
=
pw
.
Document
(
title:
'My Résumé'
,
author:
'David PHAM-VAN'
);
final
profileImage
=
pw
.
MemoryImage
(
...
...
demo/test/printing_test.dart
View file @
ee1e8f0
...
...
@@ -2,11 +2,13 @@ import 'dart:io';
import
'package:flutter_test/flutter_test.dart'
;
import
'package:pdf/pdf.dart'
;
import
'package:printing_demo/data.dart'
;
import
'package:printing_demo/document.dart'
;
void
main
(
)
{
testWidgets
(
'Pdf Generate the document'
,
(
WidgetTester
tester
)
async
{
final
doc
=
await
generateDocument
(
PdfPageFormat
.
a4
);
final
data
=
CustomData
();
final
doc
=
await
generateDocument
(
PdfPageFormat
.
a4
,
data
);
final
file
=
File
(
'document.pdf'
);
file
.
writeAsBytesSync
(
doc
);
...
...
Please
register
or
login
to post a comment