David PHAM-VAN

Document.save() now returns a Future

# Changelog
## 1.14.0
## 2.0.0
- A borderRadius can only be given for a uniform Border
- Add LayoutWidgetBuilder
... ... @@ -8,6 +8,8 @@
- Improve internal sructure
- Add some asserts on the TtfParser
- Add document loading
- Remove deprecated methods
- Document.save() now returns a Future
## 1.13.0
... ...
... ... @@ -89,7 +89,7 @@ To save the pdf file:
// final output = await getTemporaryDirectory();
// final file = File("${output.path}/example.pdf");
final file = File("example.pdf");
await file.writeAsBytes(pdf.save());
await file.writeAsBytes(await pdf.save());
```
## Encryption, Digital Signature, and loading a PDF Document
... ...
... ... @@ -2,10 +2,10 @@ import 'dart:io';
import 'package:pdf/widgets.dart' as pw;
void main() {
final doc = pw.Document();
Future<void> main() async {
final pdf = pw.Document();
doc.addPage(
pdf.addPage(
pw.Page(
build: (pw.Context context) => pw.Center(
child: pw.Text('Hello World!'),
... ... @@ -14,5 +14,5 @@ void main() {
);
final file = File('example.pdf');
file.writeAsBytesSync(doc.save());
await file.writeAsBytes(await pdf.save());
}
... ...
... ... @@ -191,7 +191,7 @@ class PdfDocument {
bool get hasGraphicStates => _graphicStates != null;
/// This writes the document to an OutputStream.
void _write(PdfStream os) {
Future<void> _write(PdfStream os) async {
final pos = PdfOutput(os);
// Write each object to the [PdfStream]. We call via the output
... ... @@ -199,16 +199,16 @@ class PdfDocument {
objects.forEach(pos.write);
// Finally close the output, which writes the xref table.
pos.close();
await pos.close();
}
/// Generate the PDF document as a memory file
Uint8List save() {
Future<Uint8List> save() async {
final os = PdfStream();
if (prev != null) {
os.putBytes(prev.bytes);
}
_write(os);
await _write(os);
return os.output();
}
}
... ...
... ... @@ -68,7 +68,7 @@ class PdfOutput {
}
/// This closes the Stream, writing the xref table
void close() {
Future<void> close() async {
final xref = os.offset;
os.putString('xref\n');
... ... @@ -136,7 +136,7 @@ class PdfOutput {
os.putString('\nstartxref\n$xref\n%%EOF\n');
if (signatureID != null) {
signatureID.writeSignature(os);
await signatureID.writeSignature(os);
}
}
... ...
... ... @@ -52,17 +52,17 @@ class PdfSignature extends PdfObject {
_offsetEnd = os.offset;
}
void writeSignature(PdfStream os) {
Future<void> writeSignature(PdfStream os) async {
assert(_offsetStart != null && _offsetEnd != null,
'Must reserve the object space before signing the document');
crypto.sign(this, os, params, _offsetStart, _offsetEnd);
await crypto.sign(this, os, params, _offsetStart, _offsetEnd);
}
}
abstract class PdfSignatureBase {
void preSign(PdfObject object, PdfDict params);
void sign(PdfObject object, PdfStream os, PdfDict params, int offsetStart,
int offsetEnd);
Future<void> sign(PdfObject object, PdfStream os, PdfDict params,
int offsetStart, int offsetEnd);
}
... ...
... ... @@ -112,13 +112,13 @@ class Document {
_pages.add(page);
}
Uint8List save() {
Future<Uint8List> save() async {
if (!_paint) {
for (var page in _pages) {
page.postProcess(this);
}
_paint = true;
}
return document.save();
return await document.save();
}
}
... ...
... ... @@ -4,7 +4,7 @@ description: A pdf producer for Dart. It can create pdf files for both web or fl
homepage: https://github.com/DavBfr/dart_pdf/tree/master/pdf
repository: https://github.com/DavBfr/dart_pdf
issue_tracker: https://github.com/DavBfr/dart_pdf/issues
version: 1.14.0
version: 2.0.0
environment:
sdk: ">=2.3.0 <3.0.0"
... ...
... ... @@ -20,7 +20,7 @@ import 'package:pdf/pdf.dart';
import 'package:test/test.dart';
void main() {
test('Pdf Annotations', () {
test('Pdf Annotations', () async {
final pdf = PdfDocument();
final page = PdfPage(pdf, pageFormat: const PdfPageFormat(500, 300));
final page1 = PdfPage(pdf, pageFormat: const PdfPageFormat(500, 300));
... ... @@ -71,6 +71,6 @@ void main() {
g.strokePath();
final file = File('annotations.pdf');
file.writeAsBytesSync(pdf.save());
await file.writeAsBytes(await pdf.save());
});
}
... ...
... ... @@ -486,8 +486,8 @@ void main() {
));
});
tearDownAll(() {
tearDownAll(() async {
final file = File('arabic.pdf');
file.writeAsBytesSync(pdf.save());
await file.writeAsBytes(await pdf.save());
});
}
... ...
... ... @@ -744,8 +744,8 @@ void main() {
});
});
tearDownAll(() {
tearDownAll(() async {
final file = File('colors.pdf');
file.writeAsBytesSync(pdf.save());
await file.writeAsBytes(await pdf.save());
});
}
... ...
... ... @@ -23,7 +23,7 @@ import 'package:test/test.dart';
import 'package:vector_math/vector_math_64.dart';
void main() {
test('Pdf Complex', () {
test('Pdf Complex', () async {
final img = Uint32List(10 * 10);
img.fillRange(0, img.length - 1, 0x12345678);
... ... @@ -96,6 +96,6 @@ void main() {
}
final file = File('complex.pdf');
file.writeAsBytesSync(pdf.save());
await file.writeAsBytes(await pdf.save());
});
}
... ...
... ... @@ -106,9 +106,9 @@ void main() {
);
});
tearDownAll(() {
tearDownAll(() async {
final file = File('jpeg.pdf');
file.writeAsBytesSync(pdf.save());
await file.writeAsBytes(await pdf.save());
});
}
... ...
... ... @@ -99,7 +99,7 @@ void printMetrics(
}
void main() {
test('Pdf Font Metrics', () {
test('Pdf Font Metrics', () async {
final pdf = Document();
PdfFont.courier(pdf.document);
... ... @@ -126,6 +126,6 @@ void main() {
}
final file = File('metrics.pdf');
file.writeAsBytesSync(pdf.save());
await file.writeAsBytes(await pdf.save());
});
}
... ...
... ... @@ -20,7 +20,7 @@ import 'package:pdf/pdf.dart';
import 'package:test/test.dart';
void main() {
test('Pdf Minimal', () {
test('Pdf Minimal', () async {
final pdf = PdfDocument(compress: false);
final page = PdfPage(pdf, pageFormat: PdfPageFormat.a4);
... ... @@ -30,6 +30,6 @@ void main() {
g.strokePath();
final file = File('minimal.pdf');
file.writeAsBytesSync(pdf.save());
await file.writeAsBytes(await pdf.save());
});
}
... ...
... ... @@ -135,8 +135,8 @@ void main() {
));
});
tearDownAll(() {
tearDownAll(() async {
final file = File('orientation.pdf');
file.writeAsBytesSync(pdf.save());
await file.writeAsBytes(await pdf.save());
});
}
... ...
... ... @@ -46,8 +46,8 @@ void main() {
build: (Context context) => Text('Hello World!')));
});
tearDownAll(() {
tearDownAll(() async {
final file = File('roll-paper.pdf');
file.writeAsBytesSync(pdf.save());
await file.writeAsBytes(await pdf.save());
});
}
... ...
... ... @@ -59,7 +59,7 @@ void printTextTtf(
}
void main() {
test('Pdf TrueType', () {
test('Pdf TrueType', () async {
final pdf = PdfDocument();
final page = PdfPage(pdf, pageFormat: const PdfPageFormat(500, 300));
... ... @@ -75,7 +75,7 @@ void main() {
page, g, '你好 檯號 ', File('genyomintw.ttf'), 30.0 + 30.0 * top++);
final file = File('ttf.pdf');
file.writeAsBytesSync(pdf.save());
await file.writeAsBytes(await pdf.save());
});
test('Font SubSetting', () {
... ...
... ... @@ -50,7 +50,7 @@ void printText(
}
void main() {
test('Pdf Type1 Embedded Fonts', () {
test('Pdf Type1 Embedded Fonts', () async {
final pdf = PdfDocument();
final page = PdfPage(pdf, pageFormat: const PdfPageFormat(500, 430));
... ... @@ -78,6 +78,6 @@ void main() {
printText(page, g, s, PdfFont.zapfDingbats(pdf), 20.0 + 30.0 * top++);
final file = File('type1.pdf');
file.writeAsBytesSync(pdf.save());
await file.writeAsBytes(await pdf.save());
});
}
... ...
... ... @@ -84,8 +84,8 @@ void main() {
);
});
tearDownAll(() {
tearDownAll(() async {
final file = File('widgets-barcode.pdf');
file.writeAsBytesSync(pdf.save());
await file.writeAsBytes(await pdf.save());
});
}
... ...
... ... @@ -201,8 +201,8 @@ void main() {
)));
});
tearDownAll(() {
tearDownAll(() async {
final file = File('widgets-basic.pdf');
file.writeAsBytesSync(pdf.save());
await file.writeAsBytes(await pdf.save());
});
}
... ...
... ... @@ -213,8 +213,8 @@ void main() {
});
});
tearDownAll(() {
tearDownAll(() async {
final file = File('widgets-chart.pdf');
file.writeAsBytesSync(pdf.save());
await file.writeAsBytes(await pdf.save());
});
}
... ...
... ... @@ -69,8 +69,8 @@ void main() {
));
});
tearDownAll(() {
tearDownAll(() async {
final file = File('widgets-clip.pdf');
file.writeAsBytesSync(pdf.save());
await file.writeAsBytes(await pdf.save());
});
}
... ...
... ... @@ -199,8 +199,8 @@ void main() {
));
});
tearDownAll(() {
tearDownAll(() async {
final file = File('widgets-container.pdf');
file.writeAsBytesSync(pdf.save());
await file.writeAsBytes(await pdf.save());
});
}
... ...
... ... @@ -109,8 +109,8 @@ void main() {
);
});
tearDownAll(() {
tearDownAll(() async {
final file = File('widgets-flex.pdf');
file.writeAsBytesSync(pdf.save());
await file.writeAsBytes(await pdf.save());
});
}
... ...
... ... @@ -137,8 +137,8 @@ void main() {
},
);
tearDownAll(() {
tearDownAll(() async {
final file = File('widgets-form.pdf');
file.writeAsBytesSync(pdf.save());
await file.writeAsBytes(await pdf.save());
});
}
... ...
... ... @@ -60,8 +60,8 @@ void main() {
));
});
tearDownAll(() {
tearDownAll(() async {
final file = File('widgets-gridview.pdf');
file.writeAsBytesSync(pdf.save());
await file.writeAsBytes(await pdf.save());
});
}
... ...
... ... @@ -66,8 +66,8 @@ void main() {
);
});
tearDownAll(() {
tearDownAll(() async {
final file = File('widgets-icons.pdf');
file.writeAsBytesSync(pdf.save());
await file.writeAsBytes(await pdf.save());
});
}
... ...
... ... @@ -28,7 +28,7 @@ void main() {
}
});
test('Pdf Widgets MultiPage', () {
test('Pdf Widgets MultiPage', () async {
Document.debug = true;
final pdf = Document();
... ... @@ -36,13 +36,13 @@ void main() {
pdf.addPage(MultiPage(build: (Context context) => lines));
final file = File('widgets-multipage.pdf');
file.writeAsBytesSync(pdf.save());
await file.writeAsBytes(await pdf.save());
final file1 = File('widgets-multipage-1.pdf');
file1.writeAsBytesSync(pdf.save());
await file1.writeAsBytes(await pdf.save());
});
test('Pdf Widgets MonoPage', () {
test('Pdf Widgets MonoPage', () async {
Document.debug = true;
final pdf = Document();
... ... @@ -50,9 +50,9 @@ void main() {
pdf.addPage(Page(build: (Context context) => Column(children: lines)));
final file = File('widgets-monopage.pdf');
file.writeAsBytesSync(pdf.save());
await file.writeAsBytes(await pdf.save());
final file1 = File('widgets-monopage-1.pdf');
file1.writeAsBytesSync(pdf.save());
await file1.writeAsBytes(await pdf.save());
});
}
... ...
... ... @@ -44,8 +44,8 @@ void main() {
);
});
tearDownAll(() {
tearDownAll(() async {
final file = File('widgets-opacity.pdf');
file.writeAsBytesSync(pdf.save());
await file.writeAsBytes(await pdf.save());
});
}
... ...
... ... @@ -83,8 +83,8 @@ void main() {
);
});
tearDownAll(() {
tearDownAll(() async {
final file = File('widgets-outline.pdf');
file.writeAsBytesSync(pdf.save());
await file.writeAsBytes(await pdf.save());
});
}
... ...
... ... @@ -54,8 +54,8 @@ void main() {
);
});
tearDownAll(() {
tearDownAll(() async {
final file = File('widgets-partitions.pdf');
file.writeAsBytesSync(pdf.save());
await file.writeAsBytes(await pdf.save());
});
}
... ...
... ... @@ -110,8 +110,8 @@ void main() {
);
});
tearDownAll(() {
tearDownAll(() async {
final file = File('widgets-svg.pdf');
file.writeAsBytesSync(pdf.save());
await file.writeAsBytes(await pdf.save());
});
}
... ...
... ... @@ -269,8 +269,8 @@ void main() {
);
});
tearDownAll(() {
tearDownAll(() async {
final file = File('widgets-table.pdf');
file.writeAsBytesSync(pdf.save());
await file.writeAsBytes(await pdf.save());
});
}
... ...
... ... @@ -279,8 +279,8 @@ void main() {
},
);
tearDownAll(() {
tearDownAll(() async {
final file = File('widgets.pdf');
file.writeAsBytesSync(pdf.save());
await file.writeAsBytes(await pdf.save());
});
}
... ...
... ... @@ -290,8 +290,8 @@ void main() {
);
});
tearDownAll(() {
tearDownAll(() async {
final file = File('widgets-text.pdf');
file.writeAsBytesSync(pdf.save());
await file.writeAsBytes(await pdf.save());
});
}
... ...
... ... @@ -126,8 +126,8 @@ void main() {
}));
});
tearDownAll(() {
tearDownAll(() async {
final file = File('widgets-theme.pdf');
file.writeAsBytesSync(pdf.save());
await file.writeAsBytes(await pdf.save());
});
}
... ...
... ... @@ -100,8 +100,8 @@ void main() {
);
});
tearDownAll(() {
tearDownAll(() async {
final file = File('widgets-watermark.pdf');
file.writeAsBytesSync(pdf.save());
await file.writeAsBytes(await pdf.save());
});
}
... ...
... ... @@ -315,8 +315,8 @@ void main() {
);
});
tearDownAll(() {
tearDownAll(() async {
final file = File('widgets-wrap.pdf');
file.writeAsBytesSync(pdf.save());
await file.writeAsBytes(await pdf.save());
});
}
... ...
# Changelog
## 4.0.0
- Remove deprecated methods
- Document.save() now returns a Future
## 3.7.2
- Fix Printing on WEB
... ...
... ... @@ -84,7 +84,7 @@ 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(doc.save());
await file.writeAsBytes(await doc.save());
```
You can also print the document using the iOS or Android print service:
... ... @@ -97,7 +97,7 @@ await Printing.layoutPdf(
Or share the document to other applications:
```dart
await Printing.sharePdf(bytes: doc.save(), filename: 'my-document.pdf');
await Printing.sharePdf(bytes: await doc.save(), filename: 'my-document.pdf');
```
To print an HTML document:
... ... @@ -113,7 +113,7 @@ 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(doc.save(), pages: [0, 1], dpi: 72)) {
await for (var page in Printing.raster(await doc.save(), pages: [0, 1], dpi: 72)) {
final image = page.toImage(); // ...or page.toPng()
print(image);
}
... ...
... ... @@ -126,19 +126,13 @@ mixin Printing {
/// Displays a platform popup to share the Pdf document to another application
static Future<bool> sharePdf({
@Deprecated('use bytes with document.save()') PdfDocument document,
Uint8List bytes,
@required Uint8List bytes,
String filename = 'document.pdf',
Rect bounds,
}) {
assert(document != null || bytes != null);
assert(!(document == null && bytes == null));
assert(bytes != null);
assert(filename != null);
if (document != null) {
bytes = document.save();
}
bounds ??= Rect.fromCircle(center: Offset.zero, radius: 10);
return PrintingPlatform.instance.sharePdf(
... ...
... ... @@ -4,7 +4,7 @@ description: Plugin that allows Flutter apps to generate and print documents to
homepage: https://github.com/DavBfr/dart_pdf/tree/master/printing
repository: https://github.com/DavBfr/dart_pdf
issue_tracker: https://github.com/DavBfr/dart_pdf/issues
version: 3.7.2
version: 4.0.0
environment:
sdk: ">=2.3.0 <3.0.0"
... ... @@ -18,7 +18,7 @@ dependencies:
image: ^2.1.4
js: ^0.6.1
meta: ^1.1.5
pdf: ^1.13.0
pdf: ^2.0.0
plugin_platform_interface: ^1.0.2
dev_dependencies:
... ...
... ... @@ -68,8 +68,8 @@ void main() {
doc = pw.Document();
});
tearDownAll(() {
tearDownAll(() async {
final file = File('printing.pdf');
file.writeAsBytesSync(doc.save());
await file.writeAsBytes(await doc.save());
});
}
... ...
... ... @@ -7,7 +7,7 @@ import 'package:string_scanner/string_scanner.dart';
const dpi = 72.0;
const px = dpi / PdfPageFormat.inch * PdfPageFormat.point;
void main() {
Future<void> main() async {
// Open self
final source = File('../test/github_social_preview.dart').readAsStringSync();
final code = DartSyntaxHighlighter(
... ... @@ -74,7 +74,7 @@ void main() {
// END
// Save the file
File('social_preview.pdf').writeAsBytesSync(pdf.save());
await File('social_preview.pdf').writeAsBytes(await pdf.save());
// Convert to png
Process.runSync('pdftocairo',
... ...