Can we convert PDF into image using flutter in order to show thumbnail of the file?
Solution 1
Correct Answer: Using Printing and pdf plugin, In order to convert PDF to image we can simply achieve this by:
// send pdfFile as params
imageFromPdfFile(File pdfFile) async {
final document = await lib.PDFDocument.openFile(pdfFile.path);
final page = await document.getPage(1);
final pageImage = await page.render(width: page.width, height: page.height);
await page.close();
print(pageImage.bytes);
//... now convert
// .... pageImage.bytes to image
}
Solution 2
Using pdf_render and image plugins.
import 'package:pdf_render/pdf_render.dart';
import 'package:image/image.dart' as imglib;
final doc = await PdfDocument.openFile('abc.pdf');
final pages = doc.pageCount;
List<imglib.Image> images = [];
// get images from all the pages
for (int i = 1; i <= pages; i++) {
var page = await doc.getPage(i);
var imgPDF = await page.render();
var img = await imgPDF.createImageDetached();
var imgBytes = await img.toByteData(format: ImageByteFormat.png);
var libImage = imglib.decodeImage(imgBytes.buffer
.asUint8List(imgBytes.offsetInBytes, imgBytes.lengthInBytes));
images.add(libImage);
}
// stitch images
int totalHeight = 0;
images.forEach((e) {
totalHeight += e.height;
});
int totalWidth = 0;
images.forEach((element) {
totalWidth = totalWidth < element.width ? element.width : totalWidth;
});
final mergedImage = imglib.Image(totalWidth, totalHeight);
int mergedHeight = 0;
images.forEach((element) {
imglib.copyInto(mergedImage, element, dstX: 0, dstY: mergedHeight, blend: false);
mergedHeight += element.height;
});
// Save image as a file
final documentDirectory = await getExternalStorageDirectory();
File imgFile = new File('${documentDirectory.path}/abc.jpg');
new File(imgFile.path).writeAsBytes(imglib.encodeJpg(mergedImage));
jazzbpn
With 6+ years of experience, I have developed many iOS/android applications in different context, professional and oldest passion for computer programming began very early in my life. I've learned the social environment is as important as logic aspects of the developing approach then I appreciate very much to get in touch with positive and eager colleagues that involve me in new and exciting challenges. This is why I still want to get involved in new opportunities to improve my skillness.
Updated on December 19, 2022Comments
-
jazzbpn over 1 year
Use-Case: In order to show thumbnail of the PDF in file list.
Question 2: Can we convert FPF to image to show thumbnail in the list?
-
akshay yadav almost 4 yearsi guss you refer wrong plugin because pdf plugin not contain PDFDocument method lib.PDFDocument.openFile(pdfFile.path); it is only for creating pdf you can use pdf_render instead..
-
Mahmoud Salah Eldin about 3 yearslib.PDFDocument.openFile(pdfFile.path); df plugin not contain PDFDocument
-
Sparks almost 3 yearsthink you are refering to another plugin like @akshayyadav told you bedore the pdf plugin doesn't have any PDFDocument class.
-
jazzbpn almost 3 yearsPDFDocument class updated to Document class. Please go through the plugin docs.