createPhotoWithThumbnailFromPdfPage static method

Future<PhotoWithThumbnail> createPhotoWithThumbnailFromPdfPage(
  1. String filePath,
  2. String name,
  3. int pageNumber
)

Implementation

static Future<PhotoWithThumbnail> createPhotoWithThumbnailFromPdfPage(
    String filePath, String name, int pageNumber) async {
  final document = await PdfDocument.openFile(filePath);
  final page = await document.getPage(pageNumber);
  final pageImage = await page.render(
      width: page.width, height: page.height, format: PdfPageImageFormat.png);
  await page.close();
  if (pageImage == null) {
    throw Exception("Can't find render image $filePath");
  }
  var img = imgpackage.decodeImage(pageImage.bytes);
  if (img == null) {
    throw Exception('Could not decode image');
  }
  int thumbnailWidth = 0;
  int thumbnailHeight = 0;
  if (img.width > img.height) {
    thumbnailWidth = thumbnailSize;
  } else {
    thumbnailHeight = thumbnailSize;
  }
  var thumbnail = imgpackage.copyResize(img,
      width: thumbnailWidth, height: thumbnailHeight);
  var thumbNailData = Uint8List.fromList(imgpackage.encodePng(thumbnail));

  var baseName = '$name.png';
  var thumbnailBaseName = '$name.thumbnail.png';
  return PhotoWithThumbnail(
      photoData: ImageData(
          baseName: baseName,
          data: pageImage.bytes,
          width: img.width,
          height: img.height),
      thumbNailData: ImageData(
          baseName: thumbnailBaseName,
          width: thumbnailSize,
          height: thumbnailSize,
          data: thumbNailData));
}