Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing PDF directly using PrintManager Android 4.4

http://developer.android.com/training/printing/index.html documentation tells how to print a custom content by rendering it on a PDF canvas and sending thus created PDF document for printing. But has no information about if we already have a PDF document, how to send it for printing?

Does similar to bitmap printing, there is some method like printHelper.printPDF?

like image 287
Shilpi Avatar asked Nov 01 '13 13:11

Shilpi


People also ask

How do I enable Print to PDF on Android?

Tap the three vertical dots icon on the top-right. Tap Print. Tap Select printer. Tap Save as PDF.

What app do I need to Print PDF files?

To use Let's Print PDF, you need to install Let's Print Droid. As the integration with Let's Print PDF, Let's Print Droid becomes a powerful PDF printing app for Android. It can easily get the command for your printer to work directly without any cloud base system like the other apps.


2 Answers

Use the following code fragment in your onWrite() method should do it:

InputStream input = null;
OutputStream output = null;
try {
    input = new FileInputStream(new File("somefile.pdf"));
    output = new FileOutputStream(destination.getFileDescriptor());
    byte[] buf = new byte[1024];
    int bytesRead;
    while ((bytesRead = input.read(buf)) > 0) {
         output.write(buf, 0, bytesRead);
    }
} catch (Exception e) {

} finally {
    try {
        input.close();
        output.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
like image 81
user2779473 Avatar answered Sep 28 '22 14:09

user2779473


So here is what I did...

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public void clicked(View view){ //Button To start print
    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()
{

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    public void onWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback callback)
    {
        InputStream input = null;
        OutputStream output = null;
        try {
            input = new FileInputStream(new File("/storage/emulated/0/Download/file_name.pdf"));
            output = new FileOutputStream(destination.getFileDescriptor());
            byte[] buf = new byte[1024];
            int bytesRead;
            while ((bytesRead = input.read(buf)) > 0) {
                output.write(buf, 0, bytesRead);
            }
        }
        catch (Exception e) {

        } finally {
            try {
                input.close();
                output.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        callback.onWriteFinished(new PageRange[]{PageRange.ALL_PAGES});
    }

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

        //int pages = computePageCount(newAttributes);

        PrintDocumentInfo pdi = new PrintDocumentInfo.Builder("file_name.pdf").setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT).build();
        callback.onLayoutFinished(pdi, true);
    }
};
}

Android Manifest >>>

ADD Permissions

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

My phone doesn't support sd card but still, I had to write READ_EXTERNAL_STORAGE

like image 40
Aditya Gudla Avatar answered Sep 28 '22 15:09

Aditya Gudla