4

I'm looking for an easy way of making this printing HTML code fragment compatible backwards with older Android versions:

@TargetApi(Build.VERSION_CODES.KITKAT)
private void createWebPrintJob(WebView webView) {

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

    // Save the job object for later status checking
    mPrintJob = printJob;
}

This works from Android 19 (Kit-Kat), but i need some previous versions to work also.

I don't need to download and print a webpage from the internet, i already have the HTML code as a String, which i show in a webview (as yo can see in the code).

Is there an easy way of printing HTML code before Android 19?

Thanks in advance!

Hugo
  • 1,662
  • 18
  • 35
  • 1
    AFAIK no, unless you develop an app for a custom `Android`-based device where you could print over `Linux` after installing proper printer drivers... – Onik Mar 04 '16 at 22:06

1 Answers1

2

Prior to Kitkat there is no Android API to support printing. In the 2016 Google I/O printing training, the developers mentioned that prior to Kitkat your only way to print is to "throw it over the wall". That is, send an intent and hope that the user has the suitable application to print. Look at this question for examples on apps that provide printing (HP ePrint and Google Cloud Print).

Community
  • 1
  • 1
Nonos
  • 2,450
  • 2
  • 23
  • 34