question

Danielle Cox avatar image
Danielle Cox asked Raymond Lee Deactivated edited

ViewPrintJob centering on receipt

I am creating a ViewPrintJob and sending it to the printer. This is working well for the Flex receipt and the mini receipt. However, the Station is printing it pushed to the right. The code building the view had to be done (per error messages I was receiving) on the UI thread which leads me to believe that this is due to the width being created with the display and I am not sure. Can I get the characteristics of the printer and set my sizing based on reality instead of what I am doing now? I have the view's xml set to match_parent.

Also, printing the receipt takes a couple of seconds to start. Is there a way to speed that up?

LayoutInflater inflater = LayoutInflater.from(CountActivity.this);
final ViewGroup receiptView = (ViewGroup)inflater.inflate(R.layout.receipt_view, null);

receiptView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
receiptView.layout(0, 0, receiptView.getMeasuredWidth(), receiptView.getMeasuredHeight());

new AsyncTask<Void, Void, String>() {
    @Override
    protected String doInBackground(Void... voids) {
        try {
            //View view = findViewById(R.id.rightFrame);
            Printer p = getPrinter();
            PrintJob printJob = new ViewPrintJob.Builder().view(receiptView).build();
            return new PrintJobsConnector(CountActivity.this).print(p, printJob);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String id) {
        // I need to figure out what if anything goes here
        if (id == null) {
            return;
        }
        //textId.setText(id);
        //monitorState();
    }
}.execute();
PrintMerchant
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

Raymond Lee avatar image
Raymond Lee Deactivated answered Raymond Lee Deactivated edited
How ViewPrintJob works, is that it converts the View you pass in into a bitmap, then prints the bitmap. Receipts are limited to 576 pixels in width for Clover Station (see here), so it will also scale your View until it is 576 pixels in width if on a Clover Station. Clover Mobile, Mini, and Flex are limited to 384 pixels.

So, yes you can set the width of your View to 576 pixels for Clover Station, and 384 pixels for the other Clover devices, to customize it for receipt printing. Here is the documentation to check which Clover device is running your app.

Printing of the receipt should be fairly quick, we do not set any delay for receipt printing. The delay could be from network latency if it is a remote printer. Or it could be processing time if you are using ViewPrintJob, and it needs to scale down your View.
10 |2000

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

Welcome to the
Clover Developer Community