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
2021-09-10 11:13:44 -0300
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
3fa12b8245c9fab34b59c5d4aec7cbcb2169cd98
3fa12b82
1 parent
d0b50026
Add WidgetWraper.fromWidget()
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
129 additions
and
2 deletions
printing/CHANGELOG.md
printing/lib/src/widget_wrapper.dart
printing/pubspec.yaml
printing/CHANGELOG.md
View file @
3fa12b8
# Changelog
## 5.
5.1
## 5.
6.0
-
Update Google fonts
-
Fix typo in README
...
...
@@ -9,6 +9,7 @@
-
Fix error while loading shared libraries on Linux
-
Update pdfium library to 4627
-
Apply Flutter 2.5 coding style
-
Add WidgetWraper.fromWidget()
## 5.5.0
...
...
printing/lib/src/widget_wrapper.dart
View file @
3fa12b8
...
...
@@ -20,6 +20,7 @@ import 'dart:ui' as ui;
import
'package:flutter/material.dart'
;
import
'package:flutter/rendering.dart'
;
import
'package:flutter/widgets.dart'
;
import
'package:pdf/pdf.dart'
;
import
'package:pdf/widgets.dart'
as
pw
;
...
...
@@ -100,6 +101,118 @@ class WidgetWraper extends pw.ImageProvider {
);
}
/// Wrap a Flutter Widget to an ImageProvider.
///
/// ```
/// final wrapped = await WidgetWraper.fromWidget(
/// widget: Container(
/// color: Colors.white,
/// child: Text(
/// 'Hello world !',
/// style: TextStyle(color: Colors.amber),
/// ),
/// ),
/// constraints: BoxConstraints(maxWidth: 100, maxHeight: 400),
/// pixelRatio: 3,
/// );
///
/// pdf.addPage(
/// pw.Page(
/// pageFormat: format,
/// build: (context) {
/// return pw.Image(wrapped, width: 100);
/// },
/// ),
/// );
/// ```
static
Future
<
WidgetWraper
>
fromWidget
({
required
Widget
widget
,
required
BoxConstraints
constraints
,
double
pixelRatio
=
1.0
,
PdfImageOrientation
?
orientation
,
double
?
dpi
,
})
async
{
assert
(
pixelRatio
>
0
);
if
(!
constraints
.
hasBoundedHeight
||
!
constraints
.
hasBoundedHeight
)
{
throw
Exception
(
'Unable to convert an unbounded widget. Add maxWidth and maxHeight to the constraints.'
);
}
widget
=
ConstrainedBox
(
constraints:
constraints
,
child:
widget
,
);
final
_properties
=
DiagnosticPropertiesBuilder
();
widget
.
debugFillProperties
(
_properties
);
if
(
_properties
.
properties
.
isEmpty
)
{
throw
ErrorDescription
(
'Unable to get the widget properties'
);
}
final
_constraints
=
_properties
.
properties
.
whereType
<
DiagnosticsProperty
<
BoxConstraints
>>()
.
first
.
value
;
if
(
_constraints
==
null
||
!
_constraints
.
hasBoundedWidth
||
!
_constraints
.
hasBoundedWidth
)
{
throw
Exception
(
'Unable to convert an unbounded widget.'
);
}
final
_repaintBoundary
=
RenderRepaintBoundary
();
final
renderView
=
RenderView
(
child:
RenderPositionedBox
(
alignment:
Alignment
.
center
,
child:
_repaintBoundary
),
configuration:
ViewConfiguration
(
size:
Size
(
_constraints
.
maxWidth
,
_constraints
.
maxHeight
),
devicePixelRatio:
ui
.
window
.
devicePixelRatio
),
window:
_FlutterView
(
configuration:
ui
.
ViewConfiguration
(
devicePixelRatio:
ui
.
window
.
devicePixelRatio
,
),
),
);
final
pipelineOwner
=
PipelineOwner
()..
rootNode
=
renderView
;
renderView
.
prepareInitialFrame
();
final
buildOwner
=
BuildOwner
(
focusManager:
FocusManager
());
final
_rootElement
=
RenderObjectToWidgetAdapter
<
RenderBox
>(
container:
_repaintBoundary
,
child:
Directionality
(
textDirection:
TextDirection
.
ltr
,
child:
IntrinsicHeight
(
child:
IntrinsicWidth
(
child:
widget
)),
),
).
attachToRenderTree
(
buildOwner
);
buildOwner
..
buildScope
(
_rootElement
)
..
finalizeTree
();
pipelineOwner
..
flushLayout
()
..
flushCompositingBits
()
..
flushPaint
();
final
image
=
await
_repaintBoundary
.
toImage
(
pixelRatio:
pixelRatio
);
final
bytes
=
await
image
.
toByteData
(
format:
ui
.
ImageByteFormat
.
rawRgba
);
if
(
bytes
==
null
)
{
throw
Exception
(
'Unable to read image data'
);
}
return
WidgetWraper
.
_
(
bytes
.
buffer
.
asUint8List
(),
image
.
width
,
image
.
height
,
orientation
??
PdfImageOrientation
.
topLeft
,
dpi
,
);
}
/// The image data
final
Uint8List
bytes
;
...
...
@@ -114,3 +227,16 @@ class WidgetWraper extends pw.ImageProvider {
);
}
}
class
_FlutterView
extends
ui
.
FlutterView
{
_FlutterView
({
required
this
.
configuration
});
final
ui
.
ViewConfiguration
configuration
;
@override
ui
.
PlatformDispatcher
get
platformDispatcher
=>
ui
.
PlatformDispatcher
.
instance
;
@override
ui
.
ViewConfiguration
get
viewConfiguration
=>
configuration
;
}
...
...
printing/pubspec.yaml
View file @
3fa12b8
...
...
@@ -6,7 +6,7 @@ description: >
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
:
5.
5.1
version
:
5.
6.0
environment
:
sdk
:
"
>=2.12.0
<3.0.0"
...
...
Please
register
or
login
to post a comment