Open pufaqi opened 1 year ago
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.
I don't know if there is a solution
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
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