savePageAsPng method

PdfiumWrap savePageAsPng(
  1. String outPath, {
  2. int? width,
  3. int? height,
  4. int backgroundColor = 268435455,
  5. double scale = 1,
  6. int rotate = 0,
  7. int flags = 0,
  8. bool flush = false,
  9. int pngLevel = 6,
})

Saves the loaded page as png image

Throws an PdfiumException if no page is loaded. Returns a instance of PdfiumWrap

Implementation

PdfiumWrap savePageAsPng(String outPath,
    {int? width,
    int? height,
    int backgroundColor = 268435455,
    double scale = 1,
    int rotate = 0,
    int flags = 0,
    bool flush = false,
    int pngLevel = 6,}) {
  if (_page == nullptr) {
    throw PdfiumException(message: 'Page not load');
  }
  // var backgroundStr = "FFFFFFFF"; // as int 268435455
  final w = ((width ?? getPageWidth()) * scale).round();
  final h = ((height ?? getPageHeight()) * scale).round();

  final bytes = renderPageAsBytes(w, h,
      backgroundColor: backgroundColor, rotate: rotate, flags: flags,);

  final Image image = Image.fromBytes(
    width: w,
    height: h,
    bytes: bytes.buffer,
    order: ChannelOrder.bgra,
    numChannels: 4,
  );

  // save bitmap as PNG.
  File(outPath)
      .writeAsBytesSync(encodePng(image, level: pngLevel), flush: flush);
  return this;
}