import
android.content.Context;
import
android.os.Build;
import
android.os.Bundle;
import
android.print.PrintAttributes;
import
android.print.PrintDocumentAdapter;
import
android.print.PrintJob;
import
android.print.PrintManager;
import
android.view.View;
import
android.webkit.WebView;
import
android.webkit.WebViewClient;
import
android.widget.Button;
import
android.widget.Toast;
import
androidx.annotation.RequiresApi;
import
androidx.appcompat.app.AppCompatActivity;
public
class
MainActivity
extends
AppCompatActivity {
WebView printWeb;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final
WebView webView = (WebView) findViewById(R.id.webViewMain);
Button savePdfBtn = (Button) findViewById(R.id.savePdfBtn);
webView.setWebViewClient(
new
WebViewClient() {
@Override
public
void
onPageFinished(WebView view, String url) {
super
.onPageFinished(view, url);
printWeb = webView;
}
});
savePdfBtn.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View view) {
if
(printWeb !=
null
) {
if
(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
PrintTheWebPage(printWeb);
}
else
{
Toast.makeText(MainActivity.
this
,
"Not available for device below Android LOLLIPOP"
, Toast.LENGTH_SHORT).show();
}
}
else
{
Toast.makeText(MainActivity.
this
,
"WebPage not fully loaded"
, Toast.LENGTH_SHORT).show();
}
}
});
}
PrintJob printJob;
boolean
printBtnPressed =
false
;
@RequiresApi
(api = Build.VERSION_CODES.LOLLIPOP)
private
void
PrintTheWebPage(WebView webView) {
printBtnPressed =
true
;
PrintManager printManager = (PrintManager)
this
.getSystemService(Context.PRINT_SERVICE);
String jobName = getString(R.string.app_name) +
" webpage"
+ webView.getUrl();
PrintDocumentAdapter printAdapter = webView.createPrintDocumentAdapter(jobName);
assert
printManager !=
null
;
printJob = printManager.print(jobName, printAdapter,
new
PrintAttributes.Builder().build());
}
@Override
protected
void
onResume() {
super
.onResume();
if
(printJob !=
null
&& printBtnPressed) {
if
(printJob.isCompleted()) {
Toast.makeText(
this
,
"Completed"
, Toast.LENGTH_SHORT).show();
}
else
if
(printJob.isStarted()) {
Toast.makeText(
this
,
"isStarted"
, Toast.LENGTH_SHORT).show();
}
else
if
(printJob.isBlocked()) {
Toast.makeText(
this
,
"isBlocked"
, Toast.LENGTH_SHORT).show();
}
else
if
(printJob.isCancelled()) {
Toast.makeText(
this
,
"isCancelled"
, Toast.LENGTH_SHORT).show();
}
else
if
(printJob.isFailed()) {
Toast.makeText(
this
,
"isFailed"
, Toast.LENGTH_SHORT).show();
}
else
if
(printJob.isQueued()) {
Toast.makeText(
this
,
"isQueued"
, Toast.LENGTH_SHORT).show();
}
printBtnPressed =
false
;
}
}
}