How to Print PDF using Android 4.4 Printing framework

24,219

After spend some hours on google i found the solution.

PrintManager printManager = (PrintManager) this.getSystemService(Context.PRINT_SERVICE);
String jobName = this.getString(R.string.app_name) + " Document";
printManager.print(jobName, pda, null);

PrintDocumentAdapter pda = new PrintDocumentAdapter(){

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

        try {

            input = new FileInputStream(file to print);
            output = new FileOutputStream(destination.getFileDescriptor());

            byte[] buf = new byte[1024];
            int bytesRead;

            while ((bytesRead = input.read(buf)) > 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 {
                input.close();
                output.close();
            } 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("Name of file").setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT).build();

        callback.onLayoutFinished(pdi, true);
    }
};
Share:
24,219
Dinesh T A
Author by

Dinesh T A

Android app developer @Zoho Corporation

Updated on July 09, 2022

Comments

  • Dinesh T A
    Dinesh T A almost 2 years

    How to print already downloaded PDF using Android 4.4 printing framework?

    I viewed the developer documentation. but no luck. Any example would be helpful

  • abhishek
    abhishek over 9 years
    What kind of printers are supported ??
  • Dinesh T A
    Dinesh T A over 9 years
  • Access Denied
    Access Denied over 9 years
    Thanks man, you saved me tons of time :)
  • dd619
    dd619 about 9 years
    very useful..just remove int pages = computePageCount(newAttributes); from code.
  • Nandakishore Shetty
    Nandakishore Shetty over 8 years
    @Dinesh Anandan, do you any idea about USB printer using android.stackoverflow.com/questions/30498746/…
  • Mohsin
    Mohsin almost 8 years
    Thanks, perfect solution
  • Smit Davda
    Smit Davda about 7 years
    Thanks for the perfect answer, do you have any idea of how to print an image from an image url or a local device image?
  • Amit
    Amit about 7 years
    @Dinesh Do you have any Idea how to print any office document like xls, doc, docx through this?
  • Ravish Sharma
    Ravish Sharma almost 7 years
    hi guys what is pda in printmanager.print(jobname,pda,null) please help me
  • Dinesh T A
    Dinesh T A almost 7 years
    @RavishSharma It's PrintDocumentAdapter use the code like printmanager.print(jobname, new PrintDocumentAdapter(){ } ,null)
  • Pink Jazz
    Pink Jazz about 6 years
    This does not respect the orientation of PDFs, with the preview of landscape PDFs being rotated sideways.
  • Fisher
    Fisher over 5 years
    This works fine for a .pdf file, but for a .txt file it doesn't show the layout and no way to print. Do you know how to modify to print a .txt file? Thanks!
  • Girish
    Girish over 4 years
    Still, File saving as 0 bytes.
  • SinaMN75
    SinaMN75 over 4 years
    it will open android's dialog for choosing printers and other settings, is there anyway to prevent that, make it print directly without any popups?
  • Saad Bilal
    Saad Bilal over 3 years
    Awesome man !!!!
  • Venkat
    Venkat almost 3 years
    Hi...in this code - input = new FileInputStream(file to print); - what I need to print in the place of file to print... Can anyone plz help me..