Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

printer_raw.dart example doesn't seem to work #728

Open
pufaqi opened this issue Jun 19, 2023 · 3 comments
Open

printer_raw.dart example doesn't seem to work #728

pufaqi opened this issue Jun 19, 2023 · 3 comments
Labels
bug Something isn't working as expected external help wanted Looking for help on this ticket from the community P3 Low-priority issue

Comments

@pufaqi
Copy link

pufaqi commented Jun 19, 2023

When I use printer_raw.dart, the printing task appears in the printer task list, but it disappears soon, and the duration may only be tens of milliseconds? The printer does not respond at all. Of course my printer can be used normally.
1,The printer is a USB interface
2,Flutter Version 3.10.0

@Ghelwig
Copy link

Ghelwig commented Jun 21, 2023

I am having a similar issue, the print job appears in the queue and prints, but upon completion the job is stuck in a pending state and no other jobs in the queue can print until it is deleted and restarted. Can be replicated by using the same example. this is a USB printer.

@pufaqi
Copy link
Author

pufaqi commented Jun 22, 2023

I don't know if there is a solution

@halildurmus halildurmus added bug Something isn't working as expected external help wanted Looking for help on this ticket from the community labels Jun 22, 2023
@Alex37882388
Copy link

Alex37882388 commented Mar 17, 2024

me too... Have any solution? My code is as blow:

import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:ffi';
import 'package:ffi/ffi.dart';
import 'package:win32/win32.dart';

void main() {

  // 获取缓冲区大小
  final bufferSizePointer = calloc<Uint32>();
  GetDefaultPrinter(nullptr, bufferSizePointer);

  // 分配足够大的缓冲区
  final bufferSize = bufferSizePointer.value;
  final buffer = calloc<Uint16>(bufferSize);

  // 获取默认打印机的名称
  if (GetDefaultPrinter(buffer as Pointer<Utf16>, bufferSizePointer) != 0) {
    // 将缓冲区转换为Dart字符串
    final defaultPrinterName = String.fromCharCodes(buffer.asTypedList(bufferSize -1));
    // HP51CDC0 (HP Ink Tank Wireless 410 series)
    print(defaultPrinterName);

    final docName = TEXT('Document');
    final dataType = TEXT('RAW');

    final hPrinter = calloc<IntPtr>();
    if (OpenPrinter(defaultPrinterName.toNativeUtf16(), hPrinter, nullptr) != 0) {
      final docInfo1 = calloc<DOC_INFO_1>()
        ..ref.pDocName = docName
        ..ref.pOutputFile = nullptr
        ..ref.pDatatype = dataType;
      final printJob = StartDocPrinter(hPrinter.value, 1, docInfo1);
      if (printJob > 0) {
        StartPagePrinter(hPrinter.value);
        // 准备要打印的文本
        final text = 'Hello World.........';
        final textBytes = utf8.encode(text);
        final pointer = calloc<Uint8>(textBytes.length);
        final nativeList = pointer.asTypedList(textBytes.length);
        nativeList.setAll(0, textBytes);
        final bytesWritten = calloc<Uint32>();

        // 将文本写入打印机
        int res = WritePrinter(hPrinter.value, pointer.cast<Uint8>(), textBytes.length, bytesWritten);

        // 在使用完毕后,释放分配的内存
        calloc.free(pointer);
        calloc.free(bytesWritten);


        EndPagePrinter(hPrinter.value);
        EndDocPrinter(hPrinter.value);
        print('Printed demo.png successfully.');

        calloc.free(hPrinter);
        calloc.free(docInfo1);

      } else {
        print('Failed to start print job.');
      }
      ClosePrinter(hPrinter.value);
    } else {
      print('Failed to open printer.');
    }
    calloc.free(docName);
    calloc.free(dataType);

  } else {
    print('Failed to get default printer.');
  }

  // 释放内存
  calloc.free(bufferSizePointer);
  calloc.free(buffer);
}

The return value of 'res' is 1,and I saw a task added to the printer task list, and then it disappeared, but no response from the printer.
I tried the Dell PC with HP ink printer, and Dell laptops with label printers, got the same problem.

int res = WritePrinter(hPrinter.value, pointer.cast<Uint8>(), textBytes.length, bytesWritten);

BWT, I installed visual studio 2022, the Dell laptops installed VS2019

@halildurmus halildurmus changed the title printer_raw.dart doesn't seem to work printer_raw.dart example doesn't seem to work Oct 19, 2024
@github-actions github-actions bot removed the examples label Nov 15, 2024
@halildurmus halildurmus added the P3 Low-priority issue label Jan 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working as expected external help wanted Looking for help on this ticket from the community P3 Low-priority issue
Projects
None yet
Development

No branches or pull requests

4 participants