gmuth / ipp-client-kotlin

A client implementation of the ipp protocol written in kotlin
MIT License
75 stars 10 forks source link

Connect to a HP printer but fail to print out the file #14

Closed miaoxinli closed 1 year ago

miaoxinli commented 1 year ago

Thanks in advance for your library.

        val uri = URI.create("ipp://192.168.1.30/ipp/printer")
        val file = File(this@FirstFragment.requireContext().filesDir,"hello_kitty2.jpg")

        val ippClient = IppClient()
        val request = IppRequest(IppOperation.PrintJob, uri).apply {
            // constructor adds 'attributes-charset', 'attributes-natural-language' and 'printer-uri'
            operationGroup.attribute("document-format", IppTag.MimeMediaType, "image/urf")
            documentInputStream  = FileInputStream(file)
        }
        val response = ippClient.exchange(request)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            response.jobGroup.forEach { t, u ->
                println(t + u.toString())
            }
        }

or

        val ippPrinter = IppPrinter("ipp://192.168.1.30/ipp/printer")
        val width = 2540 * 2 // hundreds of mm
        val bitmap: Bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.hello_kitty);
        val stream = ByteArrayOutputStream()
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream)
        val byteArray = stream.toByteArray()
        val printJob = ippPrinter.printJob(
            byteArray,
            IppMedia.Collection(
                size = IppMedia.Size(width, width * 2),
                margins = IppMedia.Margins(0)
            )
        )
        printJob.logDetails()

I have tried different APIs, the job state is pending always as below.

2023-02-21 17:16:14.601 14767-14873/com.example.airprint I/System.out: job-urijob-uri (uri) = ipp://192.168.1.30/ipp/print/0208
2023-02-21 17:16:14.601 14767-14873/com.example.airprint I/System.out: job-idjob-id (integer) = 208
2023-02-21 17:16:14.601 14767-14873/com.example.airprint I/System.out: job-statejob-state (enum) = pending
2023-02-21 17:16:14.601 14767-14873/com.example.airprint I/System.out: job-state-reasonsjob-state-reasons (1setOf keyword) = none

This is the supported document format information. I wonder if is there any way to figure out what is wrong.

com.example.airprint I/System.out: IppAttributesGroup        INFO    document-format-supported (1setOf mimeMediaType) = image/urf,application/PCLm,application/octet-stream
dependencies {
    implementation("de.gmuth:ipp-client:2.3")
    implementation("com.github.jmdns:jmdns:3.5.8")
}
gmuth commented 1 year ago

Thanks for your report. However it looks like your issue is caused by using the wrong formats or not declaring the format correctly e.g. with documentFormat("image/jpeg") This library is about transmitting printable documents to ipp printers. It does not support rendering or converting document formats to other pdls. Your printer only announces urf and PCLm as supported pdls, so you can try to produce your print data accordingly. Sometimes printers support more formats then they announce. JPEG and PWG are worth a try. Make sure you set the correct mimeType as document format. I recommend to look at jipps pdl-render that supports PCLm. Maybe you can use their PCLmWriter to render your bitmap.