David PHAM-VAN

Move Printing example to demo

Showing 100 changed files with 1670 additions and 180 deletions

Too many changes to show.

To preserve performance only 100 of 100+ files are displayed.

... ... @@ -15,16 +15,23 @@
DART_SRC=$(shell find . -name '*.dart')
CLNG_SRC=$(shell find printing/ios -name '*.java' -o -name '*.m' -o -name '*.h') $(shell find printing/android -name '*.java' -o -name '*.m' -o -name '*.h')
SWFT_SRC=$(shell find . -name '*.swift')
FONTS=pdf/open-sans.ttf pdf/open-sans-bold.ttf pdf/roboto.ttf pdf/noto-sans.ttf pdf/genyomintw.ttf
FONTS=pdf/open-sans.ttf pdf/open-sans-bold.ttf pdf/roboto.ttf pdf/noto-sans.ttf pdf/genyomintw.ttf demo/assets/roboto1.ttf demo/assets/roboto2.ttf demo/assets/roboto3.ttf demo/assets/open-sans.ttf demo/assets/open-sans-bold.ttf
COV_PORT=9292
all: $(FONTS) format
all: $(FONTS) demo/assets/logo.png demo/assets/profile.jpg format
pdf/open-sans.ttf:
curl -L "https://github.com/google/fonts/raw/master/apache/opensans/OpenSans-Regular.ttf" > $@
demo/assets/open-sans.ttf: pdf/open-sans.ttf
cp $^ $@
pdf/open-sans-bold.ttf:
curl -L "https://github.com/google/fonts/raw/master/apache/opensans/OpenSans-Bold.ttf" > $@
cp $@ demo/assets/
demo/assets/open-sans-bold.ttf: pdf/open-sans-bold.ttf
cp $^ $@
pdf/roboto.ttf:
curl -L "https://github.com/google/fonts/raw/master/apache/robotomono/RobotoMono-Regular.ttf" > $@
... ... @@ -35,6 +42,21 @@ pdf/noto-sans.ttf:
pdf/genyomintw.ttf:
curl -L "https://github.com/ButTaiwan/genyo-font/raw/bc2fa246196fefc1ef9e9843bc8cdba22523a39d/TW/GenYoMinTW-Heavy.ttf" > $@
demo/assets/roboto1.ttf:
curl -L "https://fonts.gstatic.com/s/roboto/v20/KFOlCnqEu92Fr1MmSU5vAw.ttf" > $@
demo/assets/roboto2.ttf:
curl -L "https://fonts.gstatic.com/s/roboto/v20/KFOlCnqEu92Fr1MmWUlvAw.ttf" > $@
demo/assets/roboto3.ttf:
curl -L "https://fonts.gstatic.com/s/roboto/v20/KFOkCnqEu92Fr1MmgWxP.ttf" > $@
demo/assets/logo.png:
curl -L "https://pigment.github.io/fake-logos/logos/medium/color/auto-speed.png" > $@
demo/assets/profile.jpg:
curl -L "https://www.fakepersongenerator.com/Face/female/female20151024334209870.jpg" > $@
format: format-dart format-clang format-swift
format-dart: $(DART_SRC)
... ... @@ -58,15 +80,14 @@ get-pdf:
get-printing:
cd printing; flutter packages get
cd printing/example; flutter packages get
get-web:
cd pdf/web_example; pub get
get-demo:
cd demo; flutter packages get
get-readme:
cd test; flutter packages get
get: $(FONTS) get-pdf get-printing get-web get-readme
get: $(FONTS) get-pdf get-printing get-demo get-readme
test-pdf: $(FONTS) get-pdf .coverage
cd pdf; pub global run coverage:collect_coverage --port=$(COV_PORT) -o coverage.json --resume-isolates --wait-paused &\
... ... @@ -77,18 +98,16 @@ test-pdf: $(FONTS) get-pdf .coverage
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-demo: $(FONTS) get-printing .coverage
cd demo; flutter test --coverage --coverage-path lcov.info
test-readme: $(FONTS) get-readme
cd test; dart extract_readme.dart
cd test; dartanalyzer readme-*.dart
test-web:
cd pdf/web_example; pub get
cd pdf/web_example; pub run webdev build
test: test-pdf test-printing node_modules test-web
cat pdf/lcov.info printing/lcov.info printing/example/lcov.info | node_modules/.bin/lcov-summary
test: test-pdf test-printing test-demo node_modules
cat pdf/lcov.info printing/lcov.info demo/lcov.info | node_modules/.bin/lcov-summary
clean:
git clean -fdx -e .vscode -e ref
... ... @@ -148,9 +167,9 @@ ref:
cd $@; curl -OL 'https://www.adobe.com/content/dam/acom/en/devnet/acrobat/pdfs/pdf_reference_1-7.pdf'
gh-pages:
cd printing/example; flutter build web
cd demo; flutter build web
git checkout gh-pages
rm -rf assets icons
mv -fv printing/example/build/web/* .
mv -fv demo/build/web/* .
.PHONY: test format format-dart format-clang clean publish-pdf publish-printing analyze ref
... ...
... ... @@ -34,3 +34,7 @@ lib/generated_plugin_registrant.dart
# Exceptions to above rules.
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages
macos/Podfile.lock
assets/*.png
assets/*.jpg
... ...
... ... @@ -14,7 +14,9 @@
* limitations under the License.
*/
// ignore_for_file: omit_local_variable_types
// ignore_for_file: always_specify_types
import 'dart:typed_data';
import 'package:intl/intl.dart';
import 'package:pdf/pdf.dart';
... ... @@ -39,6 +41,7 @@ class Calendar extends StatelessWidget {
) {
return Container(
width: double.infinity,
margin: const EdgeInsets.only(bottom: 8),
child: Text(
DateFormat.yMMMM().format(date),
style: const TextStyle(
... ... @@ -122,7 +125,7 @@ class Calendar extends StatelessWidget {
child: Container(
foregroundDecoration: BoxDecoration(
border: BoxBorder(
color: PdfColors.black,
color: PdfColors.grey,
top: true,
left: true,
right: index % 7 == 6,
... ... @@ -146,7 +149,7 @@ class Calendar extends StatelessWidget {
return Container(
foregroundDecoration: BoxDecoration(
border: BoxBorder(
color: PdfColors.black,
color: PdfColors.grey,
left: true,
right: index % 7 == 6,
bottom: true,
... ... @@ -158,16 +161,32 @@ class Calendar extends StatelessWidget {
);
return Container(
padding: const EdgeInsets.all(20),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
title(context, DateTime(_year, _month)),
head,
Flexible(flex: 1, child: body),
Expanded(child: body),
],
),
);
}
}
Future<Uint8List> generateCalendar(PdfPageFormat pageFormat) async {
//Create a PDF document.
final document = Document();
document.addPage(
Page(
pageFormat: pageFormat,
orientation: PageOrientation.landscape,
build: (context) => Calendar(
date: DateTime.now(),
),
),
);
return document.save();
}
... ...
/*
* Copyright (C) 2017, David PHAM-VAN <dev.nfet.net@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// ignore_for_file: always_specify_types
import 'dart:typed_data';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as pw;
Future<Uint8List> generateDocument(PdfPageFormat format) async {
final pw.Document doc = pw.Document();
doc.addPage(pw.MultiPage(
pageFormat:
PdfPageFormat.letter.copyWith(marginBottom: 1.5 * PdfPageFormat.cm),
crossAxisAlignment: pw.CrossAxisAlignment.start,
header: (pw.Context context) {
if (context.pageNumber == 1) {
return null;
}
return pw.Container(
alignment: pw.Alignment.centerRight,
margin: const pw.EdgeInsets.only(bottom: 3.0 * PdfPageFormat.mm),
padding: const pw.EdgeInsets.only(bottom: 3.0 * PdfPageFormat.mm),
decoration: const pw.BoxDecoration(
border: pw.BoxBorder(
bottom: true, width: 0.5, color: PdfColors.grey)),
child: pw.Text('Portable Document Format',
style: pw.Theme.of(context)
.defaultTextStyle
.copyWith(color: PdfColors.grey)));
},
footer: (pw.Context context) {
return pw.Container(
alignment: pw.Alignment.centerRight,
margin: const pw.EdgeInsets.only(top: 1.0 * PdfPageFormat.cm),
child: pw.Text(
'Page ${context.pageNumber} of ${context.pagesCount}',
style: pw.Theme.of(context)
.defaultTextStyle
.copyWith(color: PdfColors.grey)));
},
build: (pw.Context context) => <pw.Widget>[
pw.Header(
level: 0,
child: pw.Row(
mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
children: <pw.Widget>[
pw.Text('Portable Document Format', textScaleFactor: 2),
pw.PdfLogo()
])),
pw.Paragraph(
text:
'The Portable Document Format (PDF) is a file format developed by Adobe in the 1990s to present documents, including text formatting and images, in a manner independent of application software, hardware, and operating systems. Based on the PostScript language, each PDF file encapsulates a complete description of a fixed-layout flat document, including the text, fonts, vector graphics, raster images and other information needed to display it. PDF was standardized as an open format, ISO 32000, in 2008, and no longer requires any royalties for its implementation.'),
pw.Paragraph(
text:
'Today, PDF files may contain a variety of content besides flat text and graphics including logical structuring elements, interactive elements such as annotations and form-fields, layers, rich media (including video content) and three dimensional objects using U3D or PRC, and various other data formats. The PDF specification also provides for encryption and digital signatures, file attachments and metadata to enable workflows requiring these features.'),
pw.Header(level: 1, text: 'History and standardization'),
pw.Paragraph(
text:
"Adobe Systems made the PDF specification available free of charge in 1993. In the early years PDF was popular mainly in desktop publishing workflows, and competed with a variety of formats such as DjVu, Envoy, Common Ground Digital Paper, Farallon Replica and even Adobe's own PostScript format."),
pw.Paragraph(
text:
'PDF was a proprietary format controlled by Adobe until it was released as an open standard on July 1, 2008, and published by the International Organization for Standardization as ISO 32000-1:2008, at which time control of the specification passed to an ISO Committee of volunteer industry experts. In 2008, Adobe published a Public Patent License to ISO 32000-1 granting royalty-free rights for all patents owned by Adobe that are necessary to make, use, sell, and distribute PDF compliant implementations.'),
pw.Paragraph(
text:
"PDF 1.7, the sixth edition of the PDF specification that became ISO 32000-1, includes some proprietary technologies defined only by Adobe, such as Adobe XML Forms Architecture (XFA) and JavaScript extension for Acrobat, which are referenced by ISO 32000-1 as normative and indispensable for the full implementation of the ISO 32000-1 specification. These proprietary technologies are not standardized and their specification is published only on Adobe's website. Many of them are also not supported by popular third-party implementations of PDF."),
pw.Paragraph(
text:
'On July 28, 2017, ISO 32000-2:2017 (PDF 2.0) was published. ISO 32000-2 does not include any proprietary technologies as normative references.'),
pw.Header(level: 1, text: 'Technical foundations'),
pw.Paragraph(text: 'The PDF combines three technologies:'),
pw.Bullet(
text:
'A subset of the PostScript page description programming language, for generating the layout and graphics.'),
pw.Bullet(
text:
'A font-embedding/replacement system to allow fonts to travel with the documents.'),
pw.Bullet(
text:
'A structured storage system to bundle these elements and any associated content into a single file, with data compression where appropriate.'),
pw.Header(level: 2, text: 'PostScript'),
pw.Paragraph(
text:
'PostScript is a page description language run in an interpreter to generate an image, a process requiring many resources. It can handle graphics and standard features of programming languages such as if and loop commands. PDF is largely based on PostScript but simplified to remove flow control features like these, while graphics commands such as lineto remain.'),
pw.Paragraph(
text:
'Often, the PostScript-like PDF code is generated from a source PostScript file. The graphics commands that are output by the PostScript code are collected and tokenized. Any files, graphics, or fonts to which the document refers also are collected. Then, everything is compressed to a single file. Therefore, the entire PostScript world (fonts, layout, measurements) remains intact.'),
pw.Column(
crossAxisAlignment: pw.CrossAxisAlignment.start,
children: <pw.Widget>[
pw.Paragraph(
text:
'As a document format, PDF has several advantages over PostScript:'),
pw.Bullet(
text:
'PDF contains tokenized and interpreted results of the PostScript source code, for direct correspondence between changes to items in the PDF page description and changes to the resulting page appearance.'),
pw.Bullet(
text:
'PDF (from version 1.4) supports graphic transparency; PostScript does not.'),
pw.Bullet(
text:
'PostScript is an interpreted programming language with an implicit global state, so instructions accompanying the description of one page can affect the appearance of any following page. Therefore, all preceding pages in a PostScript document must be processed to determine the correct appearance of a given page, whereas each page in a PDF document is unaffected by the others. As a result, PDF viewers allow the user to quickly jump to the final pages of a long document, whereas a PostScript viewer needs to process all pages sequentially before being able to display the destination page (unless the optional PostScript Document Structuring Conventions have been carefully complied with).'),
]),
pw.Header(level: 1, text: 'Content'),
pw.Paragraph(
text:
'A PDF file is often a combination of vector graphics, text, and bitmap graphics. The basic types of content in a PDF are:'),
pw.Bullet(
text:
'Text stored as content streams (i.e., not encoded in plain text)'),
pw.Bullet(
text:
'Vector graphics for illustrations and designs that consist of shapes and lines'),
pw.Bullet(
text:
'Raster graphics for photographs and other types of image'),
pw.Bullet(text: 'Multimedia objects in the document'),
pw.Paragraph(
text:
'In later PDF revisions, a PDF document can also support links (inside document or web page), forms, JavaScript (initially available as plugin for Acrobat 3.0), or any other types of embedded contents that can be handled using plug-ins.'),
pw.Paragraph(
text:
'PDF 1.6 supports interactive 3D documents embedded in the PDF - 3D drawings can be embedded using U3D or PRC and various other data formats.'),
pw.Paragraph(
text:
'Two PDF files that look similar on a computer screen may be of very different sizes. For example, a high resolution raster image takes more space than a low resolution one. Typically higher resolution is needed for printing documents than for displaying them on screen. Other things that may increase the size of a file is embedding full fonts, especially for Asiatic scripts, and storing text as graphics. '),
pw.Header(
level: 1, text: 'File formats and Adobe Acrobat versions'),
pw.Paragraph(
text:
'The PDF file format has changed several times, and continues to evolve, along with the release of new versions of Adobe Acrobat. There have been nine versions of PDF and the corresponding version of the software:'),
pw.Table.fromTextArray(context: context, data: const <List<String>>[
<String>['Date', 'PDF Version', 'Acrobat Version'],
<String>['1993', 'PDF 1.0', 'Acrobat 1'],
<String>['1994', 'PDF 1.1', 'Acrobat 2'],
<String>['1996', 'PDF 1.2', 'Acrobat 3'],
<String>['1999', 'PDF 1.3', 'Acrobat 4'],
<String>['2001', 'PDF 1.4', 'Acrobat 5'],
<String>['2003', 'PDF 1.5', 'Acrobat 6'],
<String>['2005', 'PDF 1.6', 'Acrobat 7'],
<String>['2006', 'PDF 1.7', 'Acrobat 8'],
<String>['2008', 'PDF 1.7', 'Acrobat 9'],
<String>['2009', 'PDF 1.7', 'Acrobat 9.1'],
<String>['2010', 'PDF 1.7', 'Acrobat X'],
<String>['2012', 'PDF 1.7', 'Acrobat XI'],
<String>['2017', 'PDF 2.0', 'Acrobat DC'],
]),
pw.Padding(padding: const pw.EdgeInsets.all(10)),
pw.Paragraph(
text:
'Text is available under the Creative Commons Attribution Share Alike License.')
]));
return doc.save();
}
... ...
/*
* Copyright (C) 2017, David PHAM-VAN <dev.nfet.net@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// ignore_for_file: always_specify_types
import 'dart:typed_data';
import 'package:flutter/services.dart';
import 'package:intl/intl.dart';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as pw;
Future<Uint8List> generateInvoice(PdfPageFormat pageFormat) async {
final lorem = pw.LoremText();
final products = <Product>[
Product('19874', lorem.sentence(4), 3.99, 2),
Product('98452', lorem.sentence(6), 15, 2),
Product('28375', lorem.sentence(4), 6.95, 3),
Product('95673', lorem.sentence(3), 49.99, 4),
Product('23763', lorem.sentence(2), 560.03, 1),
Product('55209', lorem.sentence(5), 26, 1),
Product('09853', lorem.sentence(5), 26, 1),
Product('23463', lorem.sentence(5), 34, 1),
Product('56783', lorem.sentence(5), 7, 4),
Product('78256', lorem.sentence(5), 23, 1),
Product('23745', lorem.sentence(5), 94, 1),
Product('07834', lorem.sentence(5), 12, 1),
Product('23547', lorem.sentence(5), 34, 1),
Product('98387', lorem.sentence(5), 7.99, 2),
];
final invoice = Invoice(
invoiceNumber: '982347',
products: products,
customerName: 'Abraham Swearegin',
customerAddress: '54 rue de Rivoli\n75001 Paris, France',
paymentInfo:
'4509 Wiseman Street\nKnoxville, Tennessee(TN), 37929\n865-372-0425',
tax: .15,
baseColor: PdfColors.teal,
accentColor: PdfColors.blueGrey900,
);
return await invoice.buildPdf(pageFormat);
}
class Invoice {
Invoice({
this.products,
this.customerName,
this.customerAddress,
this.invoiceNumber,
this.tax,
this.paymentInfo,
this.baseColor,
this.accentColor,
});
final List<Product> products;
final String customerName;
final String customerAddress;
final String invoiceNumber;
final double tax;
final String paymentInfo;
final PdfColor baseColor;
final PdfColor accentColor;
static const _darkColor = PdfColors.blueGrey800;
static const _lightColor = PdfColors.white;
PdfColor get _baseTextColor =>
baseColor.luminance < 0.5 ? _lightColor : _darkColor;
PdfColor get _accentTextColor =>
baseColor.luminance < 0.5 ? _lightColor : _darkColor;
double get _total =>
products.map<double>((p) => p.total).reduce((a, b) => a + b);
double get _grandTotal => _total * (1 + tax);
PdfImage _logo;
Future<Uint8List> buildPdf(PdfPageFormat pageFormat) async {
// Create a PDF document.
final doc = pw.Document();
final font1 = await rootBundle.load('assets/roboto1.ttf');
final font2 = await rootBundle.load('assets/roboto2.ttf');
final font3 = await rootBundle.load('assets/roboto3.ttf');
_logo = PdfImage.file(
doc.document,
bytes: (await rootBundle.load('assets/logo.png')).buffer.asUint8List(),
);
// Add page to the PDF
doc.addPage(
pw.MultiPage(
pageTheme: _buildTheme(
pageFormat,
font1 != null ? pw.Font.ttf(font1) : null,
font2 != null ? pw.Font.ttf(font2) : null,
font3 != null ? pw.Font.ttf(font3) : null,
),
header: _buildHeader,
footer: _buildFooter,
build: (context) => [
_contentHeader(context),
_contentTable(context),
pw.SizedBox(height: 20),
_contentFooter(context),
pw.SizedBox(height: 20),
_termsAndConditions(context),
],
),
);
// Return the PDF file content
return doc.save();
}
pw.Widget _buildHeader(pw.Context context) {
return pw.Column(
children: [
pw.Row(
crossAxisAlignment: pw.CrossAxisAlignment.start,
children: [
pw.Expanded(
child: pw.Column(
children: [
pw.Container(
height: 50,
padding: const pw.EdgeInsets.only(left: 20),
alignment: pw.Alignment.centerLeft,
child: pw.Text(
'INVOICE',
style: pw.TextStyle(
color: baseColor,
fontWeight: pw.FontWeight.bold,
fontSize: 40,
),
),
),
pw.Container(
decoration: pw.BoxDecoration(
borderRadius: 2,
color: accentColor,
),
padding: const pw.EdgeInsets.only(
left: 40, top: 10, bottom: 10, right: 20),
alignment: pw.Alignment.centerLeft,
height: 50,
child: pw.DefaultTextStyle(
style: pw.TextStyle(
color: _accentTextColor,
fontSize: 12,
),
child: pw.GridView(
crossAxisCount: 2,
children: [
pw.Text('Invoice #'),
pw.Text(invoiceNumber),
pw.Text('Date:'),
pw.Text(_formatDate(DateTime.now())),
],
),
),
),
],
),
),
pw.Expanded(
child: pw.Column(
mainAxisSize: pw.MainAxisSize.min,
children: [
pw.Container(
alignment: pw.Alignment.topRight,
padding: const pw.EdgeInsets.only(bottom: 8, left: 30),
height: 72,
child: _logo != null ? pw.Image(_logo) : pw.PdfLogo(),
),
// pw.Container(
// color: baseColor,
// padding: pw.EdgeInsets.only(top: 3),
// ),
],
),
),
],
),
if (context.pageNumber > 1) pw.SizedBox(height: 20)
],
);
}
pw.Widget _buildFooter(pw.Context context) {
return pw.Row(
mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
crossAxisAlignment: pw.CrossAxisAlignment.end,
children: [
pw.Container(
height: 20,
width: 100,
child: pw.BarcodeWidget(
barcode: pw.Barcode.pdf417(),
data: 'Invoice# $invoiceNumber',
),
),
pw.Text(
'Page ${context.pageNumber}/${context.pagesCount}',
style: const pw.TextStyle(
fontSize: 12,
color: PdfColors.grey,
),
),
],
);
}
pw.PageTheme _buildTheme(
PdfPageFormat pageFormat, pw.Font base, pw.Font bold, pw.Font italic) {
return pw.PageTheme(
pageFormat: pageFormat,
theme: pw.ThemeData.withFont(
base: base,
bold: bold,
italic: italic,
),
buildBackground: (context) => pw.FullPage(
ignoreMargins: true,
child: pw.Stack(
children: [
pw.Positioned(
bottom: 0,
left: 0,
child: pw.Container(
height: 20,
width: pageFormat.width / 2,
decoration: pw.BoxDecoration(
gradient: pw.LinearGradient(
colors: [baseColor, PdfColors.white],
),
),
),
),
pw.Positioned(
bottom: 20,
left: 0,
child: pw.Container(
height: 20,
width: pageFormat.width / 4,
decoration: pw.BoxDecoration(
gradient: pw.LinearGradient(
colors: [accentColor, PdfColors.white],
),
),
),
),
pw.Positioned(
top: pageFormat.marginTop + 72,
left: 0,
right: 0,
child: pw.Container(
height: 3,
color: baseColor,
),
),
],
),
),
);
}
pw.Widget _contentHeader(pw.Context context) {
return pw.Row(
crossAxisAlignment: pw.CrossAxisAlignment.start,
children: [
pw.Expanded(
child: pw.Container(
margin: const pw.EdgeInsets.symmetric(horizontal: 20),
height: 70,
child: pw.FittedBox(
child: pw.Text(
'Total: ${_formatCurrency(_grandTotal)}',
style: pw.TextStyle(
color: baseColor,
fontStyle: pw.FontStyle.italic,
),
),
),
),
),
pw.Expanded(
child: pw.Row(
children: [
pw.Container(
margin: const pw.EdgeInsets.only(left: 10, right: 10),
height: 70,
child: pw.Text(
'Invoice to:',
style: pw.TextStyle(
color: _darkColor,
fontWeight: pw.FontWeight.bold,
fontSize: 12,
),
),
),
pw.Expanded(
child: pw.Container(
height: 70,
child: pw.RichText(
text: pw.TextSpan(
text: '$customerName\n',
style: pw.TextStyle(
color: _darkColor,
fontWeight: pw.FontWeight.bold,
fontSize: 12,
),
children: [
const pw.TextSpan(
text: '\n',
style: pw.TextStyle(
fontSize: 5,
),
),
pw.TextSpan(
text: customerAddress,
style: pw.TextStyle(
fontWeight: pw.FontWeight.normal,
fontSize: 10,
),
),
])),
),
),
],
),
),
],
);
}
pw.Widget _contentFooter(pw.Context context) {
return pw.Row(
crossAxisAlignment: pw.CrossAxisAlignment.start,
children: [
pw.Expanded(
flex: 2,
child: pw.Column(
crossAxisAlignment: pw.CrossAxisAlignment.start,
children: [
pw.Text(
'Thank you for your business',
style: pw.TextStyle(
color: _darkColor,
fontWeight: pw.FontWeight.bold,
),
),
pw.Container(
margin: const pw.EdgeInsets.only(top: 20, bottom: 8),
child: pw.Text(
'Payment Info:',
style: pw.TextStyle(
color: baseColor,
fontWeight: pw.FontWeight.bold,
),
),
),
pw.Text(
paymentInfo,
style: const pw.TextStyle(
fontSize: 8,
lineSpacing: 5,
color: _darkColor,
),
),
],
),
),
pw.Expanded(
flex: 1,
child: pw.DefaultTextStyle(
style: const pw.TextStyle(
fontSize: 10,
color: _darkColor,
),
child: pw.Column(
crossAxisAlignment: pw.CrossAxisAlignment.start,
children: [
pw.Row(
mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
children: [
pw.Text('Sub Total:'),
pw.Text(_formatCurrency(_total)),
],
),
pw.SizedBox(height: 5),
pw.Row(
mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
children: [
pw.Text('Tax:'),
pw.Text('${(tax * 100).toStringAsFixed(1)}%'),
],
),
pw.Divider(color: accentColor),
pw.DefaultTextStyle(
style: pw.TextStyle(
color: baseColor,
fontSize: 14,
fontWeight: pw.FontWeight.bold,
),
child: pw.Row(
mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
children: [
pw.Text('Total:'),
pw.Text(_formatCurrency(_grandTotal)),
],
),
),
],
),
),
),
],
);
}
pw.Widget _termsAndConditions(pw.Context context) {
return pw.Row(
crossAxisAlignment: pw.CrossAxisAlignment.end,
children: [
pw.Expanded(
child: pw.Column(
crossAxisAlignment: pw.CrossAxisAlignment.start,
children: [
pw.Container(
decoration: pw.BoxDecoration(
border: pw.BoxBorder(
top: true,
color: accentColor,
),
),
padding: const pw.EdgeInsets.only(top: 10, bottom: 4),
child: pw.Text(
'Terms & Conditions',
style: pw.TextStyle(
fontSize: 12,
color: baseColor,
fontWeight: pw.FontWeight.bold,
),
),
),
pw.Text(
pw.LoremText().paragraph(40),
textAlign: pw.TextAlign.justify,
style: const pw.TextStyle(
fontSize: 6,
lineSpacing: 2,
color: _darkColor,
),
),
],
),
),
pw.Expanded(
child: pw.SizedBox(),
),
],
);
}
pw.Widget _contentTable(pw.Context context) {
const tableHeaders = [
'SKU#',
'Item Description',
'Price',
'Quantity',
'Total'
];
return pw.Table.fromTextArray(
border: null,
cellAlignment: pw.Alignment.centerLeft,
headerDecoration: pw.BoxDecoration(
borderRadius: 2,
color: baseColor,
),
headerHeight: 25,
cellHeight: 40,
cellAlignments: {
0: pw.Alignment.centerLeft,
1: pw.Alignment.centerLeft,
2: pw.Alignment.centerRight,
3: pw.Alignment.center,
4: pw.Alignment.centerRight,
},
headerStyle: pw.TextStyle(
color: _baseTextColor,
fontSize: 10,
fontWeight: pw.FontWeight.bold,
),
cellStyle: const pw.TextStyle(
color: _darkColor,
fontSize: 10,
),
rowDecoration: pw.BoxDecoration(
border: pw.BoxBorder(
bottom: true,
color: accentColor,
width: .5,
),
),
headers: List<String>.generate(
tableHeaders.length,
(col) => tableHeaders[col],
),
data: List<List<String>>.generate(
products.length,
(row) => List<String>.generate(
tableHeaders.length,
(col) => products[row].getIndex(col),
),
),
);
}
}
String _formatCurrency(double amount) {
return '\$${amount.toStringAsFixed(2)}';
}
String _formatDate(DateTime date) {
final format = DateFormat.yMMMMd('en_US');
return format.format(date);
}
class Product {
const Product(
this.sku,
this.productName,
this.price,
this.quantity,
);
final String sku;
final String productName;
final double price;
final int quantity;
double get total => price * quantity;
String getIndex(int index) {
switch (index) {
case 0:
return sku;
case 1:
return productName;
case 2:
return _formatCurrency(price);
case 3:
return quantity.toString();
case 4:
return _formatCurrency(total);
}
return '';
}
}
... ...
/*
* Copyright (C) 2017, David PHAM-VAN <dev.nfet.net@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// ignore_for_file: always_specify_types
import 'dart:async';
import 'dart:io';
import 'dart:typed_data';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:open_file/open_file.dart';
import 'package:path_provider/path_provider.dart';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as pw;
import 'package:printing/printing.dart';
import 'package:url_launcher/url_launcher.dart' as ul;
import 'calendar.dart';
import 'document.dart';
import 'invoice.dart';
import 'report.dart';
import 'resume.dart';
void main() {
debugDefaultTargetPlatformOverride = TargetPlatform.fuchsia;
runApp(MaterialApp(home: MyApp()));
}
class MyApp extends StatefulWidget {
@override
MyAppState createState() {
return MyAppState();
}
}
class MyAppState extends State<MyApp> with SingleTickerProviderStateMixin {
List<Tab> _myTabs;
List<LayoutCallback> _tabGen;
List<String> _tabUrl;
int _tab = 0;
TabController _tabController;
PrintingInfo printingInfo;
@override
void initState() {
super.initState();
_init();
}
Future<void> _init() async {
final PrintingInfo info = await Printing.info();
_myTabs = const <Tab>[
Tab(text: 'RÉSUMÉ'),
Tab(text: 'DOCUMENT'),
Tab(text: 'INVOICE'),
Tab(text: 'REPORT'),
Tab(text: 'CALENDAR'),
];
_tabGen = const <LayoutCallback>[
generateResume,
generateDocument,
generateInvoice,
generateReport,
generateCalendar,
];
_tabUrl = const <String>[
'resume.dart',
'document.dart',
'invoice.dart',
'report.dart',
'calendar.dart',
];
_tabController = TabController(
vsync: this,
length: _myTabs.length,
initialIndex: _tab,
);
_tabController.addListener(() {
setState(() {
_tab = _tabController.index;
});
});
setState(() {
printingInfo = info;
});
}
void _showPrintedToast(BuildContext context) {
final ScaffoldState scaffold = Scaffold.of(context);
scaffold.showSnackBar(
const SnackBar(
content: Text('Document printed successfully'),
),
);
}
void _showSharedToast(BuildContext context) {
final ScaffoldState scaffold = Scaffold.of(context);
scaffold.showSnackBar(
const SnackBar(
content: Text('Document shared successfully'),
),
);
}
Future<void> _saveAsFile(
BuildContext context,
LayoutCallback build,
PdfPageFormat pageFormat,
) async {
final Uint8List bytes = await build(pageFormat);
final Directory appDocDir = await getApplicationDocumentsDirectory();
final String appDocPath = appDocDir.path;
final File file = File(appDocPath + '/' + 'document.pdf');
print('Save as file ${file.path} ...');
await file.writeAsBytes(bytes);
OpenFile.open(file.path);
}
@override
Widget build(BuildContext context) {
pw.RichText.debug = true;
if (_tabController == null) {
return const Center(child: CircularProgressIndicator());
}
final actions = <PdfPreviewAction>[
if (!kIsWeb)
PdfPreviewAction(
icon: const Icon(Icons.save),
onPressed: _saveAsFile,
)
];
return Scaffold(
appBar: AppBar(
title: const Text('Pdf Printing Example'),
bottom: TabBar(
controller: _tabController,
tabs: _myTabs,
),
),
body: PdfPreview(
maxPageWidth: 700,
build: _tabGen[_tab],
actions: actions,
onPrinted: _showPrintedToast,
onShared: _showSharedToast,
),
floatingActionButton: FloatingActionButton(
backgroundColor: Colors.deepOrange,
onPressed: _showSources,
child: Icon(Icons.code),
),
);
}
void _showSources() {
ul.launch(
'https://github.com/DavBfr/dart_pdf/blob/master/demo/lib/${_tabUrl[_tab]}',
);
}
}
... ...
/*
* Copyright (C) 2017, David PHAM-VAN <dev.nfet.net@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// ignore_for_file: always_specify_types
import 'dart:math';
import 'dart:typed_data';
import 'package:flutter/services.dart';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as pw;
Future<Uint8List> generateReport(PdfPageFormat pageFormat) async {
const tableHeaders = ['Category', 'Budget', 'Expense', 'Result'];
const dataTable = [
['Phone', 80, 95, -15],
['Internet', 250, 230, 20],
['Electricity', 300, 375, -75],
['Movies', 85, 80, 5],
['Food', 300, 350, -50],
['Fuel', 650, 550, 100],
['Insurance', 250, 310, -60],
];
final baseColor = PdfColors.cyan;
// Create a PDF document.
final document = pw.Document();
// Add page to the PDF
document.addPage(
pw.Page(
pageFormat: pageFormat,
theme: pw.ThemeData.withFont(
base: pw.Font.ttf(await rootBundle.load('assets/open-sans.ttf')),
bold: pw.Font.ttf(await rootBundle.load('assets/open-sans-bold.ttf')),
),
build: (context) {
final chart1 = pw.Chart(
left: pw.Container(
alignment: pw.Alignment.topCenter,
margin: const pw.EdgeInsets.only(right: 5, top: 10),
child: pw.Transform.rotateBox(
angle: pi / 2,
child: pw.Text('Amount'),
),
),
overlay: pw.ChartLegend(
position: const pw.Alignment(-.7, 1),
decoration: const pw.BoxDecoration(
color: PdfColors.white,
border: pw.BoxBorder(
bottom: true,
top: true,
left: true,
right: true,
color: PdfColors.black,
width: .5,
),
),
),
grid: pw.CartesianGrid(
xAxis: pw.FixedAxis.fromStrings(
List<String>.generate(
dataTable.length, (index) => dataTable[index][0]),
marginStart: 30,
marginEnd: 30,
ticks: true,
),
yAxis: pw.FixedAxis(
[0, 100, 200, 300, 400, 500, 600, 700],
format: (v) => '\$$v',
divisions: true,
),
),
datasets: [
pw.BarDataSet(
color: PdfColors.blue100,
legend: tableHeaders[2],
width: 15,
offset: -10,
borderColor: baseColor,
data: List<pw.LineChartValue>.generate(
dataTable.length,
(i) {
final num v = dataTable[i][2];
return pw.LineChartValue(i.toDouble(), v.toDouble());
},
),
),
pw.BarDataSet(
color: PdfColors.amber100,
legend: tableHeaders[1],
width: 15,
offset: 10,
borderColor: PdfColors.amber,
data: List<pw.LineChartValue>.generate(
dataTable.length,
(i) {
final num v = dataTable[i][1];
return pw.LineChartValue(i.toDouble(), v.toDouble());
},
),
),
],
);
final chart2 = pw.Chart(
grid: pw.CartesianGrid(
xAxis: pw.FixedAxis([0, 1, 2, 3, 4, 5, 6]),
yAxis: pw.FixedAxis(
[0, 200, 400, 600],
divisions: true,
),
),
datasets: [
pw.LineDataSet(
drawSurface: true,
isCurved: true,
drawPoints: false,
color: baseColor,
data: List<pw.LineChartValue>.generate(
dataTable.length,
(i) {
final num v = dataTable[i][2];
return pw.LineChartValue(i.toDouble(), v.toDouble());
},
),
),
],
);
final table = pw.Table.fromTextArray(
border: null,
headers: tableHeaders,
data: dataTable,
headerStyle: pw.TextStyle(
color: PdfColors.white,
fontWeight: pw.FontWeight.bold,
),
headerDecoration: pw.BoxDecoration(
color: baseColor,
),
rowDecoration: pw.BoxDecoration(
border: pw.BoxBorder(
bottom: true,
color: baseColor,
width: .5,
),
),
);
return pw.Column(
children: [
pw.Text('Budget Report',
style: pw.TextStyle(
color: baseColor,
fontSize: 40,
)),
pw.Divider(thickness: 4),
pw.Expanded(flex: 3, child: chart1),
pw.Divider(),
pw.Expanded(
flex: 2,
child: pw.Row(
crossAxisAlignment: pw.CrossAxisAlignment.start,
children: [
pw.Expanded(child: chart2),
pw.SizedBox(width: 10),
pw.Expanded(child: table),
],
),
),
pw.SizedBox(height: 10),
pw.Row(
crossAxisAlignment: pw.CrossAxisAlignment.start,
children: [
pw.Expanded(
child: pw.Column(children: [
pw.Container(
alignment: pw.Alignment.centerLeft,
padding: const pw.EdgeInsets.only(bottom: 10),
child: pw.Text(
'Expense By Sub-Categories',
style: pw.TextStyle(
color: baseColor,
fontSize: 16,
),
),
),
pw.Text(
'Total expenses are broken into different categories for closer look into where the money was spent.',
textAlign: pw.TextAlign.justify,
)
])),
pw.SizedBox(width: 10),
pw.Expanded(
child: pw.Column(
children: [
pw.Container(
alignment: pw.Alignment.centerLeft,
padding: const pw.EdgeInsets.only(bottom: 10),
child: pw.Text(
'Spent vs. Saved',
style: pw.TextStyle(
color: baseColor,
fontSize: 16,
),
),
),
pw.Text(
'Budget was originally \$1915. A total of \$1990 was spent on the month os January which exceeded the overall budget by \$75',
textAlign: pw.TextAlign.justify,
)
],
),
),
],
),
],
);
},
),
);
// Return the PDF file content
return document.save();
}
... ...
/*
* Copyright (C) 2017, David PHAM-VAN <dev.nfet.net@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// ignore_for_file: always_specify_types
import 'dart:async';
import 'dart:typed_data';
import 'package:flutter/services.dart';
import 'package:meta/meta.dart';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as pw;
const PdfColor green = PdfColor.fromInt(0xff9ce5d0);
const PdfColor lightGreen = PdfColor.fromInt(0xffcdf1e7);
Future<Uint8List> generateResume(PdfPageFormat format) async {
final pw.Document doc =
pw.Document(title: 'My Résumé', author: 'David PHAM-VAN');
final PdfImage profileImage = PdfImage.file(
doc.document,
bytes: (await rootBundle.load('assets/profile.jpg')).buffer.asUint8List(),
);
final pw.PageTheme pageTheme = await _myPageTheme(format);
doc.addPage(pw.Page(
pageTheme: pageTheme,
build: (pw.Context context) => pw.Row(children: <pw.Widget>[
pw.Expanded(
child: pw.Column(
crossAxisAlignment: pw.CrossAxisAlignment.start,
children: <pw.Widget>[
pw.Container(
padding: const pw.EdgeInsets.only(left: 30, bottom: 20),
child: pw.Column(
crossAxisAlignment: pw.CrossAxisAlignment.start,
children: <pw.Widget>[
pw.Text('Parnella Charlesbois',
textScaleFactor: 2,
style: pw.Theme.of(context)
.defaultTextStyle
.copyWith(fontWeight: pw.FontWeight.bold)),
pw.Padding(padding: const pw.EdgeInsets.only(top: 10)),
pw.Text('Electrotyper',
textScaleFactor: 1.2,
style: pw.Theme.of(context).defaultTextStyle.copyWith(
fontWeight: pw.FontWeight.bold, color: green)),
pw.Padding(padding: const pw.EdgeInsets.only(top: 20)),
pw.Row(
crossAxisAlignment: pw.CrossAxisAlignment.start,
mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
children: <pw.Widget>[
pw.Column(
crossAxisAlignment: pw.CrossAxisAlignment.start,
children: <pw.Widget>[
pw.Text('568 Port Washington Road'),
pw.Text('Nordegg, AB T0M 2H0'),
pw.Text('Canada, ON'),
]),
pw.Column(
crossAxisAlignment: pw.CrossAxisAlignment.start,
children: <pw.Widget>[
pw.Text('+1 403-721-6898'),
_UrlText('p.charlesbois@yahoo.com',
'mailto:p.charlesbois@yahoo.com'),
_UrlText('wholeprices.ca',
'https://wholeprices.ca'),
]),
pw.Padding(padding: pw.EdgeInsets.zero)
]),
])),
_Category(title: 'Work Experience'),
_Block(title: 'Tour bus driver'),
_Block(title: 'Logging equipment operator'),
_Block(title: 'Foot doctor'),
_Category(title: 'Education'),
_Block(title: 'Bachelor Of Commerce'),
_Block(title: 'Bachelor Interior Design'),
])),
pw.Container(
height: double.infinity,
width: 2,
margin: const pw.EdgeInsets.symmetric(horizontal: 5),
color: green,
),
pw.Column(
crossAxisAlignment: pw.CrossAxisAlignment.center,
mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
children: <pw.Widget>[
pw.ClipOval(
child: pw.Container(
width: 100,
height: 100,
color: lightGreen,
child: pw.Image(profileImage),
),
),
pw.Column(children: <pw.Widget>[
_Percent(size: 60, value: .7, title: pw.Text('Word')),
_Percent(size: 60, value: .4, title: pw.Text('Excel')),
]),
pw.BarcodeWidget(
data: 'Parnella Charlesbois',
width: 60,
height: 60,
barcode: pw.Barcode.qrCode(),
),
],
)
]),
));
return doc.save();
}
Future<pw.PageTheme> _myPageTheme(PdfPageFormat format) async {
return pw.PageTheme(
pageFormat: format.applyMargin(
left: 2.0 * PdfPageFormat.cm,
top: 4.0 * PdfPageFormat.cm,
right: 2.0 * PdfPageFormat.cm,
bottom: 2.0 * PdfPageFormat.cm),
theme: pw.ThemeData.withFont(
base: pw.Font.ttf(await rootBundle.load('assets/open-sans.ttf')),
bold: pw.Font.ttf(await rootBundle.load('assets/open-sans-bold.ttf')),
),
buildBackground: (pw.Context context) {
return pw.FullPage(
ignoreMargins: true,
child: pw.CustomPaint(
size: PdfPoint(format.width, format.height),
painter: (PdfGraphics canvas, PdfPoint size) {
context.canvas
..setColor(lightGreen)
..moveTo(0, size.y)
..lineTo(0, size.y - 230)
..lineTo(60, size.y)
..fillPath()
..setColor(green)
..moveTo(0, size.y)
..lineTo(0, size.y - 100)
..lineTo(100, size.y)
..fillPath()
..setColor(lightGreen)
..moveTo(30, size.y)
..lineTo(110, size.y - 50)
..lineTo(150, size.y)
..fillPath()
..moveTo(size.x, 0)
..lineTo(size.x, 230)
..lineTo(size.x - 60, 0)
..fillPath()
..setColor(green)
..moveTo(size.x, 0)
..lineTo(size.x, 100)
..lineTo(size.x - 100, 0)
..fillPath()
..setColor(lightGreen)
..moveTo(size.x - 30, 0)
..lineTo(size.x - 110, 50)
..lineTo(size.x - 150, 0)
..fillPath();
},
),
);
},
);
}
class _Block extends pw.StatelessWidget {
_Block({this.title});
final String title;
@override
pw.Widget build(pw.Context context) {
return pw.Column(
crossAxisAlignment: pw.CrossAxisAlignment.start,
children: <pw.Widget>[
pw.Row(
crossAxisAlignment: pw.CrossAxisAlignment.start,
children: <pw.Widget>[
pw.Container(
width: 6,
height: 6,
margin: const pw.EdgeInsets.only(top: 2.5, left: 2, right: 5),
decoration: const pw.BoxDecoration(
color: green, shape: pw.BoxShape.circle),
),
pw.Text(title,
style: pw.Theme.of(context)
.defaultTextStyle
.copyWith(fontWeight: pw.FontWeight.bold)),
]),
pw.Container(
decoration: const pw.BoxDecoration(
border: pw.BoxBorder(left: true, color: green, width: 2)),
padding: const pw.EdgeInsets.only(left: 10, top: 5, bottom: 5),
margin: const pw.EdgeInsets.only(left: 5),
child: pw.Column(
crossAxisAlignment: pw.CrossAxisAlignment.start,
children: <pw.Widget>[
pw.Lorem(length: 20),
]),
),
]);
}
}
class _Category extends pw.StatelessWidget {
_Category({this.title});
final String title;
@override
pw.Widget build(pw.Context context) {
return pw.Container(
decoration: const pw.BoxDecoration(color: lightGreen, borderRadius: 6),
margin: const pw.EdgeInsets.only(bottom: 10, top: 20),
padding: const pw.EdgeInsets.fromLTRB(10, 7, 10, 4),
child: pw.Text(title, textScaleFactor: 1.5));
}
}
class _Percent extends pw.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 pw.Widget title;
final double fontSize;
final PdfColor color;
final PdfColor backgroundColor;
final double strokeWidth;
@override
pw.Widget build(pw.Context context) {
final List<pw.Widget> widgets = <pw.Widget>[
pw.Container(
width: size,
height: size,
child: pw.Stack(
alignment: pw.Alignment.center,
fit: pw.StackFit.expand,
children: <pw.Widget>[
pw.Center(
child: pw.Text(
'${(value * 100).round().toInt()}%',
textScaleFactor: fontSize,
),
),
pw.CircularProgressIndicator(
value: value,
backgroundColor: backgroundColor,
color: color,
strokeWidth: strokeWidth,
),
],
),
)
];
if (title != null) {
widgets.add(title);
}
return pw.Column(children: widgets);
}
}
class _UrlText extends pw.StatelessWidget {
_UrlText(this.text, this.url);
final String text;
final String url;
@override
pw.Widget build(pw.Context context) {
return pw.UrlLink(
destination: url,
child: pw.Text(text,
style: const pw.TextStyle(
decoration: pw.TextDecoration.underline,
color: PdfColors.blue,
)),
);
}
}
... ...
... ... @@ -28,6 +28,7 @@
33CC11132044BFA00003C045 /* MainFlutterWindow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 33CC11122044BFA00003C045 /* MainFlutterWindow.swift */; };
33D1A10422148B71006C7A3E /* FlutterMacOS.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 33D1A10322148B71006C7A3E /* FlutterMacOS.framework */; };
33D1A10522148B93006C7A3E /* FlutterMacOS.framework in Bundle Framework */ = {isa = PBXBuildFile; fileRef = 33D1A10322148B71006C7A3E /* FlutterMacOS.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
C5167F8E3334BB79DCB28275 /* Pods_Runner.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4AAEE3AC203DE6402636AB66 /* Pods_Runner.framework */; };
D73912F022F37F9E000D13A0 /* App.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D73912EF22F37F9E000D13A0 /* App.framework */; };
D73912F222F3801D000D13A0 /* App.framework in Bundle Framework */ = {isa = PBXBuildFile; fileRef = D73912EF22F37F9E000D13A0 /* App.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
/* End PBXBuildFile section */
... ... @@ -60,7 +61,7 @@
/* Begin PBXFileReference section */
333000ED22D3DE5D00554162 /* Warnings.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = Warnings.xcconfig; sourceTree = "<group>"; };
335BBD1A22A9A15E00E9071D /* GeneratedPluginRegistrant.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GeneratedPluginRegistrant.swift; sourceTree = "<group>"; };
33CC10ED2044A3C60003C045 /* example.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "example.app"; sourceTree = BUILT_PRODUCTS_DIR; };
33CC10ED2044A3C60003C045 /* example.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = example.app; sourceTree = BUILT_PRODUCTS_DIR; };
33CC10F02044A3C60003C045 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
33CC10F22044A3C60003C045 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; name = Assets.xcassets; path = Runner/Assets.xcassets; sourceTree = "<group>"; };
33CC10F52044A3C60003C045 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/MainMenu.xib; sourceTree = "<group>"; };
... ... @@ -73,8 +74,12 @@
33E51913231747F40026EE4D /* DebugProfile.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = DebugProfile.entitlements; sourceTree = "<group>"; };
33E51914231749380026EE4D /* Release.entitlements */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.entitlements; path = Release.entitlements; sourceTree = "<group>"; };
33E5194F232828860026EE4D /* AppInfo.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = AppInfo.xcconfig; sourceTree = "<group>"; };
43ED5ED41657603B2845C03F /* Pods-Runner.profile.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.profile.xcconfig"; path = "Target Support Files/Pods-Runner/Pods-Runner.profile.xcconfig"; sourceTree = "<group>"; };
4AAEE3AC203DE6402636AB66 /* Pods_Runner.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Runner.framework; sourceTree = BUILT_PRODUCTS_DIR; };
65AA7E24FA1AB2B70A493F17 /* Pods-Runner.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.debug.xcconfig"; path = "Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig"; sourceTree = "<group>"; };
7AFA3C8E1D35360C0083082E /* Release.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = Release.xcconfig; sourceTree = "<group>"; };
9740EEB21CF90195004384FC /* Debug.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; path = Debug.xcconfig; sourceTree = "<group>"; };
AFF841D258A6267ADE7CF49D /* Pods-Runner.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.release.xcconfig"; path = "Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig"; sourceTree = "<group>"; };
D73912EF22F37F9E000D13A0 /* App.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = App.framework; path = Flutter/ephemeral/App.framework; sourceTree = SOURCE_ROOT; };
/* End PBXFileReference section */
... ... @@ -85,12 +90,24 @@
files = (
D73912F022F37F9E000D13A0 /* App.framework in Frameworks */,
33D1A10422148B71006C7A3E /* FlutterMacOS.framework in Frameworks */,
C5167F8E3334BB79DCB28275 /* Pods_Runner.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXFrameworksBuildPhase section */
/* Begin PBXGroup section */
0D30096D3D852E9C057D6E9C /* Pods */ = {
isa = PBXGroup;
children = (
65AA7E24FA1AB2B70A493F17 /* Pods-Runner.debug.xcconfig */,
AFF841D258A6267ADE7CF49D /* Pods-Runner.release.xcconfig */,
43ED5ED41657603B2845C03F /* Pods-Runner.profile.xcconfig */,
);
name = Pods;
path = Pods;
sourceTree = "<group>";
};
33BA886A226E78AF003329D5 /* Configs */ = {
isa = PBXGroup;
children = (
... ... @@ -109,6 +126,7 @@
33CEB47122A05771004F2AC0 /* Flutter */,
33CC10EE2044A3C60003C045 /* Products */,
D73912EC22F37F3D000D13A0 /* Frameworks */,
0D30096D3D852E9C057D6E9C /* Pods */,
);
sourceTree = "<group>";
};
... ... @@ -160,6 +178,7 @@
D73912EC22F37F3D000D13A0 /* Frameworks */ = {
isa = PBXGroup;
children = (
4AAEE3AC203DE6402636AB66 /* Pods_Runner.framework */,
);
name = Frameworks;
sourceTree = "<group>";
... ... @@ -171,11 +190,13 @@
isa = PBXNativeTarget;
buildConfigurationList = 33CC10FB2044A3C60003C045 /* Build configuration list for PBXNativeTarget "Runner" */;
buildPhases = (
7A774E49F208BB56B0F75B23 /* [CP] Check Pods Manifest.lock */,
33CC10E92044A3C60003C045 /* Sources */,
33CC10EA2044A3C60003C045 /* Frameworks */,
33CC10EB2044A3C60003C045 /* Resources */,
33CC110E2044A8840003C045 /* Bundle Framework */,
3399D490228B24CF009A79C7 /* ShellScript */,
198EB137690A6B5A377F607D /* [CP] Embed Pods Frameworks */,
);
buildRules = (
);
... ... @@ -245,6 +266,21 @@
/* End PBXResourcesBuildPhase section */
/* Begin PBXShellScriptBuildPhase section */
198EB137690A6B5A377F607D /* [CP] Embed Pods Frameworks */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputFileListPaths = (
);
name = "[CP] Embed Pods Frameworks";
outputFileListPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-frameworks.sh\"\n";
showEnvVarsInLog = 0;
};
3399D490228B24CF009A79C7 /* ShellScript */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
... ... @@ -282,6 +318,28 @@
shellPath = /bin/sh;
shellScript = "\"$FLUTTER_ROOT\"/packages/flutter_tools/bin/macos_assemble.sh\ntouch Flutter/ephemeral/tripwire\n";
};
7A774E49F208BB56B0F75B23 /* [CP] Check Pods Manifest.lock */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputFileListPaths = (
);
inputPaths = (
"${PODS_PODFILE_DIR_PATH}/Podfile.lock",
"${PODS_ROOT}/Manifest.lock",
);
name = "[CP] Check Pods Manifest.lock";
outputFileListPaths = (
);
outputPaths = (
"$(DERIVED_FILE_DIR)/Pods-Runner-checkManifestLockResult.txt",
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n# This output is used by Xcode 'outputs' to avoid re-running this script phase.\necho \"SUCCESS\" > \"${SCRIPT_OUTPUT_FILE_0}\"\n";
showEnvVarsInLog = 0;
};
/* End PBXShellScriptBuildPhase section */
/* Begin PBXSourcesBuildPhase section */
... ...
... ... @@ -4,4 +4,7 @@
<FileRef
location = "group:Runner.xcodeproj">
</FileRef>
<FileRef
location = "group:Pods/Pods.xcodeproj">
</FileRef>
</Workspace>
... ...
name: printing_example
description: Pdf Printing Example
version: 1.0.0+1
environment:
sdk: ">=2.7.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
printing:
path_provider:
open_file:
url_launcher:
dev_dependencies:
flutter_test:
sdk: flutter
flutter_driver:
sdk: flutter
test:
dependency_overrides:
printing:
path: ../printing
pdf:
path: ../pdf
flutter:
uses-material-design: true
assets:
- assets/
... ...
import 'dart:io';
import 'dart:typed_data';
import 'package:flutter_test/flutter_test.dart';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as pw;
import 'package:printing_example/document.dart';
void main() {
testWidgets('Pdf Generate the document', (WidgetTester tester) async {
final pw.Document doc = await generateDocument(PdfPageFormat.a4);
final Uint8List doc = await generateDocument(PdfPageFormat.a4);
final File file = File('document.pdf');
file.writeAsBytesSync(doc.save());
file.writeAsBytesSync(doc);
});
}
... ...
... ... @@ -8,7 +8,11 @@ This library is divided into two parts:
It can create a full multi-pages document with graphics,
images, and text using TrueType fonts. With the ease of use you already know.
See an interactive demo here: <https://davbfr.github.io/dart_pdf/>.
<a href="https://davbfr.github.io/dart_pdf/">
<img alt="Example document" src="https://raw.githubusercontent.com/DavBfr/dart_pdf/master/pdf/example.jpg">
</a>
Use the `printing` package <https://pub.dev/packages/printing>
for full flutter print and share operation.
... ...
// ignore_for_file: omit_local_variable_types
// ignore_for_file: always_specify_types
import 'dart:io';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as pw;
void main() {
final pw.Document doc = pw.Document();
final doc = pw.Document();
doc.addPage(pw.MultiPage(
pageFormat:
PdfPageFormat.letter.copyWith(marginBottom: 1.5 * PdfPageFormat.cm),
crossAxisAlignment: pw.CrossAxisAlignment.start,
header: (pw.Context context) {
if (context.pageNumber == 1) {
return null;
}
return pw.Container(
alignment: pw.Alignment.centerRight,
margin: const pw.EdgeInsets.only(bottom: 3.0 * PdfPageFormat.mm),
padding: const pw.EdgeInsets.only(bottom: 3.0 * PdfPageFormat.mm),
decoration: const pw.BoxDecoration(
border: pw.BoxBorder(
bottom: true, width: 0.5, color: PdfColors.grey)),
child: pw.Text('Portable Document Format',
style: pw.Theme.of(context)
.defaultTextStyle
.copyWith(color: PdfColors.grey)));
},
footer: (pw.Context context) {
return pw.Container(
alignment: pw.Alignment.centerRight,
margin: const pw.EdgeInsets.only(top: 1.0 * PdfPageFormat.cm),
child: pw.Text(
'Page ${context.pageNumber} of ${context.pagesCount}',
style: pw.Theme.of(context)
.defaultTextStyle
.copyWith(color: PdfColors.grey)));
},
build: (pw.Context context) => <pw.Widget>[
pw.Header(
level: 0,
child: pw.Row(
mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
children: <pw.Widget>[
pw.Text('Portable Document Format', textScaleFactor: 2),
pw.PdfLogo()
])),
pw.Paragraph(
text:
'The Portable Document Format (PDF) is a file format developed by Adobe in the 1990s to present documents, including text formatting and images, in a manner independent of application software, hardware, and operating systems. Based on the PostScript language, each PDF file encapsulates a complete description of a fixed-layout flat document, including the text, fonts, vector graphics, raster images and other information needed to display it. PDF was standardized as an open format, ISO 32000, in 2008, and no longer requires any royalties for its implementation.'),
pw.Paragraph(
text:
'Today, PDF files may contain a variety of content besides flat text and graphics including logical structuring elements, interactive elements such as annotations and form-fields, layers, rich media (including video content) and three dimensional objects using U3D or PRC, and various other data formats. The PDF specification also provides for encryption and digital signatures, file attachments and metadata to enable workflows requiring these features.'),
pw.Header(level: 1, text: 'History and standardization'),
pw.Paragraph(
text:
"Adobe Systems made the PDF specification available free of charge in 1993. In the early years PDF was popular mainly in desktop publishing workflows, and competed with a variety of formats such as DjVu, Envoy, Common Ground Digital Paper, Farallon Replica and even Adobe's own PostScript format."),
pw.Paragraph(
text:
'PDF was a proprietary format controlled by Adobe until it was released as an open standard on July 1, 2008, and published by the International Organization for Standardization as ISO 32000-1:2008, at which time control of the specification passed to an ISO Committee of volunteer industry experts. In 2008, Adobe published a Public Patent License to ISO 32000-1 granting royalty-free rights for all patents owned by Adobe that are necessary to make, use, sell, and distribute PDF compliant implementations.'),
pw.Paragraph(
text:
"PDF 1.7, the sixth edition of the PDF specification that became ISO 32000-1, includes some proprietary technologies defined only by Adobe, such as Adobe XML Forms Architecture (XFA) and JavaScript extension for Acrobat, which are referenced by ISO 32000-1 as normative and indispensable for the full implementation of the ISO 32000-1 specification. These proprietary technologies are not standardized and their specification is published only on Adobe's website. Many of them are also not supported by popular third-party implementations of PDF."),
pw.Paragraph(
text:
'On July 28, 2017, ISO 32000-2:2017 (PDF 2.0) was published. ISO 32000-2 does not include any proprietary technologies as normative references.'),
pw.Header(level: 1, text: 'Technical foundations'),
pw.Paragraph(text: 'The PDF combines three technologies:'),
pw.Bullet(
text:
'A subset of the PostScript page description programming language, for generating the layout and graphics.'),
pw.Bullet(
text:
'A font-embedding/replacement system to allow fonts to travel with the documents.'),
pw.Bullet(
text:
'A structured storage system to bundle these elements and any associated content into a single file, with data compression where appropriate.'),
pw.Header(level: 2, text: 'PostScript'),
pw.Paragraph(
text:
'PostScript is a page description language run in an interpreter to generate an image, a process requiring many resources. It can handle graphics and standard features of programming languages such as if and loop commands. PDF is largely based on PostScript but simplified to remove flow control features like these, while graphics commands such as lineto remain.'),
pw.Paragraph(
text:
'Often, the PostScript-like PDF code is generated from a source PostScript file. The graphics commands that are output by the PostScript code are collected and tokenized. Any files, graphics, or fonts to which the document refers also are collected. Then, everything is compressed to a single file. Therefore, the entire PostScript world (fonts, layout, measurements) remains intact.'),
pw.Column(
crossAxisAlignment: pw.CrossAxisAlignment.start,
children: <pw.Widget>[
pw.Paragraph(
text:
'As a document format, PDF has several advantages over PostScript:'),
pw.Bullet(
text:
'PDF contains tokenized and interpreted results of the PostScript source code, for direct correspondence between changes to items in the PDF page description and changes to the resulting page appearance.'),
pw.Bullet(
text:
'PDF (from version 1.4) supports graphic transparency; PostScript does not.'),
pw.Bullet(
text:
'PostScript is an interpreted programming language with an implicit global state, so instructions accompanying the description of one page can affect the appearance of any following page. Therefore, all preceding pages in a PostScript document must be processed to determine the correct appearance of a given page, whereas each page in a PDF document is unaffected by the others. As a result, PDF viewers allow the user to quickly jump to the final pages of a long document, whereas a PostScript viewer needs to process all pages sequentially before being able to display the destination page (unless the optional PostScript Document Structuring Conventions have been carefully complied with).'),
]),
pw.Header(level: 1, text: 'Content'),
pw.Paragraph(
text:
'A PDF file is often a combination of vector graphics, text, and bitmap graphics. The basic types of content in a PDF are:'),
pw.Bullet(
text:
'Text stored as content streams (i.e., not encoded in plain text)'),
pw.Bullet(
text:
'Vector graphics for illustrations and designs that consist of shapes and lines'),
pw.Bullet(
text:
'Raster graphics for photographs and other types of image'),
pw.Bullet(text: 'Multimedia objects in the document'),
pw.Paragraph(
text:
'In later PDF revisions, a PDF document can also support links (inside document or web page), forms, JavaScript (initially available as plugin for Acrobat 3.0), or any other types of embedded contents that can be handled using plug-ins.'),
pw.Paragraph(
text:
'PDF 1.6 supports interactive 3D documents embedded in the PDF - 3D drawings can be embedded using U3D or PRC and various other data formats.'),
pw.Paragraph(
text:
'Two PDF files that look similar on a computer screen may be of very different sizes. For example, a high resolution raster image takes more space than a low resolution one. Typically higher resolution is needed for printing documents than for displaying them on screen. Other things that may increase the size of a file is embedding full fonts, especially for Asiatic scripts, and storing text as graphics. '),
pw.Header(
level: 1, text: 'File formats and Adobe Acrobat versions'),
pw.Paragraph(
text:
'The PDF file format has changed several times, and continues to evolve, along with the release of new versions of Adobe Acrobat. There have been nine versions of PDF and the corresponding version of the software:'),
pw.Table.fromTextArray(context: context, data: const <List<String>>[
<String>['Date', 'PDF Version', 'Acrobat Version'],
<String>['1993', 'PDF 1.0', 'Acrobat 1'],
<String>['1994', 'PDF 1.1', 'Acrobat 2'],
<String>['1996', 'PDF 1.2', 'Acrobat 3'],
<String>['1999', 'PDF 1.3', 'Acrobat 4'],
<String>['2001', 'PDF 1.4', 'Acrobat 5'],
<String>['2003', 'PDF 1.5', 'Acrobat 6'],
<String>['2005', 'PDF 1.6', 'Acrobat 7'],
<String>['2006', 'PDF 1.7', 'Acrobat 8'],
<String>['2008', 'PDF 1.7', 'Acrobat 9'],
<String>['2009', 'PDF 1.7', 'Acrobat 9.1'],
<String>['2010', 'PDF 1.7', 'Acrobat X'],
<String>['2012', 'PDF 1.7', 'Acrobat XI'],
<String>['2017', 'PDF 2.0', 'Acrobat DC'],
]),
pw.Padding(padding: const pw.EdgeInsets.all(10)),
pw.Paragraph(
text:
'Text is available under the Creative Commons Attribution Share Alike License.')
]));
doc.addPage(
pw.Page(
build: (pw.Context context) => pw.Center(
child: pw.Text('Hello World!'),
),
),
);
final File file = File('example.pdf');
final file = File('example.pdf');
file.writeAsBytesSync(doc.save());
}
... ...
# Files and directories created by pub
.dart_tool/
.packages
# Remove the following pattern if you wish to check in your lock file
pubspec.lock
# Conventional directory for build outputs
build/
# Directory created by dartdoc
doc/api/