question

tacs avatar image
tacs asked Bryanne Vega edited

Help with Printjob

Hello.
Thing No 1: can i get some help with ViewPrintJob.Builder ?
I've created the linear layout containing a bunch of textviews and few linear layouts, sent the main container view it to the ViewPrintJob.Builder, but it doesnt print anything...
I cant find any tiny snippet of this method on the docs.
Can someone give an example of this?

Thing No 2: i was thinking if its possible to get the bitmap of a webview and send it to the printer or maybe get the bitmap of an image and send it to the printer, using ImagePrintJob for both cases... So is it possible?
Thanks.
Cheers.

Print
4 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.

Jeffrey Blattman avatar image Jeffrey Blattman ♦♦ commented ·

First thing is to, using your own code, render your layout as a Bitmap. Encode the Bitmap as a PNG, and ensure it looks as you expect. This is going to tell if you the problem is w/ your layout, or with the printer.

0 Likes 0 ·
tacs avatar image tacs commented ·

HI @jeff !
I created a dialog with an imageview and i set the bitmap on that imageview and its the bitmap i want...
Right after the dialog.show, i send the bitmap to the "ImagePrintjob.Builder().bitmap(bitmapy).build()" and i get a null exception.. I have a TextPrintjob before and works...
By the way, when using TextPrintJob is there anyway to know the when the line ends or its width or char limits? How am i suppose to know if i add line and its bigger than the actual line?
Cheers.

0 Likes 0 ·
Jeffrey Blattman avatar image Jeffrey Blattman ♦♦ commented ·

Show your code, and the exception, and the logcat around the exception.

0 Likes 0 ·
Jeffrey Blattman avatar image Jeffrey Blattman ♦♦ commented ·

re: TextPrintJob. No, it's very limited. You'd just need to experiment to find something that looks okay for your pruposes.

0 Likes 0 ·
tacs avatar image
tacs answered tacs commented
@Jeffrey Blattman hoy again!
So i managed to print a bitmap using the ImagePrintJob.Builder().bitmap() but its messy to handle the resolution and dimensions and position because im trying to take a screenshot of a webview. I also managed to get the ImagePrintJob.Builder().urlString() to work, but it takes too long to print a receipt with 3 lines (over 30 seconds), i tested with many different PNG files and different dimensions, but its still slow... Is this usual when using the bitmap or... ?
One more thing, ive tried to print using ViewPrintJob, because it seems i can easily set the design with a relative layout view, but when i try to set a view, i get a notification on the clover device saying there was a printing error... Can you just give me one small example of how to use the ViewPrintJob?

Thanks very much.
Cheers.
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.

Bryanne Vega avatar image Bryanne Vega commented ·

Could you help me with an example on how you fixed your issue of printing the bitmap of a view & i'll definetly answer back with the slow print issue.

0 Likes 0 ·
tacs avatar image tacs Bryanne Vega commented ·

If you just asked i would tell you, dont get me wrong but that just a childish message...

I was trying to get the bitmap of a webview but it was failing because the view was bigger than the screen, now it works.

view.setDrawingCacheEnabled(true);
view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());

view.buildDrawingCache(true);
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);return bitmap;
0 Likes 0 ·
Bryanne Vega avatar image Bryanne Vega tacs commented ·

Excuse the ignorance (childish question), do you always get fast prints from the views? What's your dimensions?

In my case i had to save the image using

File imageFile = new File("/storage/sdcard", "clover" + File.separator + "image-print");FileOutputStream out = new FileOutputStream(imageFile);bitmapImage.compress(Bitmap.CompressFormat.PNG, 50, out);out.close();

In to get my view with the:

new ImagePrintJob.Builder().bitmap(b).maxWidth().build().print(getContext(), account);

Which is weird because it won't print without making sure the file exists prior. (Discussed in an older question).

0 Likes 0 ·
Show more comments
Show more comments
tacs avatar image
tacs answered
@Jeffrey Blattman hoy!
By the way, very nice update on the forum here, seems much cleaner and smoother :)
So i cant access the log because i dont have a dev device, im just sideloading new versions to the clover account, then update my app and then test the receipts printed.
So heres my code:
final LayoutInflater factory = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View view = factory.inflate(R.layout.receipt, null);
final AlertDialog.Builder alertadd = new AlertDialog.Builder(context);
alertadd.setView(view);
alertadd.setNeutralButton("Here!", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        Bitmap bitmapy = Receipt.getScreenViewBitmap(view);

        AlertDialog.Builder alertadd = new AlertDialog.Builder(context);
        View view2 = factory.inflate(R.layout.ss, null);
        ((ImageView) view2.findViewById(R.id.imagy)).setImageBitmap(bitmapy);
        alertadd.setView(view2);
        alertadd.setNeutralButton("Here 2!", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        alertadd.show();

        dialog.dismiss();
    }
});
alertadd.show();

So i tested this code on a button click and it works, but it doesnt work when i try to call this from the onHandleIntent overridden method, from my service class which is extending IntentService... I thought it might be the context, so i created a toast message and the toast msg appears...
This is a code im trying to use to get the bitmap of a view, so i can send it to the printer...

Cheers.

(BTW: i was trying to add this as a reply but it was showing i exceeded 600 chars but i kept deleting and it still was 600 chars...)
10 |2000

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

Bryanne Vega avatar image
Bryanne Vega answered Bryanne Vega edited
To Resolve the issue with your ViewPrintJob, simple utilize your own code provided earlier:

(Extra: I did it on an background tasks to feel cool.) lol

final LayoutInflater factory = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

final View v = factory.inflate(R.layout.receipt, null);v.setDrawingCacheEnabled(true);

v.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));voucher.layout(0, 0, voucher.getMeasuredWidth(), voucher.getMeasuredHeight());

v.buildDrawingCache(true); 

new ViewPrintJob.Builder().view(v).build().print(getContext(),getAccount());
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