![]() |
VOOZH | about |
Sometimes there is a need to save some articles available on the internet in the form of PDF files. And to do so there are many ways, one can use any browser extension or any software or any website to do so. But in order to implement this feature in the android app, one can't rely on other software or websites to do so. So to implement this amazing feature in the android app follow this tutorial. A sample GIF is given below to get an idea about what we are going to do in this article.
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that choose Java as the programming language though we are going to implement this project in Java language.
<uses-permission android:name="android.permission.INTERNET"/>
In the activity_main.xml file, there is one WebView that is used to load the websites and one Button which is used to save the loaded webpage to PDF file. Here is the code for the activity_main.xml file.
// creating object of WebView
WebView printWeb;
// Initializing the WebView
final WebView webView=(WebView)findViewById(R.id.webViewMain);// Initializing the Button
Button savePdfBtn=(Button)findViewById(R.id.savePdfBtn);
// Setting WebView Client
webView.setWebViewClient(new WebViewClient()
{
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
// initializing the printWeb Object
printWeb=webView;
}
});
// loading the URL
webView.loadUrl("https://www.google.com//");
// setting clickListener for Save Pdf Button
savePdfBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(printWeb!=null)
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// Calling createWebPrintJob()
PrintTheWebPage(printWeb);
}else
{
// Showing Toast message to user
Toast.makeText(MainActivity.this, "Not available for device below Android LOLLIPOP",
Toast.LENGTH_SHORT).show();
}
}
else
{
// Showing Toast message to user
Toast.makeText(MainActivity.this, "WebPage not fully loaded", Toast.LENGTH_SHORT).show();
}
}
});
// object of print job
PrintJob printJob;// a boolean to check the status of printing
boolean printBtnPressed=false;
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void PrintTheWebPage(WebView webView) {
// set printBtnPressed true
printBtnPressed=true;
// Creating PrintManager instance
PrintManager printManager = (PrintManager) this
.getSystemService(Context.PRINT_SERVICE);
// setting the name of job
String jobName = getString(R.string.app_name) + " webpage"+webView.getUrl();// Creating PrintDocumentAdapter instance
PrintDocumentAdapter printAdapter = webView.createPrintDocumentAdapter(jobName);// Create a print job with name and adapter instance
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()) {
// Showing Toast Message
Toast.makeText(this, "Completed", Toast.LENGTH_SHORT).show();
} else if (printJob.isStarted()) {// Showing Toast Message
Toast.makeText(this, "isStarted", Toast.LENGTH_SHORT).show();} else if (printJob.isBlocked()) {
// Showing Toast Message
Toast.makeText(this, "isBlocked", Toast.LENGTH_SHORT).show();} else if (printJob.isCancelled()) {
// Showing Toast Message
Toast.makeText(this, "isCancelled", Toast.LENGTH_SHORT).show();} else if (printJob.isFailed()) {
// Showing Toast Message
Toast.makeText(this, "isFailed", Toast.LENGTH_SHORT).show();} else if (printJob.isQueued()) {
// Showing Toast Message
Toast.makeText(this, "isQueued", Toast.LENGTH_SHORT).show();}
// set printBtnPressed false
printBtnPressed=false;}
}
Resources: