Jose Francis Jose Francis - 1 month ago 11
Android Question

How to print PDF, image and HTML documents from android device to a printer on wifi?

I need to implement document printing functionality in my android app.
I was able to print image file using the android printing framework code mentioned below :

private void doPhotoPrint(Bitmap bigbitmap)
PrintHelper photoPrinter = new PrintHelper(MainActivity.this);
photoPrinter.printBitmap("droids.jpg - test print", bigbitmap);

I went through the existing threads discussing the same question, but none of them help much.

The issue I am facing is with other document types like PDF and HLML.
It would be helpful if someone could give some insight on the same.


you can check this code

if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        PrintManager printManager = (PrintManager) this.getSystemService(Context.PRINT_SERVICE);
        String jobName = this.getString(R.string.app_name) + " Document";

        PrintDocumentAdapter pda = new PrintDocumentAdapter() {

            public void onWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback callback) {
                InputStream input = null;
                OutputStream output = null;

                try {
                    AssetManager assetManager = getAssets();
                    File file = new File(getFilesDir(), "fact_sheet.pdf");                 
                    input ="fact_sheet.pdf");                        
                    output = new FileOutputStream(destination.getFileDescriptor());
                    byte[] buf = new byte[1024];
                    int bytesRead;

                    while ((bytesRead = > 0) {
                        output.write(buf, 0, bytesRead);

                    callback.onWriteFinished(new PageRange[]{PageRange.ALL_PAGES});

                } catch (FileNotFoundException ee) {
                    //Catch exception
                } catch (Exception e) {
                    //Catch exception
                } finally {
                    try {
                    } catch (IOException e) {

            public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras) {

                if (cancellationSignal.isCanceled()) {

                // int pages = computePageCount(newAttributes);

                PrintDocumentInfo pdi = new PrintDocumentInfo.Builder("Name of file").setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT).build();

                callback.onLayoutFinished(pdi, true);
      printManager.print(jobName, pda, null);