I'm uploading a PDF and MS Word docs to a flutter app. I get the File by using file_picker package, like this File file = await FilePicker.getFile(). Once I obtain the file from the user, is there a way to show a thumbnail or small image of the first page of the document?
1 Answer
You can use thumbnailer or pdf_thumbnail package on pub.dev. I've developed the pdf_thumbnail package and it uses pdfx package to render pdf pages one by one, as image. You can use the package to display pdf thumbnails or copy my implementation from github.
Here's how to achieve with pdfx: Open the file, render each page in loop.
final document = await PdfDocument.openFile(filePath);
for (var pageNumber = 1; pageNumber <= document.pagesCount; pageNumber++) {
final page = await document.getPage(pageNumber);
final pageImage = await page.render(
width: page.width,
height: page.height,
);
images[pageNumber] = pageImage!.bytes;
await page.close();
}