exportPage static method

Future<Uint8List> exportPage(
  1. PdfPage page, {
  2. PdfRasterFormat format = PdfRasterFormat.png,
  3. double dpi = 150,
  4. double? scale,
  5. Color pageColor = const Color(0xFFFFFFFF),
  6. bool annotations = true,
  7. int? rotation,
  8. int jpegQuality = 90,
})

Renders page and encodes it as format bytes.

dpi sets the resolution (default 150); scale overrides it with an explicit pixels-per-point ratio when given. pageColor is the paper painted under the content (and what JPEG flattens transparency onto); annotations toggles drawing the page's annotations; rotation overrides the page's own /Rotate for the export. jpegQuality (1–100) applies to PdfRasterFormat.jpeg.

Returns a complete image file's bytes - write them straight to disk or upload them.

Implementation

static Future<Uint8List> exportPage(
  PdfPage page, {
  PdfRasterFormat format = PdfRasterFormat.png,
  double dpi = 150,
  double? scale,
  Color pageColor = const Color(0xFFFFFFFF),
  bool annotations = true,
  int? rotation,
  int jpegQuality = 90,
}) async {
  if (dpi <= 0) throw ArgumentError.value(dpi, 'dpi', 'must be positive');
  if (scale != null && scale <= 0) {
    throw ArgumentError.value(scale, 'scale', 'must be positive');
  }
  final pixelRatio = scale ?? dpi / 72;
  final image = await PdfPageRenderer.renderImage(
    page,
    pixelRatio: pixelRatio,
    pageColor: pageColor,
    annotations: annotations,
    rotation: rotation,
  );
  try {
    return await _encode(image, format, pageColor, jpegQuality);
  } finally {
    image.dispose();
  }
}