David PHAM-VAN

Improve Example document

... ... @@ -75,7 +75,7 @@ test-pdf: $(FONTS) get-pdf .coverage
cd pdf; for EXAMPLE in $(shell cd pdf; find example -name '*.dart'); do dart $$EXAMPLE; done
test-printing: $(FONTS) get-printing .coverage
cd printing; flutter test --coverage --coverage-path lcov.info
cd printing/example; flutter test --coverage --coverage-path ../lcov.info
test-readme: $(FONTS) get-readme
cd test; dart extract_readme.dart
... ...
... ... @@ -16,5 +16,6 @@ dependencies:
crypto: "^2.0.6"
archive: "^2.0.10"
dev_dependencies:
test: any
... ...
# Changelog
## 2.1.6
- Add qrcode to example
## 2.1.5
- Add printing completion
... ...
import 'dart:async';
import 'package:flutter/widgets.dart' as fw;
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart';
import 'package:printing/printing.dart';
const PdfColor green = PdfColor.fromInt(0xff9ce5d0);
const PdfColor lightGreen = PdfColor.fromInt(0xffcdf1e7);
class MyPage extends Page {
MyPage(
{PdfPageFormat pageFormat = PdfPageFormat.a4,
BuildCallback build,
EdgeInsets margin})
: super(pageFormat: pageFormat, margin: margin, build: build);
@override
void paint(Widget child, Context context) {
context.canvas
..setColor(lightGreen)
..moveTo(0, pageFormat.height)
..lineTo(0, pageFormat.height - 230)
..lineTo(60, pageFormat.height)
..fillPath()
..setColor(green)
..moveTo(0, pageFormat.height)
..lineTo(0, pageFormat.height - 100)
..lineTo(100, pageFormat.height)
..fillPath()
..setColor(lightGreen)
..moveTo(30, pageFormat.height)
..lineTo(110, pageFormat.height - 50)
..lineTo(150, pageFormat.height)
..fillPath()
..moveTo(pageFormat.width, 0)
..lineTo(pageFormat.width, 230)
..lineTo(pageFormat.width - 60, 0)
..fillPath()
..setColor(green)
..moveTo(pageFormat.width, 0)
..lineTo(pageFormat.width, 100)
..lineTo(pageFormat.width - 100, 0)
..fillPath()
..setColor(lightGreen)
..moveTo(pageFormat.width - 30, 0)
..lineTo(pageFormat.width - 110, 50)
..lineTo(pageFormat.width - 150, 0)
..fillPath();
super.paint(child, context);
}
}
class Block extends StatelessWidget {
Block({this.title});
final String title;
@override
Widget build(Context context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Row(crossAxisAlignment: CrossAxisAlignment.start, children: <Widget>[
Container(
width: 6,
height: 6,
margin: const EdgeInsets.only(top: 2.5, left: 2, right: 5),
decoration:
const BoxDecoration(color: green, shape: BoxShape.circle),
),
Text(title,
style: Theme.of(context)
.defaultTextStyle
.copyWith(fontWeight: FontWeight.bold)),
]),
Container(
decoration: const BoxDecoration(
border: BoxBorder(left: true, color: green, width: 2)),
padding: const EdgeInsets.only(left: 10, top: 5, bottom: 5),
margin: const EdgeInsets.only(left: 5),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Lorem(length: 20),
]),
),
]);
}
}
class Category extends StatelessWidget {
Category({this.title});
final String title;
@override
Widget build(Context context) {
return Container(
decoration: const BoxDecoration(color: lightGreen, borderRadius: 6),
margin: const EdgeInsets.only(bottom: 10, top: 20),
padding: const EdgeInsets.fromLTRB(10, 7, 10, 4),
child: Text(title, textScaleFactor: 1.5));
}
}
import 'example_widgets.dart';
Future<Document> generateDocument(PdfPageFormat format) async {
final Document pdf = Document(title: 'My Résumé', author: 'David PHAM-VAN');
... ... @@ -159,8 +60,10 @@ Future<Document> generateDocument(PdfPageFormat format) async {
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text('+1 403-721-6898'),
Text('p.charlesbois@yahoo.com'),
Text('wholeprices.ca')
UrlText('p.charlesbois@yahoo.com',
'mailto:p.charlesbois@yahoo.com'),
UrlText('wholeprices.ca',
'https://wholeprices.ca'),
]),
Padding(padding: EdgeInsets.zero)
]),
... ... @@ -175,19 +78,29 @@ Future<Document> generateDocument(PdfPageFormat format) async {
])),
Container(
height: double.infinity,
width: 10,
decoration: const BoxDecoration(
border: BoxBorder(left: true, color: green, width: 2)),
width: 2,
margin: const EdgeInsets.symmetric(horizontal: 5),
color: green,
),
Column(crossAxisAlignment: CrossAxisAlignment.start, children: <Widget>[
Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
ClipOval(
child: Container(
width: 100,
height: 100,
color: lightGreen,
child:
profileImage == null ? Container() : Image(profileImage)))
])
child: profileImage == null
? Container()
: Image(profileImage))),
Column(children: <Widget>[
Percent(size: 60, value: .7, title: Text('Word')),
Percent(size: 60, value: .4, title: Text('Excel')),
]),
QrCodeWidget(data: 'Parnella Charlesbois', size: 60),
],
)
]),
));
return pdf;
... ...
import 'package:meta/meta.dart';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart';
import 'package:qr/qr.dart';
const PdfColor green = PdfColor.fromInt(0xff9ce5d0);
const PdfColor lightGreen = PdfColor.fromInt(0xffcdf1e7);
class MyPage extends Page {
MyPage(
{PdfPageFormat pageFormat = PdfPageFormat.a4,
BuildCallback build,
EdgeInsets margin})
: super(pageFormat: pageFormat, margin: margin, build: build);
@override
void paint(Widget child, Context context) {
context.canvas
..setColor(lightGreen)
..moveTo(0, pageFormat.height)
..lineTo(0, pageFormat.height - 230)
..lineTo(60, pageFormat.height)
..fillPath()
..setColor(green)
..moveTo(0, pageFormat.height)
..lineTo(0, pageFormat.height - 100)
..lineTo(100, pageFormat.height)
..fillPath()
..setColor(lightGreen)
..moveTo(30, pageFormat.height)
..lineTo(110, pageFormat.height - 50)
..lineTo(150, pageFormat.height)
..fillPath()
..moveTo(pageFormat.width, 0)
..lineTo(pageFormat.width, 230)
..lineTo(pageFormat.width - 60, 0)
..fillPath()
..setColor(green)
..moveTo(pageFormat.width, 0)
..lineTo(pageFormat.width, 100)
..lineTo(pageFormat.width - 100, 0)
..fillPath()
..setColor(lightGreen)
..moveTo(pageFormat.width - 30, 0)
..lineTo(pageFormat.width - 110, 50)
..lineTo(pageFormat.width - 150, 0)
..fillPath();
super.paint(child, context);
}
}
class Block extends StatelessWidget {
Block({this.title});
final String title;
@override
Widget build(Context context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Row(crossAxisAlignment: CrossAxisAlignment.start, children: <Widget>[
Container(
width: 6,
height: 6,
margin: const EdgeInsets.only(top: 2.5, left: 2, right: 5),
decoration:
const BoxDecoration(color: green, shape: BoxShape.circle),
),
Text(title,
style: Theme.of(context)
.defaultTextStyle
.copyWith(fontWeight: FontWeight.bold)),
]),
Container(
decoration: const BoxDecoration(
border: BoxBorder(left: true, color: green, width: 2)),
padding: const EdgeInsets.only(left: 10, top: 5, bottom: 5),
margin: const EdgeInsets.only(left: 5),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Lorem(length: 20),
]),
),
]);
}
}
class Category extends StatelessWidget {
Category({this.title});
final String title;
@override
Widget build(Context context) {
return Container(
decoration: const BoxDecoration(color: lightGreen, borderRadius: 6),
margin: const EdgeInsets.only(bottom: 10, top: 20),
padding: const EdgeInsets.fromLTRB(10, 7, 10, 4),
child: Text(title, textScaleFactor: 1.5));
}
}
typedef QrError = void Function(dynamic error);
class _QrCodeWidget extends Widget {
_QrCodeWidget({
@required String data,
this.version,
this.errorCorrectionLevel,
this.color,
this.onError,
this.gapless = false,
}) : assert(data != null),
_qr = version == null
? QrCode.fromData(
data: data,
errorCorrectLevel: errorCorrectionLevel,
)
: QrCode(
version,
errorCorrectionLevel,
) {
// configure and make the QR code data
try {
if (version != null) {
_qr.addData(data);
}
_qr.make();
} catch (ex) {
if (onError != null) {
_hasError = true;
this.onError(ex);
}
}
}
@override
void layout(Context context, BoxConstraints constraints,
{bool parentUsesSize = false}) {
box = PdfRect.fromPoints(PdfPoint.zero, constraints.biggest);
}
/// the qr code version
final int version;
/// the qr code error correction level
final int errorCorrectionLevel;
/// the color of the dark squares
final PdfColor color;
final QrError onError;
final bool gapless;
// our qr code data
final QrCode _qr;
bool _hasError = false;
@override
void paint(Context context) {
super.paint(context);
if (_hasError) {
return;
}
final double shortestSide = box.width < box.height ? box.width : box.height;
assert(shortestSide > 0);
context.canvas.setFillColor(color);
final double squareSize = shortestSide / _qr.moduleCount.toDouble();
final int pxAdjustValue = gapless ? 1 : 0;
for (int x = 0; x < _qr.moduleCount; x++) {
for (int y = 0; y < _qr.moduleCount; y++) {
if (_qr.isDark(y, x)) {
context.canvas.drawRect(
box.left + x * squareSize,
box.top - (y + 1) * squareSize,
squareSize + pxAdjustValue,
squareSize + pxAdjustValue,
);
}
}
}
context.canvas.fillPath();
}
}
class QrCodeWidget extends StatelessWidget {
QrCodeWidget({
@required this.data,
this.version,
this.errorCorrectionLevel = QrErrorCorrectLevel.L,
this.color = PdfColors.black,
this.onError,
this.gapless = false,
this.size,
this.padding,
});
/// the qr code data
final String data;
/// the qr code version
final int version;
/// the qr code error correction level
final int errorCorrectionLevel;
/// the color of the dark squares
final PdfColor color;
final QrError onError;
final bool gapless;
final double size;
final EdgeInsets padding;
@override
Widget build(Context context) {
Widget qrcode = AspectRatio(
aspectRatio: 1.0,
child: _QrCodeWidget(
data: data,
version: version,
errorCorrectionLevel: errorCorrectionLevel,
color: color,
onError: onError,
gapless: gapless,
));
if (padding != null) {
qrcode = Padding(padding: padding, child: qrcode);
}
if (size != null) {
qrcode = SizedBox(width: size, height: size, child: qrcode);
}
return qrcode;
}
}
class Percent extends StatelessWidget {
Percent({
@required this.size,
@required this.value,
this.title,
this.fontSize = 1.2,
this.color = green,
this.backgroundColor = PdfColors.grey300,
this.strokeWidth = 5,
}) : assert(size != null);
final double size;
final double value;
final Widget title;
final double fontSize;
final PdfColor color;
final PdfColor backgroundColor;
final double strokeWidth;
@override
Widget build(Context context) {
final List<Widget> widgets = <Widget>[
Container(
width: size,
height: size,
child: Stack(
alignment: Alignment.center,
fit: StackFit.expand,
children: <Widget>[
Center(
child: Text(
'${(value * 100).round().toInt()}%',
textScaleFactor: fontSize,
),
),
CircularProgressIndicator(
value: value,
backgroundColor: backgroundColor,
color: color,
strokeWidth: strokeWidth,
),
],
),
)
];
if (title != null) {
widgets.add(title);
}
return Column(children: widgets);
}
}
class UrlText extends StatelessWidget {
UrlText(this.text, this.url);
final String text;
final String url;
@override
Widget build(Context context) {
return UrlLink(
destination: url,
child: Text(text,
style: TextStyle(
decoration: TextDecoration.underline,
color: PdfColors.blue,
)),
);
}
}
... ...
... ... @@ -107,6 +107,11 @@ class MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
bool canDebug = false;
assert(() {
canDebug = true;
return true;
}());
return RepaintBoundary(
key: previewContainer,
child: Scaffold(
... ... @@ -130,6 +135,22 @@ class MyAppState extends State<MyApp> {
child: const Text('Save to file'), onPressed: _saveAsFile),
RaisedButton(
child: const Text('Print Html'), onPressed: _printHtml),
canDebug
? Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
const Text('Debug'),
Switch.adaptive(
onChanged: (bool value) {
setState(() {
pdf.Document.debug = value;
});
},
value: pdf.Document.debug,
),
],
)
: const SizedBox(),
],
),
),
... ...
... ... @@ -13,6 +13,7 @@ dependencies:
path_provider:
flutter_full_pdf_viewer:
cupertino_icons:
qr:
dev_dependencies:
flutter_test:
... ...
import 'dart:io';
import 'package:flutter_test/flutter_test.dart';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as pdf;
import 'document.dart';
import 'package:pdf/widgets.dart';
import 'package:printing_example/document.dart';
void main() {
testWidgets('Pdf Generate the document', (WidgetTester tester) async {
final pdf.Document document = await generateDocument(PdfPageFormat.a4);
final Document document = await generateDocument(PdfPageFormat.a4);
final File file = File('document.pdf');
file.writeAsBytesSync(document.save());
});
... ...
... ... @@ -4,7 +4,7 @@ description: Plugin that allows Flutter apps to generate and print documents to
homepage: https://github.com/DavBfr/dart_pdf/tree/master/printing
repository: https://github.com/DavBfr/dart_pdf
issue_tracker: https://github.com/DavBfr/dart_pdf/issues
version: 2.1.5
version: 2.1.6
environment:
sdk: ">=2.1.0 <3.0.0"
... ...
../example/lib/document.dart
\ No newline at end of file