syncfusion / flutter-widgets

Syncfusion Flutter widgets libraries include high quality UI widgets and file-format packages to help you create rich, high-quality applications for iOS, Android, and web from a single code base.
1.5k stars 708 forks source link

How to get Uint8List data of page? #1964

Open xyzbilal opened 3 weeks ago

xyzbilal commented 3 weeks ago

Use case

Create thumbnail of pdf for certain page.

Proposal

Hi, I m workin on a e-newspaper app with serverpod and I try to create an image thumbnail when user uploads a pdf, I want to get first page and create jpeg image from it. its possible to create with pdfrx page on client site but because it requires flutter ui, I cant use it server side. My question is if I can get Uint8List data of page in pdf. I share my example function for reference. regards.

Future<void> savePdfFirstPageAsImage(String pdfPath) async {

  final pdfDocument = await PdfDocument.openFile(pdfPath);

  final page = await pdfDocument.pages.first;

  final pageImage = await page.render( // this requires ui
    width: page.width,
    height: page.height,
  );

  Uint8List pageData = pageImage!.bytes;

  final image = img.decodePng(pageData);

  final directory = await getApplicationDocumentsDirectory();
  final imagePath = '${directory.path}/first_page.png';
  final imageFile = File(imagePath);

  await imageFile.writeAsBytes(img.encodePng(image!));

  // Kaydedilen yol
  print('Image saved at: $imagePath');

}
immankumarsync commented 3 weeks ago

Hi @xyzbilal, At present, we do not support exporting a page as an image using the PdfPage API. However, you can extract the PDF page as an image using the syncfusion_flutter_pdfviewer package.

You can find the sample code in the issue #1879. Please confirm us whether the solution worked for you

xyzbilal commented 2 weeks ago

Actually this is not exact solution because it requires ui and in serverpod server side ui is not available. I was looking for solution without importing dart ui. Your suggestion works in client side. if you do not have solution in pdf then we will implement this in client site with pdf viewer. thanks.

immankumarsync commented 1 week ago

Unfortunately, extracting the PDF page image without using the Dart UI library is not currently possible. If you need further assistance or have other questions, please feel free to ask.