Is there a way to bypass printer selection in Android Java?

61 Views Asked by At

As part of a project, I am currently developing an Android Java application for a point of sale, in which I must be able, at the click of a button, to print a document.

To print this document (which I obtain in base 64), I transform it into pdf before printing it.

When printing, I must systematically select the printer to use via the Android interface.

My question is, knowing the IP address and port of the printer to use, is there a way to start printing directly, without having to first select the printer?

And if so, is there also a way to print to a Bluetooth or USB printer without selecting it first?

Here my code : In my main class

public void printPdfNormalPrinter (String base64 ){
        PDFPrinter.printPDFFromBase64(this.cordova.getContext(), base64);
    }

And in PDFPrinter.java


import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.CancellationSignal;
import android.os.Environment;
import android.os.ParcelFileDescriptor;
import android.print.PageRange;
import android.print.PrintAttributes;
import android.print.PrintDocumentAdapter;
import android.print.PrintDocumentInfo;
import android.print.PrintManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Base64;

import android.util.Base64InputStream;
public class PDFPrinter {

    public static void printPDFFromBase64(Context context, String base64PDF) {
        byte[] decodedBytes = android.util.Base64.decode(base64PDF, 0);

        File pdfFile = createTempPDFFile(context, decodedBytes);

        if (pdfFile != null) {
            printPDF(context, pdfFile);
        }
    }

    private static File createTempPDFFile(Context context, byte[] pdfBytes) {
        File pdfFile = null;
        FileOutputStream fos = null;
        try {
            File storageDir = context.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS);
            pdfFile = File.createTempFile("temp", ".pdf", storageDir);

            fos = new FileOutputStream(pdfFile);
            fos.write(pdfBytes);
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return pdfFile;
    }

    private static void printPDF(Context context, File pdfFile) {
        PrintManager printManager = (PrintManager) context.getSystemService(Context.PRINT_SERVICE);

        String jobName = context.getString(R.string.app_name) + " Document";

        PrintAttributes attributes = new PrintAttributes.Builder()
                .setMediaSize(PrintAttributes.MediaSize.ISO_A4)
                .setMinMargins(PrintAttributes.Margins.NO_MARGINS)
                .build();

        printManager.print(jobName, new PDFPrintDocumentAdapter(context, pdfFile.getAbsolutePath()), attributes);
    }

    private static class PDFPrintDocumentAdapter extends PrintDocumentAdapter {
        private Context context;
        private String filePath;

        public PDFPrintDocumentAdapter(Context context, String filePath) {
            this.context = context;
            this.filePath = filePath;
        }

        @Override
        public void onWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback callback) {
            try {
                FileInputStream fis = new FileInputStream(filePath);
                FileOutputStream fos = new FileOutputStream(destination.getFileDescriptor());

                byte[] buffer = new byte[1024];
                int length;
                while ((length = fis.read(buffer)) > 0) {
                    fos.write(buffer, 0, length);
                }

                fis.close();
                fos.close();

                callback.onWriteFinished(new PageRange[]{PageRange.ALL_PAGES});
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras) {
            if (cancellationSignal.isCanceled()) {
                callback.onLayoutCancelled();
                return;
            }

            PrintDocumentInfo pdi = new PrintDocumentInfo.Builder("pdf_document.pdf")
                    .setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT)
                    .build();

            callback.onLayoutFinished(pdi, true);
        }
    }
}
0

There are 0 best solutions below