2

I need to support printing on KitKat devices but my target SDK is 13 (changing is not an option).

Specifically I need to print a webview.

This is the API for printing a webview: http://developer.android.com/training/printing/html-docs.html

laalto
  • 150,114
  • 66
  • 286
  • 303
Randy
  • 4,351
  • 2
  • 25
  • 46
  • Why do you need the target SDK to stay at 13? This will put the WebView in a compatibility mode which may have some unexpected results. – Matt Gaunt Jan 13 '14 at 16:01

2 Answers2

4

It's an old one but printing is kind of usefull so this could be good to work correctly. (Without reflection ;))

A better way to work with devices version. No try-catch needed, just need to add some messages before the return or you just hide the button/menu/... depending on the same condition.

@TargetApi(Build.VERSION_CODES.KITKAT)
    private void createWebPrintJob(WebView webView) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) 
            return;

        // Get a PrintManager instance
        PrintManager printManager = (PrintManager) 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";
        printManager.print(jobName, printAdapter,
                new PrintAttributes.Builder().build());

    }

The printJob will be execute only with SDK 19 and above

AxelH
  • 14,325
  • 2
  • 25
  • 55
  • `target SDK is 13` is the reason why I used reflection. Obviously, I didn't want to use reflection but I didn't have the option of changing the target. – Randy Feb 26 '15 at 15:30
  • 1
    The code work on every sdk, it will check the sdk and if it is prior Kitkat, just stop the print method. It mean that this support sdk 13 and it will provide the print functionnality for Kitkat as requested. (The @targetApi his to be able to build the apk with some not supported functionnalities) – AxelH Feb 26 '15 at 15:34
  • createPrintDocumentAdapter() is deprecated now, and using the non-deprecated version requires API 21 (lollipop)...Great move from Google – FabioR Jun 17 '20 at 16:26
  • @FabioR I believe this is when they introduced the printing service... in the settings of the device. Not sure, I didn't work on Android in years... I would guess this is using a different API. – AxelH Jun 23 '20 at 06:24
1

Here is my solution:

public void print(WebView webView) {
    //PrintManager
    String PRINT_SERVICE = (String) Context.class.getDeclaredField("PRINT_SERVICE").get(null);
    Object printManager = mActivity.getSystemService(PRINT_SERVICE);

    //PrintDocumentAdapter
    Class<?> printDocumentAdapterClass = Class.forName("android.print.PrintDocumentAdapter");
    Method createPrintDocumentAdapterMethod = webview.getClass().getMethod("createPrintDocumentAdapter");
    Object printAdapter = createPrintDocumentAdapterMethod.invoke(webview);

    //PrintAttributes
    Class<?> printAttributesBuilderClass = Class.forName("android.print.PrintAttributes$Builder");
    Constructor<?> ctor = printAttributesBuilderClass.getConstructor();
    Object printAttributes = ctor.newInstance(new Object[] {});
    Method buildMethod = printAttributes.getClass().getMethod("build");
    Object printAttributesBuild = buildMethod.invoke(printAttributes);

    //PrintJob
    String jobName = "My Document";
    Method printMethod = printManager.getClass().getMethod("print", String.class, printDocumentAdapterClass, printAttributesBuild.getClass());
    Object printJob = printMethod.invoke(printManager, jobName, printAdapter, printAttributesBuild);

    // Save the job object for later status checking
    mPrintJobs.add(printJob);
}

Just make sure this is called on the main thread. Also note: You need to use a try catch. Devices which are not running 4.4+ will crash if you don't.

Randy
  • 4,351
  • 2
  • 25
  • 46
  • 5
    Using a try catch for handling android versions is really not the best solution, one should rather check the Build version. http://developer.android.com/training/basics/supporting-devices/platforms.html – animaonline Jan 27 '14 at 13:36
  • @Randy can you please help me https://stackoverflow.com/questions/49124930/pdf-printing-view-issue – Sunisha Guptan Mar 10 '18 at 14:28