David PHAM-VAN

Uniformize examples

... ... @@ -40,7 +40,7 @@ coverage.json
package-lock.json
lcov.info
test/readme.dart
test/readme*.dart
test/android
test/ios
test/diff
... ...
... ... @@ -81,7 +81,7 @@ test-printing: $(FONTS) get-printing .coverage
test-readme: $(FONTS) get-readme
cd test; dart extract_readme.dart
cd test; dartanalyzer readme.dart
cd test; dartanalyzer readme-*.dart
test-web:
cd pdf/web_example; pub get
... ...
... ... @@ -8,6 +8,7 @@
- Remove deprecated functions
- Optimize file size
- Add PdfColor.shade
- Uniformize examples
## 1.5.0
... ...
... ... @@ -20,16 +20,31 @@ The coordinate system is using the internal Pdf unit:
[![Buy Me A Coffee](https://bmc-cdn.nyc3.digitaloceanspaces.com/BMC-button-images/custom_images/orange_img.png "Buy Me A Coffee")](https://www.buymeacoffee.com/JORBmbw9h "Buy Me A Coffee")
Example:
## Installing
If you want to print the Pdf document on an actual printer with Flutter,
follow the instructions at <https://pub.dev/packages/printing>
1. Add this package to your package's `pubspec.yaml` file as described
on the installation tab
2. Import the libraries
```dart
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as pw;
```
## Examples
```dart
final pdf = Document();
final pdf = pw.Document();
pdf.addPage(Page(
pdf.addPage(pw.Page(
pageFormat: PdfPageFormat.a4,
build: (Context context) {
return Center(
child: Text("Hello World"),
build: (pw.Context context) {
return pw.Center(
child: pw.Text("Hello World"),
); // Center
})); // Page
```
... ... @@ -45,10 +60,10 @@ final image = PdfImage(
height: img.height,
);
pdf.addPage(Page(
build: (Context context) {
return Center(
child: Image(image),
pdf.addPage(pw.Page(
build: (pw.Context context) {
return pw.Center(
child: pw.Image(image),
); // Center
})); // Page
```
... ... @@ -57,13 +72,13 @@ To use a TrueType font:
```dart
final Uint8List fontData = File('open-sans.ttf').readAsBytesSync();
final ttf = Font.ttf(fontData.buffer.asByteData());
final ttf = pw.Font.ttf(fontData.buffer.asByteData());
pdf.addPage(Page(
pdf.addPage(pw.Page(
pageFormat: PdfPageFormat.a4,
build: (Context context) {
return Center(
child: Text('Hello World', style: TextStyle(font: ttf, fontSize: 40)),
build: (pw.Context context) {
return pw.Center(
child: pw.Text('Hello World', style: pw.TextStyle(font: ttf, fontSize: 40)),
); // Center
})); // Page
```
... ...
... ... @@ -3,130 +3,132 @@
import 'dart:io';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart';
import 'package:pdf/widgets.dart' as pw;
void main() {
final Document pdf = Document();
final pw.Document doc = pw.Document();
pdf.addPage(MultiPage(
doc.addPage(pw.MultiPage(
pageFormat:
PdfPageFormat.letter.copyWith(marginBottom: 1.5 * PdfPageFormat.cm),
crossAxisAlignment: CrossAxisAlignment.start,
header: (Context context) {
crossAxisAlignment: pw.CrossAxisAlignment.start,
header: (pw.Context context) {
if (context.pageNumber == 1) {
return null;
}
return Container(
alignment: Alignment.centerRight,
margin: const EdgeInsets.only(bottom: 3.0 * PdfPageFormat.mm),
padding: const EdgeInsets.only(bottom: 3.0 * PdfPageFormat.mm),
decoration: const BoxDecoration(
border:
BoxBorder(bottom: true, width: 0.5, color: PdfColors.grey)),
child: Text('Portable Document Format',
style: Theme.of(context)
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: (Context context) {
return Container(
alignment: Alignment.centerRight,
margin: const EdgeInsets.only(top: 1.0 * PdfPageFormat.cm),
child: Text('Page ${context.pageNumber} of ${context.pagesCount}',
style: Theme.of(context)
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: (Context context) => <Widget>[
Header(
build: (pw.Context context) => <pw.Widget>[
pw.Header(
level: 0,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text('Portable Document Format', textScaleFactor: 2),
PdfLogo()
child: pw.Row(
mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
children: <pw.Widget>[
pw.Text('Portable Document Format', textScaleFactor: 2),
pw.PdfLogo()
])),
Paragraph(
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.'),
Paragraph(
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.'),
Header(level: 1, text: 'History and standardization'),
Paragraph(
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."),
Paragraph(
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.'),
Paragraph(
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."),
Paragraph(
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.'),
Header(level: 1, text: 'Technical foundations'),
Paragraph(text: 'The PDF combines three technologies:'),
Bullet(
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.'),
Bullet(
pw.Bullet(
text:
'A font-embedding/replacement system to allow fonts to travel with the documents.'),
Bullet(
pw.Bullet(
text:
'A structured storage system to bundle these elements and any associated content into a single file, with data compression where appropriate.'),
Header(level: 2, text: 'PostScript'),
Paragraph(
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.'),
Paragraph(
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.'),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Paragraph(
pw.Column(
crossAxisAlignment: pw.CrossAxisAlignment.start,
children: <pw.Widget>[
pw.Paragraph(
text:
'As a document format, PDF has several advantages over PostScript:'),
Bullet(
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.'),
Bullet(
pw.Bullet(
text:
'PDF (from version 1.4) supports graphic transparency; PostScript does not.'),
Bullet(
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).'),
]),
Header(level: 1, text: 'Content'),
Paragraph(
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:'),
Bullet(
pw.Bullet(
text:
'Text stored as content streams (i.e., not encoded in plain text)'),
Bullet(
pw.Bullet(
text:
'Vector graphics for illustrations and designs that consist of shapes and lines'),
Bullet(
pw.Bullet(
text:
'Raster graphics for photographs and other types of image'),
Bullet(text: 'Multimedia objects in the document'),
Paragraph(
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.'),
Paragraph(
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.'),
Paragraph(
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. '),
Header(level: 1, text: 'File formats and Adobe Acrobat versions'),
Paragraph(
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:'),
Table.fromTextArray(context: context, data: const <List<String>>[
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'],
... ... @@ -142,12 +144,12 @@ void main() {
<String>['2012', 'PDF 1.7', 'Acrobat XI'],
<String>['2017', 'PDF 2.0', 'Acrobat DC'],
]),
Padding(padding: const EdgeInsets.all(10)),
Paragraph(
pw.Padding(padding: const pw.EdgeInsets.all(10)),
pw.Paragraph(
text:
'Text is available under the Creative Commons Attribution Share Alike License.')
]));
final File file = File('example.pdf');
file.writeAsBytesSync(pdf.save());
file.writeAsBytesSync(doc.save());
}
... ...
... ... @@ -8,6 +8,7 @@
- Fix inconsistent API
- Add Unit tests
- Update example tab
- Uniformize examples
## 3.1.0
... ...
... ... @@ -15,16 +15,46 @@ for documentation.
[![Buy Me A Coffee](https://bmc-cdn.nyc3.digitaloceanspaces.com/BMC-button-images/custom_images/orange_img.png "Buy Me A Coffee")](https://www.buymeacoffee.com/JORBmbw9h "Buy Me A Coffee")
Example:
## Installing
1. Add this package to your package's `pubspec.yaml` file as described
on the installation tab
2. Import the libraries
```dart
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as pw;
import 'package:printing/printing.dart';
```
3. Enable Swift on the iOS project, in `ios/Podfile`:
```Ruby
target 'Runner' do
use_frameworks! # <-- Add this line
```
4. Set minimum Android version in `android/app/build.gradle`:
```java
defaultConfig {
...
minSdkVersion 21 // <-- Change this line to 21 or more
...
}
```
## Examples
```dart
final pdf = Document();
final doc = pw.Document();
pdf.addPage(Page(
doc.addPage(pw.Page(
pageFormat: PdfPageFormat.a4,
build: (Context context) {
return Center(
child: Text("Hello World"),
build: (pw.Context context) {
return pw.Center(
child: pw.Text("Hello World"),
); // Center
})); // Page
```
... ... @@ -33,12 +63,12 @@ To load an image from an ImageProvider:
```dart
const imageProvider = const AssetImage('assets/image.png');
final PdfImage image = await pdfImageFromImageProvider(pdf: pdf.document, image: imageProvider);
final PdfImage image = await pdfImageFromImageProvider(pdf: doc.document, image: imageProvider);
pdf.addPage(Page(
build: (Context context) {
return Center(
child: Image(image),
doc.addPage(pw.Page(
build: (pw.Context context) {
return pw.Center(
child: pw.Image(image),
); // Center
})); // Page
```
... ... @@ -47,12 +77,12 @@ To use a TrueType font from a flutter bundle:
```dart
final font = await rootBundle.load("assets/open-sans.ttf");
final ttf = Font.ttf(font);
final ttf = pw.Font.ttf(font);
pdf.addPage(Page(
build: (Context context) {
return Center(
child: Text('Dart is awesome', style: TextStyle(font: ttf, fontSize: 40)),
doc.addPage(pw.Page(
build: (pw.Context context) {
return pw.Center(
child: pw.Text('Dart is awesome', style: pw.TextStyle(font: ttf, fontSize: 40)),
); // Center
})); // Page
```
... ... @@ -62,20 +92,20 @@ To save the pdf file using the [path_provider](https://pub.dev/packages/path_pro
```dart
final output = await getTemporaryDirectory();
final file = File("${output.path}/example.pdf");
await file.writeAsBytes(pdf.save());
await file.writeAsBytes(doc.save());
```
You can also print the document using the iOS or Android print service:
```dart
await Printing.layoutPdf(
onLayout: (PdfPageFormat format) async => pdf.save());
onLayout: (PdfPageFormat format) async => doc.save());
```
Or share the document to other applications:
```dart
await Printing.sharePdf(bytes: pdf.save(), filename: 'my-document.pdf');
await Printing.sharePdf(bytes: doc.save(), filename: 'my-document.pdf');
```
To print an HTML document:
... ... @@ -91,29 +121,8 @@ await Printing.layoutPdf(
Convert a Pdf to images, one image per page, get only pages 1 and 2 at 72 dpi:
```dart
await for (var page in Printing.raster(pdf.save(), pages: [0, 1], dpi: 72)) {
final image = page.toImage();
// ...or page.toPng()
await for (var page in Printing.raster(doc.save(), pages: [0, 1], dpi: 72)) {
final image = page.toImage(); // ...or page.toPng()
print(image);
}
```
## Installing
1. Add this package to your package's `pubspec.yaml` file as described on the installation tab
2. Enable Swift on the iOS project, in `ios/Podfile`:
```Ruby
target 'Runner' do
use_frameworks! # <-- Add this line
```
3. Set minimum Android version in `android/app/build.gradle`:
```java
defaultConfig {
...
minSdkVersion 21 // <-- Change this line to 21 or more
...
}
```
... ...
import 'dart:async';
import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:flutter/widgets.dart' as fw;
import 'package:flutter/widgets.dart';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart';
import 'package:pdf/widgets.dart' as pw;
import 'package:printing/printing.dart';
import 'example_widgets.dart';
Future<Document> generateDocument(PdfPageFormat format) async {
final Document pdf = Document(title: 'My Résumé', author: 'David PHAM-VAN');
Future<pw.Document> generateDocument(PdfPageFormat format) async {
final pw.Document doc =
pw.Document(title: 'My Résumé', author: 'David PHAM-VAN');
final PdfImage profileImage = kIsWeb
? null
: await pdfImageFromImageProvider(
pdf: pdf.document,
image: const fw.NetworkImage(
pdf: doc.document,
image: const NetworkImage(
'https://www.gravatar.com/avatar/00000000000000000000000000000000?d=mp&s=200'),
onError: (dynamic exception, StackTrace stackTrace) {
print('Unable to download image');
});
final PageTheme pageTheme = myPageTheme(format);
final pw.PageTheme pageTheme = myPageTheme(format);
pdf.addPage(Page(
doc.addPage(pw.Page(
pageTheme: pageTheme,
build: (Context context) => Row(children: <Widget>[
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
padding: const EdgeInsets.only(left: 30, bottom: 20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text('Parnella Charlesbois',
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: Theme.of(context)
style: pw.Theme.of(context)
.defaultTextStyle
.copyWith(fontWeight: FontWeight.bold)),
Padding(padding: const EdgeInsets.only(top: 10)),
Text('Electrotyper',
.copyWith(fontWeight: pw.FontWeight.bold)),
pw.Padding(padding: const pw.EdgeInsets.only(top: 10)),
pw.Text('Electrotyper',
textScaleFactor: 1.2,
style: Theme.of(context).defaultTextStyle.copyWith(
fontWeight: FontWeight.bold, color: green)),
Padding(padding: const EdgeInsets.only(top: 20)),
Row(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text('568 Port Washington Road'),
Text('Nordegg, AB T0M 2H0'),
Text('Canada, ON'),
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'),
]),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text('+1 403-721-6898'),
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'),
]),
Padding(padding: EdgeInsets.zero)
pw.Padding(padding: pw.EdgeInsets.zero)
]),
])),
Category(title: 'Work Experience'),
... ... @@ -77,32 +78,32 @@ Future<Document> generateDocument(PdfPageFormat format) async {
Block(title: 'Bachelor Of Commerce'),
Block(title: 'Bachelor Interior Design'),
])),
Container(
pw.Container(
height: double.infinity,
width: 2,
margin: const EdgeInsets.symmetric(horizontal: 5),
margin: const pw.EdgeInsets.symmetric(horizontal: 5),
color: green,
),
Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
ClipOval(
child: Container(
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: profileImage == null
? Container()
: Image(profileImage))),
Column(children: <Widget>[
Percent(size: 60, value: .7, title: Text('Word')),
Percent(size: 60, value: .4, title: Text('Excel')),
? pw.Container()
: 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')),
]),
QrCodeWidget(data: 'Parnella Charlesbois', size: 60),
pw.QrCodeWidget(data: 'Parnella Charlesbois', size: 60),
],
)
]),
));
return pdf;
return doc;
}
... ...
import 'package:meta/meta.dart';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart';
import 'package:pdf/widgets.dart' as pw;
const PdfColor green = PdfColor.fromInt(0xff9ce5d0);
const PdfColor lightGreen = PdfColor.fromInt(0xffcdf1e7);
PageTheme myPageTheme(PdfPageFormat format) {
return PageTheme(
pw.PageTheme myPageTheme(PdfPageFormat format) {
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),
buildBackground: (Context context) {
return FullPage(
buildBackground: (pw.Context context) {
return pw.FullPage(
ignoreMargins: true,
child: CustomPaint(
child: pw.CustomPaint(
size: PdfPoint(format.width, format.height),
painter: (PdfGraphics canvas, PdfPoint size) {
context.canvas
... ... @@ -55,60 +55,62 @@ PageTheme myPageTheme(PdfPageFormat format) {
);
}
class Block extends StatelessWidget {
class Block extends pw.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(
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 EdgeInsets.only(top: 2.5, left: 2, right: 5),
decoration:
const BoxDecoration(color: green, shape: BoxShape.circle),
margin: const pw.EdgeInsets.only(top: 2.5, left: 2, right: 5),
decoration: const pw.BoxDecoration(
color: green, shape: pw.BoxShape.circle),
),
Text(title,
style: Theme.of(context)
pw.Text(title,
style: pw.Theme.of(context)
.defaultTextStyle
.copyWith(fontWeight: FontWeight.bold)),
.copyWith(fontWeight: pw.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),
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 StatelessWidget {
class Category extends pw.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));
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 StatelessWidget {
class Percent extends pw.StatelessWidget {
Percent({
@required this.size,
@required this.value,
... ... @@ -123,7 +125,7 @@ class Percent extends StatelessWidget {
final double value;
final Widget title;
final pw.Widget title;
final double fontSize;
... ... @@ -134,22 +136,22 @@ class Percent extends StatelessWidget {
final double strokeWidth;
@override
Widget build(Context context) {
final List<Widget> widgets = <Widget>[
Container(
pw.Widget build(pw.Context context) {
final List<pw.Widget> widgets = <pw.Widget>[
pw.Container(
width: size,
height: size,
child: Stack(
alignment: Alignment.center,
fit: StackFit.expand,
children: <Widget>[
Center(
child: Text(
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,
),
),
CircularProgressIndicator(
pw.CircularProgressIndicator(
value: value,
backgroundColor: backgroundColor,
color: color,
... ... @@ -164,23 +166,23 @@ class Percent extends StatelessWidget {
widgets.add(title);
}
return Column(children: widgets);
return pw.Column(children: widgets);
}
}
class UrlText extends StatelessWidget {
class UrlText extends pw.StatelessWidget {
UrlText(this.text, this.url);
final String text;
final String url;
@override
Widget build(Context context) {
return UrlLink(
pw.Widget build(pw.Context context) {
return pw.UrlLink(
destination: url,
child: Text(text,
style: TextStyle(
decoration: TextDecoration.underline,
child: pw.Text(text,
style: pw.TextStyle(
decoration: pw.TextDecoration.underline,
color: PdfColors.blue,
)),
);
... ...
... ... @@ -11,7 +11,7 @@ import 'package:markdown/markdown.dart' as markdown;
import 'package:path_provider/path_provider.dart';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as pdf;
import 'package:pdf/widgets.dart' as pw;
import 'package:printing/printing.dart';
import 'document.dart';
... ... @@ -140,7 +140,7 @@ class MyAppState extends State<MyApp> {
Future<void> _sharePdf() async {
print('Share ...');
final pdf.Document document = await generateDocument(PdfPageFormat.a4);
final pw.Document document = await generateDocument(PdfPageFormat.a4);
// Calculate the widget center for iPad sharing popup position
final RenderBox referenceBox =
... ... @@ -165,19 +165,19 @@ class MyAppState extends State<MyApp> {
final bool result =
await Printing.layoutPdf(onLayout: (PdfPageFormat format) {
final pdf.Document document = pdf.Document();
final pw.Document document = pw.Document();
final PdfImage image = PdfImage(document.document,
image: bytes.buffer.asUint8List(),
width: im.width,
height: im.height);
document.addPage(pdf.Page(
document.addPage(pw.Page(
pageFormat: format,
build: (pdf.Context context) {
return pdf.Center(
child: pdf.Expanded(
child: pdf.Image(image),
build: (pw.Context context) {
return pw.Center(
child: pw.Expanded(
child: pw.Image(image),
),
); // Center
})); // Page
... ... @@ -289,10 +289,10 @@ class MyAppState extends State<MyApp> {
Switch.adaptive(
onChanged: (bool value) {
setState(() {
pdf.Document.debug = value;
pw.Document.debug = value;
});
},
value: pdf.Document.debug,
value: pw.Document.debug,
),
],
)
... ...
... ... @@ -2,14 +2,14 @@ import 'dart:io';
import 'package:flutter_test/flutter_test.dart';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.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 Document document = await generateDocument(PdfPageFormat.a4);
final pw.Document doc = await generateDocument(PdfPageFormat.a4);
final File file = File('document.pdf');
file.writeAsBytesSync(document.save());
file.writeAsBytesSync(doc.save());
});
}
... ...
... ... @@ -21,11 +21,11 @@ import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as pdf;
import 'package:pdf/widgets.dart' as pw;
import 'package:printing/printing.dart';
pdf.Document doc;
pdf.Font ttf;
pw.Document doc;
pw.Font ttf;
void main() {
final String path =
... ... @@ -53,10 +53,10 @@ void main() {
pdf: doc.document, image: FileImage(File('$path/example.png')));
doc.addPage(
pdf.Page(
build: (pdf.Context context) => pdf.Center(
child: pdf.Container(
child: pdf.Image(image),
pw.Page(
build: (pw.Context context) => pw.Center(
child: pw.Container(
child: pw.Image(image),
),
),
),
... ... @@ -64,12 +64,12 @@ void main() {
});
setUpAll(() {
pdf.Document.debug = true;
pdf.RichText.debug = true;
pw.Document.debug = true;
pw.RichText.debug = true;
final Uint8List fontData =
File('$path/../pdf/open-sans.ttf').readAsBytesSync();
ttf = pdf.Font.ttf(fontData.buffer.asByteData());
doc = pdf.Document();
ttf = pw.Font.ttf(fontData.buffer.asByteData());
doc = pw.Document();
});
tearDownAll(() {
... ...
... ... @@ -30,7 +30,7 @@ Iterable<String> getCode(List<md.Node> nodes, [bool isCode = false]) sync* {
yield* getCode(node.children,
node.tag == 'code' && node.attributes['class'] == 'language-dart');
} else if (node is md.Text) {
if (isCode) {
if (isCode && !node.text.startsWith('import')) {
yield '// ------------';
yield node.text;
}
... ... @@ -40,47 +40,39 @@ Iterable<String> getCode(List<md.Node> nodes, [bool isCode = false]) sync* {
}
}
void main() {
void buildFile(String src, String dest, bool flutter) {
final md.Document document = md.Document(
extensionSet: md.ExtensionSet.commonMark,
encodeHtml: false,
);
final output = File('readme.dart');
final output = File(dest);
final st = output.openWrite();
st.writeln('import \'dart:io\';');
st.writeln('import \'dart:typed_data\';');
st.writeln('import \'package:pdf/pdf.dart\';');
st.writeln('import \'package:pdf/widgets.dart\';');
st.writeln('import \'package:image/image.dart\' show decodeImage;');
st.writeln('import \'package:printing/printing.dart\';');
if (flutter) {
st.writeln('import \'package:flutter/services.dart\' show rootBundle;');
st.writeln('import \'package:flutter/widgets.dart\' show AssetImage;');
st.writeln('import \'package:path_provider/path_provider.dart\';');
{
final data = File('../pdf/README.md').readAsStringSync();
final List<String> lines = data.replaceAll('\r\n', '\n').split('\n');
final List<md.Node> parsedLines = document.parseLines(lines);
final Iterable<String> code = getCode(parsedLines);
st.writeln('Future pdfReadme() async {');
st.writeln(code.join('\n'));
st.writeln('}');
st.writeln('import \'package:printing/printing.dart\';');
} else {
st.writeln('import \'dart:typed_data\';');
st.writeln('import \'package:image/image.dart\' show decodeImage;');
}
{
final data = File('../printing/README.md').readAsStringSync();
st.writeln('import \'package:pdf/pdf.dart\';');
st.writeln('import \'package:pdf/widgets.dart\' as pw;');
final data = File(src).readAsStringSync();
final List<String> lines = data.replaceAll('\r\n', '\n').split('\n');
final List<md.Node> parsedLines = document.parseLines(lines);
final Iterable<String> code = getCode(parsedLines);
st.writeln('Future printingReadme() async {');
st.writeln(code.join('\n'));
st.writeln('}');
}
st.writeln('Future main() async {');
st.writeln('await pdfReadme();');
st.writeln('await printingReadme();');
st.writeln(code.join('\n'));
st.writeln('}');
st.close();
}
void main() {
buildFile('../pdf/README.md', 'readme-pdf.dart', false);
buildFile('../printing/README.md', 'readme-printing.dart', true);
}
... ...