Toggle navigation
Toggle navigation
This project
Loading...
Sign in
flutter_package
/
dart_pdf
Go to a project
Toggle navigation
Projects
Groups
Snippets
Help
Toggle navigation pinning
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Authored by
David PHAM-VAN
2019-06-08 09:56:16 -0400
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
a940af96aae75a774ca7df5431e7edff913340cf
a940af96
1 parent
d47953b6
Update Readme with more examples
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
199 additions
and
38 deletions
pdf/CHANGELOG.md
pdf/README.md
pdf/pubspec.yaml
printing/CHANGELOG.md
printing/README.md
printing/pubspec.yaml
test/extract_readme.dart
test/pubspec.yaml
pdf/CHANGELOG.md
View file @
a940af9
# 1.3.11
*
Update Readme
# 1.3.10
*
Deprecate the document argument in Printing.sharePdf()
*
Add default value to alpha in PdfColor variants
...
...
pdf/README.md
View file @
a940af9
...
...
@@ -10,8 +10,8 @@ images and text using TrueType fonts. With the ease of use you already know.
<img
alt=
"Example document"
src=
"https://raw.githubusercontent.com/DavBfr/dart_pdf/master/pdf/example.jpg"
>
> Use the `printing` package <https://pub.dartlang.org/packages/printing>
> for full flutter print and share operation.
Use the
`printing`
package
<https://pub.dartlang.org/packages/printing>
for full flutter print and share operation.
The coordinate system is using the internal Pdf unit:
*
1.0 is defined as 1 / 72.0 inch
...
...
@@ -19,8 +19,9 @@ The coordinate system is using the internal Pdf unit:
Example:
```
dart
final
pdf
=
Document
()
..
addPage
(
Page
(
final
pdf
=
Document
();
pdf
.
addPage
(
Page
(
pageFormat:
PdfPageFormat
.
a4
,
build:
(
Context
context
)
{
return
Center
(
...
...
@@ -32,30 +33,43 @@ final pdf = Document()
To load an image it is possible to use the dart library
[
image
](
https://pub.dartlang.org/packages/image
)
:
```
dart
Image
image
=
decodeImage
(
Io
.
File
(
'test.webp'
).
readAsBytesSync
());
PdfImage
image
=
PdfImage
(
pdf
,
final
img
=
decodeImage
(
File
(
'test.webp'
).
readAsBytesSync
());
final
image
=
PdfImage
(
pdf
.
document
,
image:
img
.
data
.
buffer
.
asUint8List
(),
width:
img
.
width
,
height:
img
.
height
);
g
.
drawImage
(
image
,
100.0
,
100.0
,
80.0
);
height:
img
.
height
,
);
pdf
.
addPage
(
Page
(
build:
(
Context
context
)
{
return
Center
(
child:
Image
(
image
),
);
// Center
}));
// Page
```
To use a TrueType font:
```
dart
PdfTtfFont
ttf
=
PdfTtfFont
(
pdf
,
(
File
(
"open-sans.ttf"
).
readAsBytesSync
()
as
Uint8List
).
buffer
.
asByteData
());
g
.
setColor
(
PdfColor
(
0.3
,
0.3
,
0.3
));
g
.
drawString
(
ttf
,
20.0
,
"Dart is awesome"
,
50.0
,
30.0
);
final
Uint8List
fontData
=
File
(
'open-sans.ttf'
).
readAsBytesSync
();
final
ttf
=
Font
.
ttf
(
fontData
.
buffer
.
asByteData
());
pdf
.
addPage
(
Page
(
pageFormat:
PdfPageFormat
.
a4
,
build:
(
Context
context
)
{
return
Center
(
child:
Text
(
'Hello World'
,
style:
TextStyle
(
font:
ttf
,
fontSize:
40
)),
);
// Center
}));
// Page
```
To save the
image on Flutter, use the
[
path_provider
](
https://pub.dartlang.org/packages/path_provider
)
library
:
To save the
pdf file
:
```
dart
Directory
tempDir
=
await
getTemporaryDirectory
();
String
tempPath
=
tempDir
.
path
;
var
file
=
File
(
"
$tempPath
/file.pdf"
);
// On Flutter, use the [path_provider](https://pub.dartlang.org/packages/path_provider) library:
// final output = await getTemporaryDirectory();
// final file = File("${output.path}/example.pdf");
final
file
=
File
(
"example.pdf"
);
await
file
.
writeAsBytes
(
pdf
.
save
());
```
...
...
pdf/pubspec.yaml
View file @
a940af9
...
...
@@ -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.3.1
0
version
:
1.3.1
1
environment
:
sdk
:
"
>=2.1.0
<3.0.0"
...
...
printing/CHANGELOG.md
View file @
a940af9
# 2.0.4
*
Update Readme
# 2.0.3
*
Add file save and view to example application
*
Convert print screen example to Widgets
...
...
printing/README.md
View file @
a940af9
...
...
@@ -7,32 +7,71 @@ See the example on how to use the plugin.
<img
alt=
"Example document"
src=
"https://raw.githubusercontent.com/DavBfr/dart_pdf/master/printing/example.png"
width=
"300"
>
> This plugin uses the `pdf` package <https://pub.dartlang.org/packages/pdf>
> for pdf creation. Please refer to <https://pub.dartlang.org/documentation/pdf/latest/>
> for documentation.
This plugin uses the
`pdf`
package
<https://pub.dartlang.org/packages/pdf>
for pdf creation. Please refer to
<https://pub.dartlang.org/documentation/pdf/latest/>
for documentation.
To load an image it is possible to use
[
Image.toByteData
](
https://docs.flutter.io/flutter/dart-ui/Image/toByteData.html
)
:
Example:
```
dart
var
Image
im
;
var
bytes
=
await
im
.
toByteData
(
format:
ui
.
ImageByteFormat
.
rawRgba
);
PdfImage
image
=
PdfImage
(
pdf
,
image:
bytes
.
buffer
.
asUint8List
(),
width:
im
.
width
,
height:
im
.
height
);
g
.
drawImage
(
image
,
100.0
,
100.0
,
80.0
);
final
pdf
=
PdfDoc
();
pdf
.
addPage
(
Page
(
pageFormat:
PdfPageFormat
.
a4
,
build:
(
Context
context
)
{
return
Center
(
child:
Text
(
"Hello World"
),
);
// Center
}));
// Page
```
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
);
pdf
.
addPage
(
Page
(
build:
(
Context
context
)
{
return
Center
(
child:
Image
(
image
),
);
// Center
}));
// Page
```
To use a TrueType font from a flutter bundle:
```
dart
var
font
=
await
rootBundle
.
load
(
"assets/open-sans.ttf"
);
PdfTtfFont
ttf
=
PdfTtfFont
(
pdf
,
font
);
g
.
setColor
(
PdfColor
(
0.3
,
0.3
,
0.3
));
g
.
drawString
(
ttf
,
20.0
,
"Dart is awesome"
,
50.0
,
30.0
);
final
font
=
await
rootBundle
.
load
(
"assets/open-sans.ttf"
);
final
ttf
=
Font
.
ttf
(
font
);
pdf
.
addPage
(
Page
(
build:
(
Context
context
)
{
return
Center
(
child:
Text
(
'Dart is awesome'
,
style:
TextStyle
(
font:
ttf
,
fontSize:
40
)),
);
// Center
}));
// Page
```
To save the pdf file using the
[
path_provider
](
https://pub.dartlang.org/packages/path_provider
)
library:
```
dart
final
output
=
await
getTemporaryDirectory
();
final
file
=
File
(
"
${output.path}
/example.pdf"
);
await
file
.
writeAsBytes
(
pdf
.
save
());
```
You can also print the document using the iOS or Android print service:
```
dart
await
Printing
.
layoutPdf
(
onLayout:
(
PdfPageFormat
format
)
async
=>
pdf
.
save
());
```
Or share the document to other applications:
```
dart
await
Printing
.
sharePdf
(
bytes:
pdf
.
save
(),
filename:
'my-document.pdf'
);
```
## Installing
...
...
printing/pubspec.yaml
View file @
a940af9
...
...
@@ -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
:
2.0.
3
version
:
2.0.
4
environment
:
sdk
:
"
>=2.1.0
<3.0.0"
...
...
test/extract_readme.dart
0 → 100644
View file @
a940af9
/*
* 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.
*/
import
'dart:io'
;
import
'package:markdown/markdown.dart'
as
md
;
Iterable
<
String
>
getCode
(
List
<
md
.
Node
>
nodes
,
[
bool
isCode
=
false
])
sync
*
{
for
(
md
.
Node
node
in
nodes
)
{
if
(
node
is
md
.
Element
)
{
// print(node.tag);
// print(node.attributes);
yield
*
getCode
(
node
.
children
,
node
.
tag
==
'code'
&&
node
.
attributes
[
'class'
]
==
'language-dart'
);
}
else
if
(
node
is
md
.
Text
)
{
if
(
isCode
)
{
yield
'// ------------'
;
yield
node
.
text
;
}
}
else
{
print
(
node
);
}
}
}
void
main
(
)
{
final
md
.
Document
document
=
md
.
Document
(
extensionSet:
md
.
ExtensionSet
.
commonMark
,
encodeHtml:
false
,
);
final
output
=
File
(
'readme.dart'
);
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
\'
;'
);
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
(
'}'
);
}
{
final
data
=
File
(
'../printing/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 printingReadme() async {'
);
st
.
writeln
(
code
.
join
(
'
\n
'
));
st
.
writeln
(
'}'
);
}
st
.
writeln
(
'Future main() async {'
);
st
.
writeln
(
'await pdfReadme();'
);
st
.
writeln
(
'await printingReadme();'
);
st
.
writeln
(
'}'
);
st
.
close
();
}
...
...
test/pubspec.yaml
0 → 100644
View file @
a940af9
name
:
readme_to_dart
author
:
David PHAM-VAN <dev.nfet.net@gmail.com>
description
:
Readme Markdown to dart source converter.
homepage
:
https://github.com/DavBfr/dart_pdf/tree/master/test
repository
:
https://github.com/DavBfr/dart_pdf
issue_tracker
:
https://github.com/DavBfr/dart_pdf/issues
version
:
1.0.0
environment
:
sdk
:
"
>=2.1.0
<3.0.0"
dependencies
:
pdf
:
printing
:
markdown
:
meta
:
string_scanner
:
path
:
image
:
path_provider
:
...
...
Please
register
or
login
to post a comment