David PHAM-VAN

Use default margin in example

... ... @@ -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";
... ...
... ... @@ -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;
}
... ...
... ... @@ -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);
}
... ...
... ... @@ -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());
});
... ...