Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a PDF file to an image with Flutter?

Tags:

flutter

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.

like image 320
markhorrocks Avatar asked Dec 30 '19 20:12

markhorrocks


People also ask

How do I add PDF assets in flutter?

Step 1: Create a new directory in the root directory of the project and name it as "assets”. Added the PDF document inside the new directory as shown in the following image. Step 2: Add the PDF document in the assets section of pubspec. yaml file.


1 Answers

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>',
));
like image 174
Dav Avatar answered Oct 17 '22 22:10

Dav