Skip to content

printBytes and printBitmap not works #6

@blue492

Description

@blue492

Hello, thanks for this pacakge

Iam tring to print pdf or image, pdf is php base64_encode, and image is 'data:image/png;base64,' . $base64_image
I tested my files efter converting in show_pdf and show_img, they look good.

I tested print samble text from my app, it works in the printer.

The problem is the printer print Chineese letters when I try to print base64, it look like that printBytes and printBitmap not works look into my functions below printBase64Pdf and printBase64Image

I have following device:
Swift 2 Pro
Pos Device
Model: I23M02

My packages:
another_imin_printer: ^0.6.0
pdfx: ^2.9.2
image: ^4.5.4

How to solve my problem?

Here is my flutter code:

import 'dart:convert';
import 'dart:typed_data';
import 'package:another_imin_printer/enums/print_size_imin.dart';
import 'package:another_imin_printer/enums/print_style_align.dart';
import 'package:another_imin_printer/imin_printer.dart';
import 'package:another_imin_printer/print_style.dart';
import 'package:get/get.dart';
import 'package:pdfx/pdfx.dart';
import 'package:image/image.dart' as img;
import 'package:flutter/material.dart';
import 'package:syncfusion_flutter_pdfviewer/pdfviewer.dart';

class IminPrinterHelper {
  final IminPrinter _printer = IminPrinter();
  static const int maxWidth = 384; // use 384 for 58 mm printers

  Future<void> initPrinter({PrintSizeImin size = PrintSizeImin.mm58}) => _printer.initPrinter(printSizeImin: size);

  show_pdf(String base64Pdf){
    final Uint8List pdfBytes = base64Decode(base64Pdf);

    Navigator.push(Get.context!, MaterialPageRoute(builder: (_) => Scaffold(
      appBar: AppBar(title: Text('Preview')),
      body: SfPdfViewer.memory(pdfBytes),
    )));
  }

  show_img(final monoBytes){
    final img.Image image = img.decodeImage(monoBytes)!;

// Convert to a format suitable for displaying in Flutter
    final imgBytes = Uint8List.fromList(img.encodePng(image));

    Navigator.push(Get.context!, MaterialPageRoute(builder: (_) => Scaffold(
      appBar: AppBar(title: Text('Preview')),
      body: Image.memory(imgBytes),
    )));
  }

  Future<void> printSampleText() async {
    await initPrinter();

    await _printer.printText(
      'hello',
      printStyle: const PrintStyle(
        textAlign: PrintStyleAlign.center,
      ),
    );

    await _printer.print2ColumnsText(['Left side', 'Right side']);

    await _printer.fullCut();
  }


  Future<void> printBase64Pdf(String base64Pdf) async {
    final Uint8List pdfBytes = base64Decode('data:application/pdf;base64,' + base64Pdf);
    final doc = await PdfDocument.openData(pdfBytes);
    final page = await doc.getPage(1);
    final imgPage = await page.render(
      width: page.width, height: page.height,
      format: PdfPageImageFormat.png,
    );
    await page.close();
    await doc.close();

    final png = imgPage!.bytes;
    if (png == null || png.isEmpty) {
      print('❌ Render failed');
      return;
    }

    final original = img.decodeImage(png)!;
    final resized = original.width > maxWidth
        ? img.copyResize(original, width: maxWidth)
        : original;
    final mono = img.luminanceThreshold(resized, threshold: 0.5);
    final monoBytes = Uint8List.fromList(img.encodePng(mono));
    //show_img(monoBytes);

    await _printer.printBytes(monoBytes);
    await _printer.partialCut();
  }

  Future<void> printBase64Image(String base64Image) async {
    final base64String = base64Image.split(',').last;          // Remove 'data:image/png;base64,'
    final Uint8List imageBytes = base64Decode(base64String);

    final IminPrinter _printer = IminPrinter();

    await _printer.initPrinter(printSizeImin: PrintSizeImin.mm58);
    await _printer.printBitmap(imageBytes);                // Preferred for image printing
    await _printer.fullCut();

  }

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions