question

spmolina avatar image
spmolina asked signup commented

Printing wth ImagePrintJob and TextPrintJob

I am trying to print a customized receipt with the ImagePrintJob and TextPrintJob, but am running into the following problem.

I cannot style anything on it when printing. As it all ends up left-aligned and left-justified.

What I am trying to do is center an image on the receipt paper to act as a logo. Then I am also attempting to place item names on the left side, with the price on the right.

Is there anything I can look at to help me do this?

Thanks. Much appreciated.

Print
10 |2000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

Jeffrey Blattman avatar image
Jeffrey Blattman answered signup commented

TextPrintJob is limited and you won't be able to style the text. If you want something more complex you can use ImagePrintJob. The image should be scaled to fill the the width of the printer. It's then up to you to render the image with the proper text and alignments, etc. You can also look into ViewPrintJob which allows you to render the receipt using an Android View hierarchy.

For example,

TextView tv = new Textview(context);
tv.setText("Hello, printer.");
new ViewPrintJob.Builder().view(tv).build().print(...);
5 comments
10 |2000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

I like the idea of using ViewPrintJob, but you wouldn't happen to have any examples I could look at? Like how TextPrintJob and ImagePrintJob have the PrintTestActivity on GitHub.

I created a view to print, but just having difficulties sending it to the printer. I might just be missing a basic step, but any help here would be greatly appreciated. Thanks.

0 Likes 0 ·

No we don't have samples for that. What problems are you having?

0 Likes 0 ·

Well the sample I created was made in an XML file so I could easily modify it later and I can't seem to get it to save to a View variable so I can print it. My variable ends up "null" when I call findViewById(R.layout.receiptsample) or call findViewById(R.id.entirereceipt).

Here is the code where I am trying to call and save it if that helps.

View mReceipt = (View) findViewById(R.id.entire_receipt);

0 Likes 0 ·

It's really hard to say without seeing more of your source. I'd suggest testing with something like I put in the answer to see if the printing mechanism is working then worry about modularizing it later.

0 Likes 0 ·

Hi SPMolina ,Have you go the solution ..i have also making custom receipt but not able to align left or right or centre , please let me know if you have solution

0 Likes 0 ·

Welcome to the
Clover Developer Community