How to create bill for POS printer size in android

2019-05-16 11:10发布

问题:

I have created the pdf for bill using iText. I want to print this bill in POS printer. POS printer Paper size will be 58mm. My PDF is look like A4 size. How to re size this page for POS printer size?

And, If products goes more than 100 mean, I have to use new page. So there will be two pages. For this case how can I print the bill in single paper.

Or else is there any other way to create bill and print using android mobile or tablet?

回答1:

When you create a document like this:

Document document = new Document();

A document is created of which all the pages have the default size. The default size is A4.

There is a class named PageSize that contains many different standard sizes. For instance, if you want to create a page with the American LETTER size, you can create the Document like this:

Document document = new Document(PageSize.LETTER);

You are confronted with two problems:

  1. Your bills do not have a standard size.
  2. You don't know the size of your bill in advance.

Solving problem 1 is easy: the Document class accepts a Rectangle parameter. You can create your own page size like this:

Rectangle pagesize = new Rectangle(288, 720);
Document document = new Document(pagesize);

In this case, you'll have pages that measure 4 by 10 inch:

288 user units = 288 pt = 4 x 72pt = 4 inch
720 user units = 720 pt = 10 x 72pt = 10 inch

You can solve your first problem by converting 58 mm into pt: 164.409448819 pt

Solving the second problem is more difficult. One way to do it, would be by creating a document that is extremely long. The maximum size of a PDF is 14,400 by 14,400 user units, so you could create your rectangle like this:

 Rectangle pagesize = new Rectangle(164.41f, 14400);

It would be really surprising if you had a bill that was longer than 5.08 meter. If that PDF is too long for your printer, you could store the y position at the end of the content and then reduce the page size in a second go.

Another approach would be to add all the content to a ColumnText object, then ask the column for its height and create a new ColumnText object for a newly created document with the height calculated before. How to do this? That's explained in my answer to this question: How to adjust the page height to the content height?