2Answers
  • 8
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

Printing a receipt to a file

I can print a receipt (byte[] printer format) via a thermal printer, like this :

// Print a receipt
OutputStream outputStream = mPrinterSocket.getOutputStream();
for (byte[] packet : content)
    outputStream.write(packet);
outputStream.flush();

But I can not manage to save a receipt (byte[] printer format) into a file (pdf or image). I tried :

// Save a receipt
try (OutputStream outputStream = new FileOutputStream(filePath)) {
    for (byte[] packet : content) {
        outputStream.write(packet);
        outputStream.flush();
    }
} catch (Exception exception) {
    exception.printStackTrace();
}

But it returned a corrupted file, containing:

???š????????????‹???†††††???????????†††††††††††?E???†††††††††††???E????††††††††††???????????????????????†††††††††????????????????????V

Is the process to "print a byte[] to file" different from "save a byte[] to file"? For example, I use the character {0x1b, 0x45, 0x01} to put a text in bold, is this a problem? I would like to avoid reformatting everything or using an external library.

      • 1
    • I'd wrap the FileOutputStream into a BufferedOutputStream, instead of using a plain FOS. Also, you don't need to flush() after each packet, just outside of the loop - as in your first example.
      • 1
    • here what is "mPrinterSocket" .For some 3rd party libraries (I think like ur printer library) they are accepting data in normal format and process into their properietary format to print .So please given more details what you are tyring in the mPrinterSocket with the printer and how to you save the file ,etc

I'd wrap the FileOutputStream into a BufferedOutputStream, instead of using a plain FOS. When reading from or writing to a file it is always a good practice to use a buffer.

Also, you don't need to flush() after each packet, just outside of the loop - as in your first example. This also improves performance.

  • 1
Reply Report