android-print-manager

can to print pdf using predefined print framework?


How to print PDF using predefined PDF framework in android ?

How does Print-Spooler Api used to print PDF files


Solution

  • This code worked for me

    val printManager = this.getSystemService(Context.PRINT_SERVICE) as PrintManager
                val jobName = this.getString(R.string.app_name) + " Document"
                try{
                    printManager.print(jobName, pda, null)
                }
                catch(ex:RuntimeException)
                {
                    Toast.makeText(this,"Can't print pdf file",Toast.LENGTH_SHORT).show()
                }
    

    PrintDocumentAdapter.kt

      var pda: PrintDocumentAdapter = object : PrintDocumentAdapter() {
        
                override fun onWrite(
                        pages: Array<PageRange>,
                        destination: ParcelFileDescriptor,
                        cancellationSignal: CancellationSignal,
                        callback: WriteResultCallback
                ) {
                    var input: InputStream? = null
                    var output: OutputStream? = null
                    try {
                        input = uri?.let { contentResolver.openInputStream(it) }
        
                        output = FileOutputStream(destination.fileDescriptor)
                        val buf = ByteArray(1024)
                        var bytesRead: Int
                        if (input != null) {
                            while (input.read(buf).also { bytesRead = it } > 0) {
                                output.write(buf, 0, bytesRead)
                            }
                        }
                        callback.onWriteFinished(arrayOf(PageRange.ALL_PAGES))
                    } catch (ee: FileNotFoundException) {
                        //Code to Catch FileNotFoundException
                    } catch (e: Exception) {
                       //Code to Catch exception
                    } finally {
                        try {
                            input!!.close()
                            output!!.close()
                        } catch (e: IOException) {
                            e.printStackTrace()
                        }
                    }
                }
        
                override fun onLayout(
                        oldAttributes: PrintAttributes,
                        newAttributes: PrintAttributes,
                        cancellationSignal: CancellationSignal,
                        callback: LayoutResultCallback,
                        extras: Bundle
                ) {
                    if (cancellationSignal.isCanceled) {
                        callback.onLayoutCancelled()
                        return
                    }
                    val pdi = PrintDocumentInfo.Builder("Name of file")
                        .setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT).build()
                    callback.onLayoutFinished(pdi, true)
                }
            }