Java Code Examples for android.webkit.WebView#createPrintDocumentAdapter()

The following examples show how to use android.webkit.WebView#createPrintDocumentAdapter() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ShareUtil.java    From Stringlate with MIT License 7 votes vote down vote up
/**
 * Print a {@link WebView}'s contents, also allows to create a PDF
 *
 * @param webview WebView
 * @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;
}
 
Example 2
Source File: PreviewFragment.java    From Resume-Builder with MIT License 5 votes vote down vote up
private void createWebPrintJob(WebView webView) {

        // Get a PrintManager instance
        PrintManager printManager = (PrintManager) getActivity()
                .getSystemService(Context.PRINT_SERVICE);

        // Get a print adapter instance
        PrintDocumentAdapter printAdapter = webView.createPrintDocumentAdapter();

        // Create a print job with name and adapter instance
        String jobName = getString(R.string.app_name) + " Document";
        PrintJob printJob = printManager.print(jobName, printAdapter,
                new PrintAttributes.Builder().build());
    }
 
Example 3
Source File: MainActivity.java    From Android-SmartWebView with MIT License 5 votes vote down vote up
private void print_page(WebView view, String print_name, boolean manual) {
	PrintManager printManager = (PrintManager) getSystemService(Context.PRINT_SERVICE);
	PrintDocumentAdapter printAdapter = view.createPrintDocumentAdapter(print_name);
	PrintAttributes.Builder builder = new PrintAttributes.Builder();
	builder.setMediaSize(PrintAttributes.MediaSize.ISO_A5);
	PrintJob printJob = printManager.print(print_name, printAdapter, builder.build());

	if(printJob.isCompleted()){
		Toast.makeText(getApplicationContext(), R.string.print_complete, Toast.LENGTH_LONG).show();
	}
	else if(printJob.isFailed()){
		Toast.makeText(getApplicationContext(), R.string.print_failed, Toast.LENGTH_LONG).show();
	}
}
 
Example 4
Source File: ShareUtil.java    From memetastic with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Print a {@link WebView}'s contents, also allows to create a PDF
 *
 * @param webview WebView
 * @param jobName Name of the job (affects PDF name too)
 * @return {{@link PrintJob}} or null
 */
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public PrintJob print(final WebView webview, final String jobName, final boolean... landscape) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        final PrintDocumentAdapter printAdapter;
        final PrintManager printManager = (PrintManager) _context.getSystemService(Context.PRINT_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            printAdapter = webview.createPrintDocumentAdapter(jobName);
        } else {
            printAdapter = webview.createPrintDocumentAdapter();
        }
        final PrintAttributes.Builder attrib = new PrintAttributes.Builder();
        if (landscape != null && landscape.length > 0 && landscape[0]) {
            attrib.setMediaSize(new PrintAttributes.MediaSize("ISO_A4", "android", 11690, 8270));
            attrib.setMinMargins(new PrintAttributes.Margins(0, 0, 0, 0));
        }
        if (printManager != null) {
            try {
                return printManager.print(jobName, printAdapter, attrib.build());
            } catch (Exception ignored) {
            }
        }
    } else {
        Log.e(getClass().getName(), "ERROR: Method called on too low Android API version");
    }
    return null;
}
 
Example 5
Source File: DetailFragment.java    From kolabnotes-android with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void createWebPrintJob(WebView webView, String jobName) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        // Get a PrintManager instance
        PrintManager printManager = (PrintManager) getActivity()
                .getSystemService(Context.PRINT_SERVICE);

        // Get a print adapter instance
        PrintDocumentAdapter printAdapter = webView.createPrintDocumentAdapter();

        // Create a print job with name and adapter instance
        PrintJob printJob = printManager.print(jobName, printAdapter,
                new PrintAttributes.Builder().build());
    }
}
 
Example 6
Source File: TemplatePrinterActivity.java    From commcare-android with Apache License 2.0 5 votes vote down vote up
/**
 * Starts a print job for the given WebView
 *
 * Source: https://developer.android.com/training/printing/html-docs.html
 *
 * @param v the WebView to be printed
 */
@TargetApi(Build.VERSION_CODES.KITKAT)
private void createWebPrintJob(WebView v) {
    // Get a PrintManager instance
    PrintManager printManager = (PrintManager) getSystemService(Context.PRINT_SERVICE);

    // Get a print adapter instance
    PrintDocumentAdapter printAdapter = new PrintDocumentAdapterWrapper(this, v.createPrintDocumentAdapter());

    // Create a print job with name and adapter instance
    printJob = printManager.print(jobName, printAdapter, new PrintAttributes.Builder().build());
}
 
Example 7
Source File: MainActivity.java    From Notepad with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
@TargetApi(Build.VERSION_CODES.KITKAT)
private void createWebPrintJob(WebView webView) {
    // Get a PrintManager instance
    PrintManager printManager = (PrintManager) getSystemService(PRINT_SERVICE);

    // Get a print adapter instance
    PrintDocumentAdapter printAdapter = webView.createPrintDocumentAdapter();

    // Create a print job with name and adapter instance
    String jobName = getString(R.string.document, getString(R.string.app_name));
    printManager.print(jobName, printAdapter,
            new PrintAttributes.Builder().build());
}
 
Example 8
Source File: ShareUtil.java    From openlauncher with Apache License 2.0 4 votes vote down vote up
/**
 * Print a {@link WebView}'s contents, also allows to create a PDF
 *
 * @param webview WebView
 * @param jobName Name of the job (affects PDF name too)
 * @return {{@link PrintJob}} or null
 */
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public PrintJob print(final WebView webview, final String jobName, final boolean... landscape) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        final PrintDocumentAdapter printAdapter;
        final PrintManager printManager = (PrintManager) _context.getSystemService(Context.PRINT_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            printAdapter = webview.createPrintDocumentAdapter(jobName);
        } else {
            printAdapter = webview.createPrintDocumentAdapter();
        }
        final PrintAttributes.Builder attrib = new PrintAttributes.Builder();
        if (landscape != null && landscape.length > 0 && landscape[0]) {
            attrib.setMediaSize(new PrintAttributes.MediaSize("ISO_A4", "android", 11690, 8270));
            attrib.setMinMargins(new PrintAttributes.Margins(0, 0, 0, 0));
        }
        if (printManager != null) {
            try {
                return printManager.print(jobName, printAdapter, attrib.build());
            } catch (Exception ignored) {
            }
        }
    } else {
        Log.e(getClass().getName(), "ERROR: Method called on too low Android API version");
    }
    return null;
}