Print custom invoice via Bluetooth and usb printers on android
I am developing android app and i want to print custom invoice this my code
public class PrintBillActivity extends AppCompatActivity {
Display mDisplay;
String imagesUri;
String path;
Bitmap bitmap;
int totalHeight;
int totalWidth;
public static final int READ_PHONE = 110;
String file_name = "Screenshot";
File myPath;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_print_bill);
RecyclerView l=findViewById(R.id.lv);
Button btn = findViewById(R.id.print_btn);
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
mDisplay = wm.getDefaultDisplay();
LinearLayoutManager lm = new LinearLayoutManager(PrintBillActivity.this);
lm.setOrientation(RecyclerView.VERTICAL);
l.setLayoutManager(lm);
String s=getIntent().getExtras().getString("data");
Type type = new TypeToken<List<selected_service_item>>() {
}.getType();
ArrayList<selected_service_item> data=new Gson().fromJson(s,type);
selected_services_adapter adapter=new selected_services_adapter(data);
l.setAdapter(adapter);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (ContextCompat.checkSelfPermission(PrintBillActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions(PrintBillActivity.this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
else{
btn.setVisibility(View.GONE);
takeScreenShot();
btn.setVisibility(View.VISIBLE);
}
}
});
}
public Bitmap getBitmapFromView(View view, int totalHeight, int totalWidth){
Bitmap returnedBitmap = Bitmap.createBitmap(totalWidth, totalHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
Drawable bgDrawable = view.getBackground();
if(bgDrawable != null){
bgDrawable.draw(canvas);
}else{
canvas.drawColor(Color.WHITE);
}
view.draw(canvas);
return returnedBitmap;
}
private void takeScreenShot(){
File folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/ScreenShot/");
if(!folder.exists()){
boolean success = folder.mkdir();
}
path = folder.getAbsolutePath();
path = path + "/" + file_name + System.currentTimeMillis() + ".pdf";
View u = findViewById(R.id.ticket);
NestedScrollView z = findViewById(R.id.ticket);
totalHeight = z.getChildAt(0).getHeight();
totalWidth = z.getChildAt(0).getWidth();
String extr = Environment.getExternalStorageDirectory() + "/Flight Ticket/";
File file = new File(extr);
if(!file.exists())
file.mkdir();
String fileName = file_name + ".jpg";
myPath = new File(extr, fileName);
imagesUri = myPath.getPath();
bitmap = getBitmapFromView(u, totalHeight, totalWidth);
try{
FileOutputStream fos = new FileOutputStream(myPath);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
}catch (Exception e){
e.printStackTrace();
}
createPdf();
}
private void createPdf() {
PdfDocument document = new PdfDocument();
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(bitmap.getWidth(), bitmap.getHeight(), 1).create();
PdfDocument.Page page = document.startPage(pageInfo);
Canvas canvas = page.getCanvas();
Paint paint = new Paint();
paint.setColor(Color.parseColor("#ffffff"));
canvas.drawPaint(paint);
Bitmap bitmap = Bitmap.createScaledBitmap(this.bitmap, this.bitmap.getWidth(), this.bitmap.getHeight(), true);
paint.setColor(Color.BLUE);
canvas.drawBitmap(bitmap, 0, 0, null);
document.finishPage(page);
File filePath = new File(path);
try{
if(!filePath.exists()){
boolean success = filePath.createNewFile();
}
document.writeTo(new FileOutputStream(filePath));
}catch (IOException e){
e.printStackTrace();
Toast.makeText(this, "Something Wrong: "+e.toString(), Toast.LENGTH_SHORT).show();
}
document.close();
if (myPath.exists())
myPath.delete();
//openPdf(path);
printBIll();
}
private void printBIll(){
PrintManager printManager= (PrintManager) getSystemService(Context.PRINT_SERVICE);
try
{
PrintDocumentAdapter printAdapter = new PdfDocumentAdapter(this,path );
printManager.print("Document", printAdapter,new PrintAttributes.Builder().build());
}
catch (Exception e)
{
Log.d("pdfer",e.getMessage());
}
}
@Override
public void onBackPressed() {
startActivity(new Intent(this,MainActivity.class));
finish();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (grantResults.length>0 &&grantResults[0]==PackageManager.PERMISSION_GRANTED)
takeScreenShot();
else
Toast.makeText(this, "يرجى منح الصلاحيات للطباعة", Toast.LENGTH_SHORT).show();
}
activity_print_bill.xml
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="فاتورة"
android:textStyle="bold"
android:textSize="22sp"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="26dp"
android:textSize="14sp"
android:text="رقم الفاتورة: 37913 "/>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="إسم المتجر"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#E4222121" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="عنوان المتجر"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#E4222121" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="تاريخ: 12/12/2021"
android:layout_gravity="right"/>
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="الرقم الضريبي: 123456789002"
android:layout_gravity="right" />
<TextView
android:id="@+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="الخدمات :"
android:textSize="16sp"
android:textColor="#E4222121"
android:textStyle="bold"
android:layout_gravity="right"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp" />
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_gravity="right"
android:text="المجموع شامل ضريبة القيمة المضافة (ريال) 220"
/>
<TextView
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#E4222121"
android:text="ضريبة القيمة المضافة(15%) 32 "
android:layout_gravity="right" />
<TextView
android:id="@+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="شكرا لك لاستخدام خدمتنا "
android:layout_marginTop="36dp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/darker_gray"
android:layout_marginTop="18dp"/>
<Button
android:id="@+id/print_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="@color/black"
android:text="طباعة"/>
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.core.widget.NestedScrollView>
The Code simply convert invoice layout to pdf and than print the pdf ,but when I try it on usb printer doesn't shom me the printer .so how i can print custom layout via usb printers or is there anywhy to print custom invoice via any printer? note:I see most of old question and they do not doing what i want
from Recent Questions - Stack Overflow https://ift.tt/3kqLTDe
https://ift.tt/eA8V8J
Comments
Post a Comment