Save webview to pdf

MainActivity.java class

package com.pankaj.yourpckagename;

import android.content.Context;
import android.os.Bundle;
import android.print.PrintAttributes;
import android.print.PrintDocumentAdapter;
import android.print.PrintManager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {

//create object of webView
private WebView myWebView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//initialize webview
myWebView = findViewById(R.id.myWebView);
myWebView.getSettings().setBuiltInZoomControls(false);
myWebView.getSettings().setSupportZoom(false);
myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
myWebView.getSettings().setAllowFileAccess(true);
myWebView.getSettings().setJavaScriptEnabled(true);
WebSettings webSettings=myWebView.getSettings();

myWebView.loadUrl("https://xyz@@@/");
//add webview client to handle event of loading
myWebView.setWebViewClient(new WebViewClient() {

public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}

@Override
public void onPageFinished(WebView view, String url) {
//if page loaded successfully then show print button
findViewById(R.id.fab).setVisibility(View.VISIBLE);
}
});

//prepare your html content which will be show in webview

}

private void createWebPrintJob(WebView webView) {

//create object of print manager in your device
PrintManager printManager = (PrintManager) this.getSystemService(Context.PRINT_SERVICE);

//create object of print adapter
PrintDocumentAdapter printAdapter = webView.createPrintDocumentAdapter();

//provide name to your newly generated pdf file
String jobName = getString(R.string.app_name) + " Print Test";

//open print dialog
printManager.print(jobName, printAdapter, new PrintAttributes.Builder().build());
}

//perform click pdf creation operation on click of print button click
public void printPDF(View view) {
createWebPrintJob(myWebView);
}
}

Xml file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.yourpackage"
>

<WebView
android:id="@+id/myWebView"
android:layout_width="match_parent"
android:layout_height="match_parent"
></WebView>

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_gravity="bottom|end"
android:layout_margin="20dp"
android:onClick="printPDF"
android:src="@drawable/print"
android:visibility="gone"
/>

</RelativeLayout>

Android mainifesto file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.package"
>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CALL_PHONE" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
>
<activity android:name=".Splash">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme"
/>
</application>

</manifest>

Leave a Reply

Your email address will not be published. Required fields are marked *