8

How can I convert a PDF file into an image with Flutter?

I want to print the image to an ESC/POS printer using esc_pos_printer. This package won't accept PDFImage, it needs to be a Flutter Image.

I see plenty of PHP plugins that do this but nothing for Flutter.

edit: There is an answer to another question here which shows some code to decode an image from "pdf64" but I can't figure out exactly what "pdf64" is.

I created a PDF from html using flutter_html_to_pdflike this:

Directory appDocDir = await getApplicationDocumentsDirectory();
var targetPath = appDocDir.path;
var generatedPdfFile = await FlutterHtmlToPdf.convertFromHtmlContent(
    htmlContent, targetPath, targetFileName);
generatedPdfFilePath = generatedPdfFile.path;

Now I need to know how to create a Flutter Image from that PDF or the bytecode to send raw to the printer.

markhorrocks
  • 1,199
  • 19
  • 82
  • 151

1 Answers1

9

You can use https://pub.dev/packages/printing:

await for (var page in Printing.raster(document)) {
  final image = page.asImage();
  ...
}

This plugin can also convert your Html to Pdf with this:

final pdf = await Printing.convertHtml(
      format: PdfPageFormat.a4,
      html: '<html><body><p>Hello!</p></body></html>',
));
Dav
  • 497
  • 4
  • 8
  • Use pdf_merger for converting pdf into image it supports both android and IOS. Before call this package make sure you allow read write permission. https://pub.dev/packages/pdf_merger – Abhay Dhiman Jul 08 '21 at 08:58