Save & Print PDF from Webview on Android

 


Save and Print Pdf  (Java):

This post shows how to programmatically save and and Print PDF from any web page shown in an Android Webview ( Using programming language java). Frist Create an Empty Activity Project in Android Studio. if you add print and save option in your old project also you can do that.
 

Printing a PDF from a WebView :

It is very helpful for webview users. printing pdf and any webpage for educational purpose. how to you do that ? just follow some steps.

Steps 1.
Add internet permission 
 <uses-permission android:name="android.permission.INTERNET" />  
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />  
Steps 2.
Go to Android to Project. create a Direcatory Name Android. into the Android Directory create another Directory Name  Print . then you create a xml file and file Name annotations.xml
Example :

annotations.xml code :
 <root>  
   <item name='android.print.PrintDocumentAdapter.WriteResultCallback'>  
     <annotation name='java.lang.Deprecated' />  
   </item>  
 </root>  
Steps 3.
go to activity_main.xml and write this code. must you use mother layout is RelativeLayout 
 <WebView  
     android:id="@+id/webView"  
     android:layout_width="match_parent"  
     android:layout_height="match_parent"  
     />  
   <com.google.android.material.floatingactionbutton.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:src="@mipmap/ic_launcher"  
     android:visibility="gone" />  
Steps 4.
go to MainActivity.java and follow this code 
 public class MainActivity extends AppCompatActivity {  
   private WebView webView;  
   FloatingActionButton fab;  
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_main);  
     //initialize WebView  
     webView = findViewById(R.id.webView);  
     fab = findViewById(R.id.fab);  
     fab.setOnClickListener(new View.OnClickListener() {  
       @Override  
       public void onClick(View v) {  
         // Code Here ==========  
         createWebPrintJob(webView);  
       }  
     });  
     webView.setWebChromeClient(new WebChromeClient());  
     webView.getSettings().setJavaScriptEnabled(true);  
     // WebView loading handling  
     webView.setWebViewClient(new WebViewClient() {  
       public boolean shouldOverrideUrlLoading(WebView view, String url) {  
         return false;  
       }  
       @Override  
       public void onPageFinished(WebView view, String url) {  
         //if WebView load successfully then VISIBLE fab Button  
         fab.setVisibility(View.VISIBLE);  
       }  
     });  
     webView.loadUrl("https://www.google.com");  
   } // OnCreate method close here =============  
   private void createWebPrintJob(WebView webView) {  
     PrintManager printManager = (PrintManager) this.getSystemService(Context.PRINT_SERVICE);  
     PrintDocumentAdapter printAdapter = webView.createPrintDocumentAdapter();  
     String jobName = getString(R.string.app_name) + " Print Test";  
     printManager.print(jobName, printAdapter, new PrintAttributes.Builder().build());  
   }  
 } // Public Clase close here =========================  

Post a Comment

Previous Post Next Post
Girl in a jacket