Advertisement
Guest User

print service

a guest
May 23rd, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 3.89 KB | None | 0 0
  1. menu
  2. =============
  3. <?xml version="1.0" encoding="utf-8"?>
  4. <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
  5.     <item android:id="@+id/action_print"
  6.           android:orderInCategory="100"
  7.           app:showAsAction="never"
  8.           android:title="فاتورة طباعة"
  9.           />
  10. </menu>
  11.  
  12. layout
  13. ===========
  14. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  15.               android:orientation="vertical"
  16.               android:layout_width="match_parent"
  17.               android:layout_height="match_parent"
  18. >
  19.     <WebView
  20.             android:layout_width="match_parent"
  21.             android:layout_height="match_parent"
  22.             android:id="@+id/webView"/>
  23.  
  24. </LinearLayout>
  25.  
  26. java
  27. ============
  28. package com.example.nadininvoice
  29.  
  30. import android.content.Context
  31. import android.support.v7.app.AppCompatActivity
  32. import android.os.Bundle
  33. import android.print.PrintAttributes
  34. import android.print.PrintManager
  35. import android.util.Log
  36. import android.view.Menu
  37. import android.view.MenuItem
  38. import android.webkit.WebResourceRequest
  39. import android.webkit.WebView
  40. import android.webkit.WebViewClient
  41. import android.widget.Toast
  42. import kotlinx.android.synthetic.main.activity_main.*
  43.  
  44. class MainActivity : AppCompatActivity() {
  45.     //declare of web object
  46.     private var myWebView: WebView?=null
  47.     //simple html page for testing
  48.     val htmlDocument = "<html><body><h1>Nadin Wedding bus cost</h1><hr>" +
  49.             "<p>Bus for friends from hacker U - 3000Nis</p>" +
  50.             "<p>Bus for groom familiy (Hassan) - 7500nis</p>" +
  51.             "<p>Bus for groom friends (Hassan) - 7500nis</p>" +
  52.             "<hr><b>total cost : 18000Nis</b>" +
  53.             "</body></html>"
  54.  
  55.     override fun onCreate(savedInstanceState: Bundle?) {
  56.         super.onCreate(savedInstanceState)
  57.         setContentView(R.layout.activity_main)
  58.         printWebView()
  59.         Toast.makeText(this,"loading site...",Toast.LENGTH_LONG).show()
  60.     }
  61.  
  62.     private fun printWebView()
  63.     {
  64.         val webView = WebView(applicationContext)
  65.         webView.webViewClient = object : WebViewClient(){
  66.             override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
  67.                 return false
  68.             }
  69.  
  70.             override fun onPageFinished(view: WebView?, url: String?) {
  71.                 createWebPrintJob(view)
  72.                 myWebView=null
  73.             }
  74.         }
  75.  
  76.         //load the html data into the webView
  77.         //webView.loadDataWithBaseURL(null,htmlDocument,"text/HTML","UTF-8",null)
  78.         //load the html data into the webView by a url
  79.         webView.loadUrl(("https://www.hackeru.co.il/course/applications"))
  80.         myWebView=webView
  81.     }
  82.  
  83.     private fun createWebPrintJob(view: WebView?){
  84.         //getting the print service
  85.         val printManger = getSystemService(Context.PRINT_SERVICE) as PrintManager
  86.         //get the print adapter
  87.         val printAdapter = view!!.createPrintDocumentAdapter("myDoucment")
  88.         // create a job name
  89.         val jobName = getString(R.string.app_name)+ " print test"
  90.         //all is ready, you can print the document, create a print command
  91.         printManger.print(jobName,printAdapter,PrintAttributes.Builder().build())
  92.     }
  93.  
  94.     override fun onOptionsItemSelected(item: MenuItem?): Boolean {
  95.         /*
  96.             if (item.itemId == R.id.action_print)
  97.             {
  98.                 createWebPrintJob(myWebView)
  99.             }
  100.         */
  101.         when (item!!.itemId){
  102.             R.menu.menu->{
  103.                 createWebPrintJob(myWebView)
  104.             }
  105.             else->{
  106.  
  107.             }
  108.         }
  109.         return super.onOptionsItemSelected(item)
  110.     }
  111.  
  112.     override fun onCreateOptionsMenu(menu: Menu?): Boolean {
  113.         menuInflater.inflate(R.menu.menu,menu)
  114.         return true
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement