The FlutterToPDF package lets you export any Flutter widget to PDF Documents and is written natively in Dart.
Installing
Depend on it
Run this command:
$ flutter pub add flutter_to_pdf
This will add a line like this to your package's pubspec.yaml (and run an implicit flutter pub get
):
dependencies:
flutter_to_pdf: ^0.2.2
Alternatively, your editor might support flutter pub get
. Check the docs for your editor to learn more.
Import it
Now in your Dart code, you can use:
import 'package:flutter_to_pdf/flutter_to_pdf.dart';
Features
- export to PDF Document
- export to PDF Page
- export to PDF Widget
- export with options
- Page format options
- Text field options
- Checkbox options
Usage
Create an instance of the ExportDelegate
, wrap your widget with a ExportFrame
widget and provide a frameId
, as well as the exportDelegate
.
When you want to export the widget call the desired export function (either exportToPdfDocument
, exportToPdfPage
or exportToPdfWidget
) and pass in the frameId
.
// create instance of ExportDelegate
final ExportDelegate exportDelegate = ExportDelegate();
ExportFrame(
frameId: 'someFrameId',
child: SomeWidget(), // the widget you want to export
)
// export the frame to a PDF Document
final pdf = await exportDelegate.exportToPdfDocument('someFrameId');
// export the frame to a PDF Page
final page = await exportDelegate.exportToPdfPage('someFrameId');
// export the frame to a PDF Widget
final widget = await exportDelegate.exportToPdfWidget('someFrameId');
Note: If you wish to use types on the variables pdf
, page
and widget
, you can use the Document
, Page
and Widget
classes provided by PDF-package.
Custom TTF Fonts
If the widget you want to export contains any custom TTF fonts, you must provide the used ttfFonts
to the ExportDelegate
.
final ExportDelegate exportDelegate = ExportDelegate(
ttfFonts: {
'SomeFontFamily': 'assets/fonts/someFont.ttf',
},
);
TextFields and Checkboxes
If the widget you want to export contains any TextField
, TextFormField
or Checkbox
, a unique Key
must be provided to each TextField
, TextFormField
and Checkbox
.
TextField(
key: Key('someUniqueTextFieldKey'),
controller: TextEditingController(),
...
)
Checkbox(
key: Key('someUniqueCheckboxKey'),
value: false,
...
)
Other widgets like CustomPaint and Charts
If the widget you want to export contains any custom widgets like CustomPaint
or Charts
, the widgets you want to export must be wrapped in a CaptureWrapper
widget. Furthermore, the CaptureWrapper
must have a unique Key
.
These widgets will then be captured as an image and exported to the PDF Document.
CaptureWrapper(
key: const Key('someUniqueCustomPaintKey'),
child: CustomPaint(
size: const Size(300, 300),
painter: SomePainter(),
),
)
Export Options
The ExportDelegate
´s constructor has a named optional parameter options
which can be used to customize the export process.
final ExportOptions options = ExportOptions(
pageFormatOptions: PageFormatOptions.a3(),
textFieldOptions: TextFieldOptions.uniform(
interactive: false,
),
checkboxOptions: CheckboxOptions.uniform(
interactive: false,
),
);
final ExportDelegate exportDelegate = ExportDelegate(options: options);
The ExportDelegate
´s options
can also be overriden using the named optional parameter overrideOptions
in the export function.
final ExportOptions options = ExportOptions(
pageFormatOptions: PageFormatOptions.a3(),
textFieldOptions: TextFieldOptions.uniform(
interactive: false,
),
checkboxOptions: CheckboxOptions.uniform(
interactive: false,
),
);
final ExportDelegate exportDelegate = ExportDelegate(options: options);
final ExportOptions overrideOptions = ExportOptions(
pageFormatOptions: PageFormatOptions.a4(),
textFieldOptions: TextFieldOptions.uniform(
interactive: true,
),
checkboxOptions: CheckboxOptions.uniform(
interactive: true,
),
);
// export the frame to a PDF Document, using the override options
final pdf = await exportDelegate.exportToPdfDocument('someFrameId', overrideOptions: overrideOptions);
// export the frame to a PDF Page, using the override options
final page = await exportDelegate.exportToPdfPage('someFrameId', overrideOptions: overrideOptions);
// export the frame to a PDF Widget, using the override options
final widget = await exportDelegate.exportToPdfWidget('someFrameId', overrideOptions: overrideOptions);
Page Format Options
The PageFormatOptions
class can be used to customize the page format of the exported PDF Document.
All constructors have an optional parameter clip
which can be used to define if the content of the page should be clipped to the page format. The default value is true
.
For the PageFormatOptions
class there are several predefined constructors available:
PageFormatOptions.a3
- A3 page formatPageFormatOptions.a4
- A4 page formatPageFormatOptions.a5
- A5 page formatPageFormatOptions.a6
- A6 page formatPageFormatOptions.letter
- Letter page formatPageFormatOptions.legal
- Legal page formatPageFormatOptions.roll57
- Roll57 page formatPageFormatOptions.roll80
- Roll80 page formatPageFormatOptions.custom
- Custom page formatThe custom page format can be used to define a custom page format.
It therefore requires at least thewidth
parameter, and has the following optional parameters:height
,marginTop
,marginBottom
,marginLeft
,marginRight
andmarginAll
.
Text Field Options
The TextFieldOptions
class can be used to customize the text fields of the exported PDF Document.
All constructors have an optional parameter interactive
which can be used to define if the text fields should be interactive. The default value is true
.
If interactive
is set to false
, the text fields will be rendered as Text.
For the TextFieldOptions
class there are several predefined constructors available:
-
TextFieldOptions.uniform
- Uniform text field optionsThe uniform text field options can be used to define a uniform
TextStyle
for all text fields.
It therefore has an optional parametertextstyle
which can be used to define a customTextStyle
for all text fields. -
TextFieldOptions.individual
- Individual text field optionsThe individual text field options can be used to define an individual
TextStyle
for each text field.
It has an optional parameterstyleMap
which maps the textfield´sKey
to aTextStyle
.
Note: The TextField therefore requires aKey
to be able to map theTextStyle
to the text field. -
TextFieldOptions.none
- No text field optionsThe text fields will be interactive and will be styled as found in the widget tree.
Checkbox Options
The CheckboxOptions
class can be used to customize the checkboxes of the exported PDF Document.
All constructors have an optional parameter interactive
which can be used to define if the checkboxes should be interactive. The default value is true
.
If interactive
is set to false
, the checkboxes will be rendered as a Container and will not be clickable.
For the CheckboxOptions
class there are several predefined constructors available:
-
CheckboxOptions.uniform
- Uniform checkbox optionsThe uniform checkbox options can be used to define a uniform
BoxDecoration
for all checkboxes.
It therefore has an optional parameterboxDecoration
which can be used to define a customBoxDecoration
for all checkboxes. -
CheckboxOptions.individual
- Individual checkbox optionsThe individual checkbox options can be used to define an individual
BoxDecoration
for each checkbox.
It has an optional parameterdecorationMap
which maps the checkbox´sKey
to aBoxDecoration
.
Note: The Checkbox therefore requires aKey
to be able to map theBoxDecoration
to the checkbox. -
CheckboxOptions.none
- No checkbox optionsThe checkboxes will be interactive and will be styled as found in the widget tree.
Libraries
- args/alignment
- args/axis
- args/border_radius
- args/border_side
- args/border_style
- args/box_constraints
- args/box_decoration
- args/box_fit
- args/color
- args/cross_axis_alignment
- args/edge_insets
- args/flex_fit
- args/gradient
- args/image_provider
- args/main_axis_alignment
- args/main_axis_size
- args/text_align
- args/text_direction
- args/text_style
- args/vertical_direction
- capture_wrapper
- export_delegate
- export_frame
- export_instance
- flutter_to_pdf
- options/checkbox_options
- options/export_options
- options/font_data
- options/page_format_options
- options/text_field_options
- utils
- widgets/align
- widgets/center
- widgets/checkbox
- widgets/clip
- widgets/column
- widgets/constrained_box
- widgets/container
- widgets/divider
- widgets/expanded
- widgets/fitted_box
- widgets/flexible
- widgets/grid_view
- widgets/image
- widgets/limited_box
- widgets/list_view
- widgets/opacity
- widgets/padding
- widgets/placeholder
- widgets/positioned
- widgets/row
- widgets/sized_box
- widgets/stack
- widgets/table
- widgets/text
- widgets/text_field
- widgets/transform
- widgets/wrap