Skip to content

How to get Uint8List data of page? #1964

Closed
@xyzbilal

Description

@xyzbilal

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');

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions