Showing
39 changed files
with
70 additions
and
55 deletions
1 | -# Pdf creation library for dart / flutter | 1 | +# Pdf for Dart and Flutter |
2 | 2 | ||
3 | -This is a low-level Pdf creation library. | ||
4 | -It can create a full multi-pages document with graphics, | ||
5 | -images and text using TrueType fonts. | 3 | +This set of plugin allows Flutter apps to generate and print pdf files to device printer. |
4 | +This plugin works for iOS and Android. | ||
6 | 5 | ||
7 | -The coordinate system is using the internal Pdf system: | ||
8 | - * (0.0, 0.0) is bottom-left | ||
9 | - * 1.0 is defined as 1 / 72.0 inch | ||
10 | - * you can use the constants for centimeters, milimeters and inch defined in PDFPageFormat | ||
11 | - | ||
12 | -Example: | ||
13 | -```dart | ||
14 | -final pdf = new PDFDocument(); | ||
15 | -final page = new PDFPage(pdf, pageFormat: PDFPageFormat.LETTER); | ||
16 | -final g = page.getGraphics(); | ||
17 | -final font = new PDFFont(pdf); | ||
18 | - | ||
19 | -g.setColor(new PDFColor(0.0, 1.0, 1.0)); | ||
20 | -g.drawRect(50.0, 30.0, 100.0, 50.0); | ||
21 | -g.fillPath(); | ||
22 | - | ||
23 | -g.setColor(new PDFColor(0.3, 0.3, 0.3)); | ||
24 | -g.drawString(font, 12.0, "Hello World!", 5.0 * PDFPageFormat.MM, 300.0); | ||
25 | - | ||
26 | -var file = new File('file.pdf'); | ||
27 | -file.writeAsBytesSync(pdf.save()); | ||
28 | -``` | ||
29 | - | ||
30 | -To load an image it is possible to use the dart library `image` | ||
31 | - | ||
32 | -```dart | ||
33 | -Image image = decodeImage(new Io.File('test.webp').readAsBytesSync()); | ||
34 | -PDFImage image = new PDFImage( | ||
35 | - pdf, | ||
36 | - image: img.data.buffer.asUint8List(), | ||
37 | - width: img.width, | ||
38 | - height: img.height); | ||
39 | -g.drawImage(image, 100.0, 100.0, 80.0); | ||
40 | -``` | ||
41 | - | ||
42 | -To use a TrueType font: | ||
43 | - | ||
44 | -```dart | ||
45 | -PDFTTFFont ttf = new PDFTTFFont( | ||
46 | - pdf, | ||
47 | - (new File("open-sans.ttf").readAsBytesSync() as Uint8List).buffer.asByteData()); | ||
48 | -g.setColor(new PDFColor(0.3, 0.3, 0.3)); | ||
49 | -g.drawString(ttf, 20.0, "Dart is awesome", 50.0, 30.0); | ||
50 | -``` |
pdf/README.md
0 → 100644
1 | +# Pdf creation library for dart / flutter | ||
2 | + | ||
3 | +This is a low-level Pdf creation library. | ||
4 | +It can create a full multi-pages document with graphics, | ||
5 | +images and text using TrueType fonts. | ||
6 | + | ||
7 | +The coordinate system is using the internal Pdf system: | ||
8 | + * (0.0, 0.0) is bottom-left | ||
9 | + * 1.0 is defined as 1 / 72.0 inch | ||
10 | + * you can use the constants for centimeters, milimeters and inch defined in PDFPageFormat | ||
11 | + | ||
12 | +Example: | ||
13 | +```dart | ||
14 | +final pdf = new PDFDocument(); | ||
15 | +final page = new PDFPage(pdf, pageFormat: PDFPageFormat.LETTER); | ||
16 | +final g = page.getGraphics(); | ||
17 | +final font = new PDFFont(pdf); | ||
18 | + | ||
19 | +g.setColor(new PDFColor(0.0, 1.0, 1.0)); | ||
20 | +g.drawRect(50.0, 30.0, 100.0, 50.0); | ||
21 | +g.fillPath(); | ||
22 | + | ||
23 | +g.setColor(new PDFColor(0.3, 0.3, 0.3)); | ||
24 | +g.drawString(font, 12.0, "Hello World!", 5.0 * PDFPageFormat.MM, 300.0); | ||
25 | + | ||
26 | +var file = new File('file.pdf'); | ||
27 | +file.writeAsBytesSync(pdf.save()); | ||
28 | +``` | ||
29 | + | ||
30 | +To load an image it is possible to use the dart library `image` | ||
31 | + | ||
32 | +```dart | ||
33 | +Image image = decodeImage(new Io.File('test.webp').readAsBytesSync()); | ||
34 | +PDFImage image = new PDFImage( | ||
35 | + pdf, | ||
36 | + image: img.data.buffer.asUint8List(), | ||
37 | + width: img.width, | ||
38 | + height: img.height); | ||
39 | +g.drawImage(image, 100.0, 100.0, 80.0); | ||
40 | +``` | ||
41 | + | ||
42 | +To use a TrueType font: | ||
43 | + | ||
44 | +```dart | ||
45 | +PDFTTFFont ttf = new PDFTTFFont( | ||
46 | + pdf, | ||
47 | + (new File("open-sans.ttf").readAsBytesSync() as Uint8List).buffer.asByteData()); | ||
48 | +g.setColor(new PDFColor(0.3, 0.3, 0.3)); | ||
49 | +g.drawString(ttf, 20.0, "Dart is awesome", 50.0, 30.0); | ||
50 | +``` |
@@ -7,14 +7,17 @@ void main() { | @@ -7,14 +7,17 @@ void main() { | ||
7 | final page = new PDFPage(pdf, pageFormat: PDFPageFormat.LETTER); | 7 | final page = new PDFPage(pdf, pageFormat: PDFPageFormat.LETTER); |
8 | final g = page.getGraphics(); | 8 | final g = page.getGraphics(); |
9 | final font = new PDFFont(pdf); | 9 | final font = new PDFFont(pdf); |
10 | + final top = page.pageFormat.height; | ||
10 | 11 | ||
11 | g.setColor(new PDFColor(0.0, 1.0, 1.0)); | 12 | g.setColor(new PDFColor(0.0, 1.0, 1.0)); |
12 | - g.drawRect(50.0, 30.0, 100.0, 50.0); | 13 | + g.drawRect(50.0 * PDFPageFormat.MM, top - 80.0 * PDFPageFormat.MM, 100.0 * PDFPageFormat.MM, |
14 | + 50.0 * PDFPageFormat.MM); | ||
13 | g.fillPath(); | 15 | g.fillPath(); |
14 | 16 | ||
15 | g.setColor(new PDFColor(0.3, 0.3, 0.3)); | 17 | g.setColor(new PDFColor(0.3, 0.3, 0.3)); |
16 | - g.drawString(font, 12.0, "Hello World!", 50.0, 300.0); | 18 | + g.drawString( |
19 | + font, 12.0, "Hello World!", 10.0 * PDFPageFormat.MM, top - 10.0 * PDFPageFormat.MM); | ||
17 | 20 | ||
18 | - var file = new File('file.pdf'); | 21 | + var file = new File('example.pdf'); |
19 | file.writeAsBytesSync(pdf.save()); | 22 | file.writeAsBytesSync(pdf.save()); |
20 | } | 23 | } |
@@ -68,12 +68,16 @@ class PDFPage extends PDFObject { | @@ -68,12 +68,16 @@ class PDFPage extends PDFObject { | ||
68 | 68 | ||
69 | /// Returns the page's PageFormat. | 69 | /// Returns the page's PageFormat. |
70 | /// @return PageFormat describing the page size in device units (72dpi) | 70 | /// @return PageFormat describing the page size in device units (72dpi) |
71 | + /// use pageFormat | ||
72 | + @deprecated | ||
71 | PDFPageFormat getPageFormat() { | 73 | PDFPageFormat getPageFormat() { |
72 | return pageFormat; | 74 | return pageFormat; |
73 | } | 75 | } |
74 | 76 | ||
75 | /// Gets the dimensions of the page. | 77 | /// Gets the dimensions of the page. |
76 | /// @return a Dimension object containing the width and height of the page. | 78 | /// @return a Dimension object containing the width and height of the page. |
79 | + /// use pageFormat.dimension | ||
80 | + @deprecated | ||
77 | PDFPoint getDimension() => new PDFPoint(pageFormat.width, pageFormat.height); | 81 | PDFPoint getDimension() => new PDFPoint(pageFormat.width, pageFormat.height); |
78 | 82 | ||
79 | /// This adds an Annotation to the page. | 83 | /// This adds an Annotation to the page. |
@@ -34,4 +34,6 @@ class PDFPageFormat { | @@ -34,4 +34,6 @@ class PDFPageFormat { | ||
34 | final double height; | 34 | final double height; |
35 | 35 | ||
36 | const PDFPageFormat(this.width, this.height); | 36 | const PDFPageFormat(this.width, this.height); |
37 | + | ||
38 | + PDFPoint get dimension => new PDFPoint(width, height); | ||
37 | } | 39 | } |
1 | name: pdf | 1 | name: pdf |
2 | author: David PHAM-VAN <dev.nfet.net@gmail.com> | 2 | author: David PHAM-VAN <dev.nfet.net@gmail.com> |
3 | description: A pdf producer for Dart. It can create pdf files for both web or flutter. | 3 | description: A pdf producer for Dart. It can create pdf files for both web or flutter. |
4 | -homepage: https://github.com/davbfr/dart_pdf | 4 | +homepage: https://github.com/davbfr/dart_pdf/pdf |
5 | version: 1.0.3 | 5 | version: 1.0.3 |
6 | 6 | ||
7 | environment: | 7 | environment: |
@@ -30,7 +30,7 @@ void main() { | @@ -30,7 +30,7 @@ void main() { | ||
30 | 30 | ||
31 | var font2 = new PDFTTFFont( | 31 | var font2 = new PDFTTFFont( |
32 | pdf, | 32 | pdf, |
33 | - (new File("../assets/Nunito-Regular.ttf").readAsBytesSync() as Uint8List) | 33 | + (new File("open-sans.ttf").readAsBytesSync() as Uint8List) |
34 | .buffer | 34 | .buffer |
35 | .asByteData()); | 35 | .asByteData()); |
36 | var s = "Hello World!"; | 36 | var s = "Hello World!"; |
@@ -17,7 +17,7 @@ void main() { | @@ -17,7 +17,7 @@ void main() { | ||
17 | var g = page.getGraphics(); | 17 | var g = page.getGraphics(); |
18 | var ttf = new PDFTTFFont( | 18 | var ttf = new PDFTTFFont( |
19 | pdf, | 19 | pdf, |
20 | - (new File("../assets/Nunito-Regular.ttf").readAsBytesSync() as Uint8List) | 20 | + (new File("open-sans.ttf").readAsBytesSync() as Uint8List) |
21 | .buffer | 21 | .buffer |
22 | .asByteData()); | 22 | .asByteData()); |
23 | var s = "Hello World!"; | 23 | var s = "Hello World!"; |
@@ -30,7 +30,7 @@ void main() { | @@ -30,7 +30,7 @@ void main() { | ||
30 | g.setColor(new PDFColor(0.3, 0.3, 0.3)); | 30 | g.setColor(new PDFColor(0.3, 0.3, 0.3)); |
31 | g.drawString(ttf, FS, s, 50.0, 30.0); | 31 | g.drawString(ttf, FS, s, 50.0, 30.0); |
32 | 32 | ||
33 | - var file = new File('file.pdf'); | 33 | + var file = new File('file2.pdf'); |
34 | file.writeAsBytesSync(pdf.save()); | 34 | file.writeAsBytesSync(pdf.save()); |
35 | }); | 35 | }); |
36 | } | 36 | } |
-
Please register or login to post a comment