Kotlin Print Webview View (KPWV)


KPWV:   SOLD       




Hi Dev, recently I was given a challenge by a client to solve their business problems, actually they had a lot of problems, but only one made me feel challenged, which was printing the display in webview.

So far, I don't have a clear picture of the possible methods to do this, but what is certain is that it is very possible to do it, that is the conclusion I have drawn from various reference sources that I have read.

I am interested in how to utilize the PrintManager and PrintDocumentAdapter classes, but the way to run them is not easy because we cannot just rely on the main thread / ui thread, so I need to create a new job to run this task in the background (asynchronously), so as not to block the ui thread.

Here's an overview of the code.

public  void createWebPagePrint(WebView webView) {
		/*if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) 
            return;*/
	    PrintManager printManager = (PrintManager) getSystemService(Context.PRINT_SERVICE);
	    PrintDocumentAdapter printAdapter = webView.createPrintDocumentAdapter();
	    String jobName = getString(R.string.webapp_name) + " Document";
	    PrintAttributes.Builder builder = new PrintAttributes.Builder();
	    builder.setMediaSize(PrintAttributes.MediaSize.ISO_A5);
	    PrintJob printJob = printManager.print(jobName, 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();
	    }
	    // Save the job object for later status checking
	}

Post a Comment

Previous Next

نموذج الاتصال