flutterware
All guides
Drawing

Renders

Turn a Flutter widget into a file: SVG, PNG or PDF. Draw a chart, an invoice or a certificate with your app's widgets, then render it from a script, from the command line, or from a Dart server that has no screen.

The SVG and PDF are real vector documents: text stays text, and anything a vector format can't express is reported rather than silently dropped.

Add the package#

Rendering is in a separate package, flutterware_render. It is not on pub.dev, so add it from GitHub to the app that draws the render points, at the tag of the flutterware version the app uses:

# pubspec.yaml
dependencies:
  flutterware_render:
    git:
      url: https://github.com/flutterware/flutterware
      path: render
      ref: v0.7.0

When you upgrade flutterware, change ref to the new version's tag.

Declare what can be rendered#

A render point is a named, typed entry: what it's called and what arguments it takes. The app then says how to draw each one:

// lib/renders.dart
import 'package:material_ui/material_ui.dart';
import 'package:flutterware_render/flutterware_render.dart';

final monthlyChart = WidgetRender<ChartRequest>(
  'charts/monthly',
  encodeArgs: (args) => args.toJson(),
  decodeArgs: ChartRequest.fromJson,
);

@RenderRegistry()
void registerRenders(RenderHost host) {
  host.widget(monthlyChart, (context, args) => MonthlyChart(args));
}

DocumentRender works the same way for a multi-page PDF built with package:pdf, and a document can include a widget captured as SVG.

A document of designed pages#

When each page of a document is designed as a whole, like a report or a certificate, draw every page as a widget and add them to the document one by one:

final patientReport = DocumentRender<ReportRequest>(
  'reports/patient',
  encodeArgs: (args) => args.toJson(),
  decodeArgs: ReportRequest.fromJson,
);

host.document(patientReport, (context, args) async {
  var document = pw.Document();
  for (var page in reportPages(args)) {
    await context.addPage(
      document,
      page,
      size: const Size(794, 1123),
      pageFormat: PdfPageFormat.a4,
    );
  }
  return document;
});

Each page is laid out at size, in logical pixels, and scaled to fit pageFormat. Its text stays text, in the font file of its weight. The pages share their fonts and their images, so a font or a photo used on every page is stored once.

Photos#

A photo shown with Image.memory or a MemoryImage goes into a PDF as its pixels. Use EncodedMemoryImage instead, with the same bytes: a JPEG then goes in as it is, at its own quality and size.

Image(image: EncodedMemoryImage(jpegBytes), fit: BoxFit.cover)

The images a widget loads are waited for before it is captured. In a widget test, capture inside tester.runAsync, or the images never arrive.

Turn it on#

// tool/flutterware.dart
fw.use(Renders(packages: [.new(app)]));

The registrar is looked for in lib/renders.dart; target: points elsewhere.

In the studio, the Renders panel lists the points and renders them live.

From the command line#

fw render charts/monthly --as=svg --size=400x200 \
    --args='{"title": "March", "values": [3, 5, 2]}' -o chart.svg
fw render reports/chart --as=pdf --args=@request.json -o report.pdf

From a server#

A server can't run Flutter itself, so the render points are compiled into a bundle that a pool of background renderers loads:

fw render bundle --out=build/render-bundle
import 'package:flutterware/render_client.dart';

var pool = await RenderPool.start(bundle: 'build/render-bundle');
var chart = await pool.svg(
  monthlyChart,
  ChartRequest(title: 'March', values: [3, 5, 2]),
  size: RenderSize(400, 200),
);
print(chart.text);

render_client.dart is in flutterware and doesn't import Flutter, so the server doesn't need flutterware_render. Keep the render points in a file that imports only package:flutterware/render_contract.dart, so the server and the app share them. The deployed server needs the bundle directory, not a Flutter SDK.

A renderer in the pool serves one request at a time and keeps nothing of it, images included, so its memory stays the same from one render to the next.

Render any widget, in your own code#

Without declaring anything, flutterware_render also has captureWidgetSvg, captureWidgetPdf and captureWidgetPng, which render a widget offscreen from inside a Flutter app or test.

Reference#

flutterware.render in the capabilities reference, and fw help render.

Edit this page on GitHub