Example usage for android.webkit WebView createPrintDocumentAdapter

List of usage examples for android.webkit WebView createPrintDocumentAdapter

Introduction

In this page you can find the example usage for android.webkit WebView createPrintDocumentAdapter.

Prototype

public PrintDocumentAdapter createPrintDocumentAdapter(String documentName) 

Source Link

Document

Creates a PrintDocumentAdapter that provides the content of this WebView for printing.

Usage

From source file:net.gsantner.opoc.util.ShareUtil.java

/**
 * Print a {@link WebView}'s contents, also allows to create a PDF
 *
 * @param webview WebView/*  w  w  w. j  a va  2 s  .  c  o  m*/
 * @param jobName Name of the job (affects PDF name too)
 * @return {{@link PrintJob}} or null
 */
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@SuppressWarnings("deprecation")
public PrintJob print(WebView webview, String jobName) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        PrintDocumentAdapter printAdapter;
        PrintManager printManager = (PrintManager) webview.getContext().getSystemService(Context.PRINT_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            printAdapter = webview.createPrintDocumentAdapter(jobName);
        } else {
            printAdapter = webview.createPrintDocumentAdapter();
        }
        if (printManager != null) {
            return printManager.print(jobName, printAdapter, new PrintAttributes.Builder().build());
        }
    } else {
        Log.e(getClass().getName(), "ERROR: Method called on too low Android API version");
    }
    return null;
}